Class not found when autoloading custom resource types during PHPUnit testing

517 views Asked by At

In my Zend Framework project, I am using some custom resource types that I add to the resource loader in my application's Bootstrap.php file.

<?php

    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    {
        protected function _initAutoloader()
        {       
            $resourceLoader = $this->getResourceLoader();
            $resourceLoader->addResourceTypes(array(
                'infrastructure' => array(
                    'namespace' => 'Infrastructure',
                    'path'      => 'infrastructure/',
                ),
                'interfaces' => array(
                    'namespace' => 'Interface',
                    'path'      => 'interfaces/',
                ),
                'default' => array(
                    'namespace' => '',
                    'path'      => '/',
                ),
            ));
        }

        ...

}

I can autoload these resources okay when running the application, but when running PHPUnit and trying to load these classes during tests, they cannot be found.

PHP Fatal error:  Class 'Wbp_Infrastructure_Persistence_InMemory_TagRepository' not found in /var/www/worldsbestprizes/tests/library/Mbe/Validate/TaggedUriTest.php on line 37

The tests/bootstrap.php file looks like this.

<?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';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Mbe_');

$application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
);

Is there something I should add to it to allow those custom resource types to be loaded?

1

There are 1 answers

3
David Stockton On

You need to register the Wbp_ namespace in a similar method to what you're doing in the tests/bootstrap.php file:

$autoloader->registerNamespace('Wbp_');