2012-05-03 79 views
2

我有一个模块的Zend Framework应用程序,我想设置PHPUnit测试。使用PHPUnit测试模块化的Zend Framework应用程序

这是项目文件夹

- project/ 
    -application/ 
     - controllers/ 
      - indexController.php 
     - modules/ 
     - mycore/ 
      - controllers/ 
       - ActionsController.php 
      - views/ 
       - ... 
    - ... 
    - tests/ 
     - application/ 
     - controllers/ 
      -IndexControllerTest.php 
     - modules/ 
      - mycore/ 
       - controllers/ 
        - ActionsControllerTest.php 
     ControllerTestCase.php 
     Bootstrap.php 
     phpunit.xml 

这是每一个安装文件在测试文件夹内容

ControllerTestCase.php

require_once 'Zend/Application.php'; 
require_once 'Zend/Auth.php'; 
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php'; 

class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase { 
    protected $application 

    public function setUp() { 
     $this->bootstrap = array($this, 'appBootstrap'); 
     parent::setUp(); 
    } 

    public function appBootstrap() { 
     $this->application = new Zend_Application(
      APPLICATION_ENV, 
      APPLICATION_PATH . '/configs/application.ini'); 
     $bootstrap = $this->application->getBootstrap()->bootstrap(); 

     $front = Zend_Controller_Front::getInstance(); 
     $front->setControllerDirectory(APPLICATION_PATH . '/controllers','default'); 
     $front->addModuleDirectory(APPLICATION_PATH . '/modules'); 

     return $bootstrap; 
    } 

    public function tearDown() { 
     Zend_Auth::getInstance()->clearIdentity(); 
     $this->resetRequest(); 
     $this->resetResponse(); 
     parent::tearDown(); 
    } 

    protected function _doLogin($identity = null) { 
     if ($identity === null) { 
      $identity = $this->_generateFakeIdentity(); 
     } 
     Zend_Auth::getInstance()->getStorage()->write($identity); 
    } 

    protected function _generateFakeIdentity() { 
     $identity = new stdClass(); 
     $identity->RecID      = 3; 
     $identity->user_firstname   = '****'; 
     $identity->user_lastname    = '********'; 
     $identity->confirmed     = true; 
     $identity->enabled     = true; 

     return $identity; 
    } 

    protected function _doLogout() { 
     Zend_Auth::getInstance()->clearIdentity(); 
    } 
} 

bootstrap.php中

error_reporting(E_ALL); 
// 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') : 'development')); 

// 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'; 

phpunit.xml

<phpunit bootstrap="./bootstrap.php" colors="true" 
convertErrorsToExceptions="true" 
convertNoticesToExceptions="true" 
convertWarningsToExceptions="true" > 
    <testsuite name="Application Test Suite"> 
     <directory>./</directory> 
    </testsuite> 
    <testsuite name="Library Test Suite"> 
     <directory>./library</directory> 
    </testsuite> 

    <filter> 
     <!-- If Zend Framework is inside your project's library, uncomment this filter --> 
     <!-- 
     <whitelist> 
      <directory suffix=".php">../../library/Zend</directory> 
     </whitelist> 
     --> 
    </filter> 
</phpunit> 

这是模块测试

的内容ActionsControllerTest.php

class Mycore_ActionsControllerTest extends Zend_Test_PHPUnit_ControllerTestCase 
{ 

    public $module; 

    public function setUp() 
    { 
     $this->module = 'mycore'; 
     $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini'); 
     $_SERVER['HTTP_HOST'] = 'unittest_host'; 
     $_SERVER['REQUEST_URI'] = '/'; 
     parent::setUp(); 
    } 

    public function testIndexAction() 
    { 
     $this->dispatch("/"); 
     // assertions 
     $this->assertModule('mycore'); 
     $this->assertController('actions'); 
     $this->assertAction('index');   
    } 


} 

这是结果:

Starting test 'IndexControllerTest::testIndexAction'. 
. 
Starting test 'Mycore_ActionsControllerTest::testIndexAction'. 
F 

Time: 1 second, Memory: 14.00Mb 

There was 1 failure: 

1) Mycore_ActionsControllerTest::testIndexAction 
Failed asserting last module used <"default"> was "mycore" 

/project/library/Zend/Test/PHPUnit/ControllerTestCase.php:929 
/project/tests/application/modules/mycore/controllers/ActionsControllerTest.php:21 

所有测试在一个模块内工作正常,但是当我开始测试模块控制器时,我得到这个错误。 我在互联网上搜索了所有内容,但找不到此错误的修复程序,所以我希望有人能帮助我。

回答

3

你得到的不是错误,它是一个失败的测试。

这可能是因为您正在调度到'/',这会在默认模块的默认控制器中查找默认操作。如果您发送到'/ mycore'或'/ mycore/actions/index',您可能会发现测试通过。

为了让您的测试通过而不改变它,您将需要change your default route指向'/ mycore/actions/index'。

+0

这就解释了为什么这不能正确工作。但是我对测试用例做了一些更改,所以我对/ mycore/actions/index进行了调度(通常应用程序应该重定向到/ user/login)。但是现在,如果我检查模块“mycore”控制器“操作”和操作“索引”,测试不会失败。我什至没有得到任何回应(没有或F或E)。我怎样才能调试这个得到一个明确的错误信息?提前致谢! – acrobat

+0

这意味着测试正在通过。您似乎没有在您的phpunit.xml中设置任何日志记录。看看[这是如何设置它的例子](http://stackoverflow.com/a/10286326/212940)。 – vascowhite

+0

难道你不会得到'。'当测试通过?索引控制器得到一个。这一个没有得到任何回应。好吧,我刚刚看到你的编辑,我会检查。谢谢! – acrobat

0

我有同样的问题。 对我来说,这是因为请求被重定向到'ErrorController'。 你可以通过评论你的assertModule行来检查。 如果你有这样的错误'失败断言最后一个控制器使用<“错误”>是“行动”'我们是在同一个案例。 这种错误可能有各种原因。 你得赶上错误。 调度后,你可以检索像这样的例外:

$response = $this->getResponse(); 
$exceptions = $response->getException(); 
// do whatever you want, mail, log, print 
相关问题