无法在控制器操作中获取引导程序


Unable to get bootstrap in controller action

我有一个用于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();
}

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

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

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

以下是对许多人有效的修复方法:

初始化引导程序中的前控制器。hp:

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上的这个答案可能会对你有所帮助,根据这个答案,它可以通过动作助手调用。