Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend_Loader Vs Autoloader with Zend_Test and PHPUnit

I am setting up testing with PHPUnit and Zend Framework and have a niggling issue with autoloading.

Firstly, I'm not sure why I need to setup autoloading in my the phpunit bootstrap.php. In the production environment, the index.php file isn't loading it? (My test cases are extending Zend_Test_PHPUnit_ControllerTestCase).

Secondly, I'm having difficulty replacing Zend_Loader with Zend_Loader_Autoload.

If I use this code in my bootstrap...

require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();

... the tests run fine, but with a warning about Zend_Loader being deprecated.

If I use this code, however...

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

The tests can't find a library class and a fatal error results.

In my application.ini I have the line autoloaderNamespaces[] = "CP_". It seems that the Loader class can see it, but not the Autoloader class. Curious...

Any ideas?

Thanks!

like image 974
DatsunBing Avatar asked Jun 16 '11 10:06

DatsunBing


1 Answers

PHPUnit sometimes uses the cli/php.ini config instead of the default one. This could be where your problem is originating as you may have added library include paths to your php.ini but neglected to add them to the cli/php.ini.

I add my libraries to the test bootstrap manually rather than through the ini file. You could try adding something like this under your autoloader lines in the bootstrap:

$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
    'basePath' => APPLICATION_PATH,
    'namespace' => '',
    'resourceTypes' => array(
        'cplib' => array(
            'path' => 'library/CP',
            'namespace' => 'CP_'
        ),
    ),
));
like image 68
Tom Jowitt Avatar answered Nov 08 '22 22:11

Tom Jowitt