2

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

这是项目文件夹

- 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();
    }
}

引导程序.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

一个模块中的所有测试都工作正常,但是当我开始测试模块控制器时,我得到了这个错误。我在整个互联网上搜索但找不到解决此错误的方法,所以我希望有人能帮助我。

4

2 回答 2

3

您得到的不是错误,而是失败的测试。

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

为了让您的测试在不更改的情况下通过,您需要将默认路由更改为指向“/mycore/actions/index”。

于 2012-05-03T08:50:04.913 回答
0

我遇到过同样的问题。对我来说,这是因为请求被重定向到“ErrorController”。您可以通过评论您的 assertModule 行来检查这一点。如果您遇到类似这样的错误“断言使用的最后一个控制器失败 <"error"> 是 "actions"',我们处于相同的情况。这种错误可能有多种原因。你必须抓住错误。调度后,您可以像这样检索异常:

$response = $this->getResponse();
$exceptions = $response->getException();
// do whatever you want, mail, log, print
于 2013-06-20T10:34:28.057 回答