PHPUnit中模型的Zend自动加载


Zend Autoload of Model in PHPUnit

我正试图为正在编写的ZF应用程序编写一些测试用例,但我似乎无法克服这一点。

我扩展了Zend_Test_PHPUnit_ControllerTestCase,添加了以下内容:

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

它可以工作,并初始化Zend Autoloader,允许我在库中加载其他类以及Zend类。所以我设置了一个测试:

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

遗憾的是,测试从未通过调度。相反,它给了我一个错误:

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

Action.php是一个扩展Zend_Controller_Action的类。其中,有一个函数使用App_Model_User对登录用户进行身份验证。自从Autoloader工作以来,我从未添加过require_once。奇怪的是:它可以通过我的浏览器工作。只是不在PHPUnit。

所以我丢弃了在Zend Autoloader:注册的自动加载器

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

它显示了我的所有模块,以及视图、控制器、模型等的名称空间。我通过浏览器和匹配的转储进行了同样的操作。

Zend Autoloader需要添加App_作为自动加载前缀,以便按预期工作,类似于以下内容:

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace("App_");