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

tub*_*035 2 php zend-framework

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

我正试图让单元测试工作,并偶然发现了一个我无法从Company_Controller_Action类中获取引导程序的问题(扩展Zend_Controller_Action):

function preDispatch() 
{
  $bootstrap = $this->getInvokeArg('bootstrap');
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

我的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();
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

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

Kar*_*rma 5

这是一个错误,如果我记得几个星期前我在Zend bug跟踪器中看到的.

以下是对许多人有用的修复:

在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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我为这个问题做了一个快速的谷歌.我得到了一个类似的解决方法(几乎与我上面提到的相同),而且stackoverflow上的这个答案可能对你有所帮助,根据这个答案它可以通过动作帮助器来调用.