Problem finding class when executing unittests

In my project I have a folder lib with a child folder classes.  In the classes folder I have a file called, "MyTestCase.php" with the following class

This lives in... < my_project>/lib/classes


<?php
/**
* Created by JetBrains PhpStorm.
* To change this template use File | Settings | File Templates.
*/

class MyTestCase extends PHPUnit_Framework_TestCase
{
}

?>

I then created a test case that lives in a tests directory under my project....


This lives in... < my_project>/lib/classes/tests



<?php
/**
* Created by JetBrains PhpStorm.
* To change this template use File | Settings | File Templates.
*/
require_once("/lib/classes/MyTestCase.php");
class SampleTest extends MyTestCase
{
  public function testSimple()
  {
    self::assertContains("123", "123");
  }
}
?>


When I attempt to run my test case I got the following error...


E_ERROR: Class 'MyTestCase' not found
#0 /tests/caltman/SampleTest.php(11)
PHP Fatal error:  Class 'MyTestCase' not found in /tests/caltman/SampleTest.php on line 11


So I thought, maybe I need to add lib/classes to my PATH.  So in my ~/.profile file I added it to my PATH and then sourced it.  Also, in phpStorm in the PHP Include paths I added it...

/Users/<my_username>/Projects/<my_project>/lib/classes

Now had this worked, this would not be ideal since I work on multiple branches and would have to constantly update this.  However, unfortunately it did NOT work.  So I'm not sure why it can't find the class.

So I then attempted to do a full path...

require_once("/Users/<my_username>/Projects/<my_project>/lib/classes/MyTestCase.php");

That worked, I'm not sure why it's different since I've set up the include paths in phpStorm and my PATH variable.

At this point I am at a loss.  Any ideas?  Any additional information needed?

OS: Snow Leopard
phpStorm: 2.0 (Build #PS-103.99)
PHPUnit: 3.5 (Latest in GitHub)
0
9 comments

It appears, and I might be off here, that phpStorm is not using the "Include path" variables and I am not sure where it is getting the path from?  I am getting this error...

E_COMPILE_ERROR: require_once(): Failed opening required 'MyTestCase.php' (include_path='.:/usr/lib/php')

This does not match any of the defined include paths I have listed...

!php.ini
!PATH
!phpStorm

0

Hi Chris,

Want to use "path" path ?  -- look at "include_path" setting in php.ini (and set_include_path() php function to work with it during runtime).

But since you are working on different branches, I may recommend the following approach:

1) create bootstrap.php in your tests folder and do all common initialisations here (like, including and initialising autoloader, loading additional classes etc). For example, the following code can be used (you may need to alter it a bit as I may misunderstand your directory structure and file locations):

<?php
require_once (__DIR__ . '/../MyTestCase.php');

That file should load MyTestCase.php (which, as I understand, is located just a folder above) so it will be available when running the tests.

2) create phpunit.xml (or any other named file) in your tests folder. Here is an example of such file:

<?xml version="1.0"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="true"
         bootstrap="./bootstrap.php"
         colors="false"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="false"
         convertWarningsToExceptions="false"
         processIsolation="false"
         stopOnFailure="false"
         strict="true"
         syntaxCheck="false"
         verbose="true">
    <groups>
        <exclude>
            <group>disabled</group>
            <group>skip</group>
        </exclude>
    </groups>
</phpunit>

The most important part is highlighted bold: bootstrap="./bootstrap.php". This instructs PHPUnit to use bootstrap file mentioned in #1 (which will be located in the same folder as phpunit.xml).

Now, when you run your PHPUnit tests (via command line/terminal or trough PhpStorm interface), just mention the path to this config file -- phpunit.xml (see documentation to phpunit command or use appropriate field in PhpStorm interface when creating PHPUnit run configuration).

0

Creating the bootstrap file allowed me to include each of the files as I need, I could even go as far as to create an autoload function in that file yes?

