Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Autoload of Model in PHPUnit

I'm trying to write some test cases for a ZF application I'm writing but I can't seem get past this point.

I have extended the Zend_Test_PHPUnit_ControllerTestCase, adding the following:

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

It works, and it initializes the Zend Autoloader, allowing me to load other classes inside my library as well as Zend classes. So I setup a test:

public function testUserUnauthorized() {
    $this->dispatch('/api/user');
    //Assertions...
}

Sadly, the test never gets past the dispatch. Instead, it gives me an error:

Fatal error: Class 'App_Model_User' not found in ....Action.php

Action.php is a class extending Zend_Controller_Action. In it, there is a function that uses App_Model_User to authenticate the logged in user. I never had to add a require_once since the Autoloader works. That's the strange part: it works through my browser. Just not in PHPUnit.

So I dumped the Autoloaders registered with the Zend Autoloader:

public function testUserUnauthorized() {
    print_r(Zend_Loader_Autoloader::getInstance()->getAutoloaders());
    exit;
    $this->dispatch('/api/user');
}

It shows all my modules, as well as the namespaces for views, controllers, models, etc. I did the same through my browser and the dumps matched.

like image 291
Nick Caballero Avatar asked Dec 04 '25 15:12

Nick Caballero


1 Answers

Zend Autoloader needs App_ added as an autoload prefix for this to work as you expect, something like this:

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace("App_");
like image 197
williamvicary Avatar answered Dec 06 '25 03:12

williamvicary