Search code examples
phpzend-frameworkphpunit

PHPUnit cannot locate Zend_Test_PHPUnit_ControllerTestCase when outside of base "tests" folder


I'm running Wamp Server with PHP 5.3.4, Zend 1.11.11, and PHPUnit 3.7.1. The structure of my project is the default Zend layout with the tests folder organized as such:

tests/
   application/
   library/
   bootstrap.php
   phpunit.xml
   IndexControllerTest.php

Where both application/ and library/ are empty. This base case works fine with my current setup, where I can run phpunit IndexControllerTest from terminal and it runs the tests without fault. The problem arises as soon as I move IndexControllerTest.php to a different location (say, within the application/ directory). I then receive a fatal error from phpunit:

PHP Fatal error: Class 'Zend_Test_PHPUnit_ControllerTestCase' not found in 
X:\Program Files (x86)\Wamp\www\Local_site\Zend\login\tests\application\IndexControllerTest.php 
on line 3

Now unless I'm mistaken, the Autoloader should be taking care of this require(), but something is going wrong as soon as I change the location of my test file. I have tried tweaking the phpunit.xml file over and over, but I am not sure if that is where my problem lies, or in my bootstrap.php, or in my code. Any insight would be greatly appreciated. Even a nudge in the right direction. Here are the relevant files:

phpunit.xml

<phpunit bootstrap="./bootstrap.php">
<testsuites>
<testsuite name="My Project">
    <directory>./tests</directory>
</testsuite>

</testsuites>

<filter>
    <!-- If Zend Framework is inside your project's library, uncomment this filter -->

    <whitelist>
        <directory suffix=".php">/library/Zend</directory>
    </whitelist>

</filter>
</phpunit>

bootstrap.php

<?php

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'testing'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));


require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();

IndexControllerTest.php

<?php
class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
    protected $application;

    public function setUp() 
    {
        // Assign and instantiate in one step:
        $this->bootstrap = new Zend_Application(
            'testing',
            APPLICATION_PATH . '/configs/application.ini'
            );
        parent::setUp();
    }

    public function testTrue()
    {
         $this->assertTrue(true);
    }
}

As per other suggestions from the golden internet, I also altered my php/php.ini include_path to be:

include_path=".;X:\Program Files (x86)\Wamp\bin\php\php5.3.4\pear;X:\Program Files (x86)\Wamp\bin\php\php5.3.4\pear\PHPUnit"

Do I need to explicitly include Zend/Test/PHPUnit/ControllerTestCase.php? I've tried hundreds of solutions already, but I've been flying blind so I could have been very close and not even known it.


Solution

  • Managed to find a solution (as simple as it is...) to this problem. For whatever reason phpunit.xml was not bootstrapping my bootstrap.php file. All it required was requiring my bootstrap before I declared my class in IndexControllerTest.php:

    IndexControllerTest

    require_once '/path/to/bootstrap.php';
    
    IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
    {
          public function testTrue() 
          {
                  $this->assertTrue(true);
          }
    }
    

    It seems odd having to require this in each test file, and I am sure I will find a more permanent solution soon. Hopefully this helps someone.

    EDIT

    Slightly better solution found where I simply copied my phpunit.xml file to the same directory as IndexControllerTest and changed bootstrap="bootstrap.php" to bootstrap="../bootstrap.php" in the new phpunit.xml file. Now no require_once is needed for all test files in that directory. Still seems like a rough fix, but better than nothing.