However, there are requires in some of my tests that I'd like to work without having to add them to the bootstrap.php...

How do I get my include_path updated?  It doesn't seem to be acknowledging changes to any of the areas I listed above...  it doesn't seem to listen to the php.ini either.

0
Creating the bootstrap file allowed me to include each of the files as I need, I could even go as far as to create an autoload function in that file yes?

Well, the choice is completely yours. You can:
1) manually include all required files (I don't see this practical, as you may need to include hundreds of files if you have a lot of tests)
2) declare and use custom autoloader in this file : http://php.net/manual/en/function.spl-autoload_register.php
3) use existing autoloader from your framework/classes (if you have one)

However, there are requires in some of my tests that I'd like to work without having to add them to the bootstrap.php...

Sorry .. I don't really understand what you mean here. If you want to run some tests where some (or most) of your classes should not be available (or run each test separately so any previous test will not affect the next test), I see 2 paths:

1) use @group and run unit tests for that group only (using separate PHPUnit run configuration or separate phpunit.xml config)

2) use processIsolation="true" in your config file or @runTestInSeparateProcess

How do I get my include_path updated?  It doesn't seem to be acknowledging changes to any of the areas I listed above...  it doesn't seem to listen to the php.ini either.

Sorry, I do not know what kind of problems you are having here. Maybe you need to read php manual to refresh/investigate how this option works. When PHP interpreter cannot find required file or class, it will utilize autoloader functionality. If custom autoloaders fail to load the class (if any present at all), then default autoloader will be invoked. It will simply search for a files in the list of directories found in include_path setting. If such file does exist in subfolder of such folder, PHP will still fail to load it (default autoloader is very simple, that is why I recommend to use custom autoloader instead).

Autoloaders are working with classes/interfaces while include_path -- with files that are included via require/include instructions.

0

Yes I think you are misunderstanding me...

Let me try and state it a again with an example.

I have a Test file that has a require_once in it.  My first question is should it look at the include_path and then append the require_once path parameter to load the file?  My expectation is that it will.  So for example:

require_once('tests/lib/TestData.php');

If the full path to the file is...

/Users/someUser/Projects/someProject/tests/lib/TestData.php

and I have

/Users/someUser/Projects/someProject/

in my include_path then shouldn't it load that file correctly?

I have confirmed that I have "/Users/someUser/Projects/someProject/" in my path yet phpStorm seems to ignore it and doesn't find the file.  

0

1) Where did you entered this path: /Users/someUser/Projects/someProject/
inside PhpStorm (File | Settings | PHP) .. or actually in php.ini  .. or inside some included PHP file (bootstrap.php) using set_include_path() ?

2) Who cannot load that file -- PhpStorm or PHP interpreter when you run your unit tests?

0

I put it in PhpStorm (File | Settings | PHP) and in php.ini.

phpStorm is the one having the problem I haven't tried running it in the PHPUnit interpretor.

0

php.ini is for PHP interpreter obviously and has no effect on PhpStorm directly.

"File | Settings | PHP" is for PhpStorm only. Such directories should be listed in you Project view panel under "External Libraries" branch.

Am I correct and /Users/someUser/Projects/someProject/ is part of the project (File | Settings | Directories) and is also entered as External library at the same time? I'm unsure but I think this may cause problems.

In any way -- I cannot figure out what may be wrong here without having more details and (highly preferably) seeing screenshots. Maybe one of the developers with their support experience can catch this easier and quicker.

phpStorm is the one having the problem I haven't tried running it in the PHPUnit interpretor.

You mean some warning in editor area ("cannot find include file" or something like that) .. or when you run PHPUnit tests from PhpStorm ?

0
Avatar
Valeria Nikolaenko

Chris, is your require_once("/lib/classes/MyTestCase.php"); statement highlighted with a error message "Can't resolve target of expression..."?

0

Please sign in to leave a comment.