2

我有一个用于 Zend Framework 的库。在其中,我拥有Company/Controller/Action.php所有应用程序控制器的扩展。它非常简单,只是将一些东西注入控制器(如 Doctrine 的实体管理器)。

我正在尝试使单元测试正常工作,但偶然发现了一个问题,即我无法从Company_Controller_Action类(扩展Zend_Controller_Action)中获取引导程序:

function preDispatch() 
{
  $bootstrap = $this->getInvokeArg('bootstrap');
}

$bootstrap此时为空。任何人有任何想法为什么?我已经验证我的引导程序正在被调用并且我可以获取前端控制器,但我无法获取引导程序(通过 getInvokeArg 或前端控制器)。

这适用于正常的生产和开发环境。我的 application.ini 的测试部分只是继承自生产部分,所以应该是一样的。

这就是我正在做的事情setUp()

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

我的 Bootstrap.php:

<?php

// 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') : 'testing'));

// 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';
Zend_Loader_Autoloader::getInstance();

我的 phpunit.xml:

<phpunit bootstrap="./Bootstrap.php" colors="true">
    <testsuite name="Application Test Suite">
        <directory>./application</directory>
    </testsuite>
    <filter>
        <whitelist>
            <directory suffix=".php">../application/</directory>
            <exclude>
                <directory suffix=".php">../application/Entities</directory>
                <directory suffix=".php">../application/modules/default/views</directory>
                <file>../application/Bootstrap.php</file>
                <file>../application/modules/default/controllers/ErrorController.php</file>
            </exclude>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="./log/report" title="PrintConcept" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70" />
        <log type="testdox" target="./log/testdox.html" />
    </logging>
</phpunit>

更新:使用上述配置, My$bootstrap不再为空。

4

1 回答 1

5

如果我记得几周前我在 Zend 错误跟踪器中看到的,这是一个错误。

这是适用于许多人的修复程序:

在 Bootstrap.php 中初始化 Front 控制器:

protected function _initFrontController()
{

    $frontControllerInstance = Zend_Controller_Front::getInstance();

    //If bootstrap is NULL or set to null then, set it.
    if(is_null($frontControllerInstance->getParam('bootstrap'))) {
        $frontControllerInstance->setParam('bootstrap', $this);
    }
    return $frontControllerInstance;
}

编辑:

我为这个问题做了一个快速的谷歌。我得到了一个类似的解决方法(几乎和我上面提到的一样),stackoverflow 上的这个答案也可能对你有所帮助,根据这个答案,它可以通过动作助手调用。

于 2012-08-22T03:56:11.957 回答