在 phpunit 类中加载 zend-framework 模型


Loading Zend-Framework Model in Phpunit Class

我想调用我的 zend-framework 模型并在AuthenticationControllerTest.php中使用那里的函数,但是当我从终端运行它时我遇到错误。

 - -MyZendproject
 - -application
    -model
     -testmodel
 - +public
 - -tests
   - aplication
     - controller
       - .AuthenticationControllerTest.php

这是我AuthenticationControllerTest.php文件

<?php
require_once `PHPUnit/Framework/TestCase.php`;
defined(`APPLICATION_PATH`)
        || define(`APPLICATION_PATH`, realpath(dirname(__FILE__) . `/../application`));
    // Define application environment
        defined(`APPLICATION_ENV`) || define(`APPLICATION_ENV`, `tests`);
        // Ensure library/ is on include_path
        set_include_path(implode(PATH_SEPARATOR,
                array(realpath(APPLICATION_PATH . `../../../library`), get_include_path())));
        // Zend_Application
        require_once `Zend/Application.php`;
        $application = new Zend_Application(
                APPLICATION_ENV,
                realpath(APPLICATION_PATH .`configs/application.ini`)
        );
class AuthenticationControllerTest extends PHPUnit_Framework_TestCase
{
     public function testLoginRetriespLogin() {
      $testmodel = new Model_testmodel_Object();//my model
    }   
}

但是当从终端"phpunit AuthenticationControllerTest"运行时,它会给我错误:

$ phpunit AuthenticationControllerTest

..FPHP 致命错误:在第 146 行的/var/www/versioned/pm160form/tests/application/controllers/AuthenticationControllerTest.php 中找不到类"Model_testmodel_Object"

看起来您的测试服文件夹未解析为实际的应用程序文件夹。看看你如何拥有

|| define(`APPLICATION_PATH`, realpath(dirname(__FILE__) . `/../application`));
但是您的目录

当前指向测试/应用程序/控制器目录,当然,该目录没有您的实际应用程序。 尝试将顶部根目录下的应用程序文件夹指向,

|| define(`APPLICATION_PATH`, realpath(dirname(__FILE__) . `/../../../application`));

另外,请注意遵循在 Zend Framework 中使用 PHP Unit 的标准,如 http://framework.zend.com/manual/1.12/en/zend.test.phpunit.html 中所述。框架需要完全引导,然后自动加载器才能工作。