dongmo20030416 2012-06-01 08:26
浏览 57

PHPUnit中Zend Autoload of Model

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.

  • 写回答

1条回答 默认 最新

  • dongtu1958 2012-06-23 16:11
    关注

    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_");
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line