0

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?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Luke Eller
  • 627
  • 1
  • 7
  • 23

1 Answers1

1

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_');
David Stockton
  • 2,261
  • 1
  • 14
  • 20
  • Hi David, thank you for your reply. I've added the code suggested, but it looks like the autoloader is now looking in the library path for the class, unsuccessfully. The class in question resides in `APPLICATION_PATH/infrastructure/Persistence/InMemory`, i.e. `application/infrastructure/Persistence/InMemory` and not the library path – Luke Eller Mar 13 '12 at 22:28
  • With the change I now get the following error. I get the following error `Warning: include_once(): Failed opening 'Wbp/Infrastructure/Persistence/Db/TagRepository.php' for inclusion (include_path='/var/www/wbp/application/../library:/var/www/wbp/library:.:/usr/share/php:/usr/share/pear:/home/luke/.netbeans/6.9/zend:/usr/share/php/libzend-framework-php') in /usr/share/php/libzend-framework-php/Zend/Loader.php on line 146` – Luke Eller Mar 13 '12 at 22:34
  • Since you're running on linux (assumption) the file system is going to be case sensitive. If you can put the file in APPLICATION_PATH/Wbp/Infrastructure/Persistence/Db/TagRepository.php then it will load properly. If you do not want to have the Wbp in the path, I'd still suggest capitalizing the Infrastructure directory. In addition, you'll need to add a namespace map that says for Wbp_* stuff, look in APPLICATION_PATH/Infrastructure/.... If you still want it to be in lower-case infrastructure, you'll need to add a Wbp_Infrastructure_ to APPLICATION_PATH/infrastructure. – David Stockton Mar 15 '12 at 15:29