php - 无法在 Controller 操作中获取 Bootstrap

标签 php zend-framework

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

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

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

$bootstrap 为空。有人知道为什么吗?我已经验证我的 Bootstrap 正在被调用,并且我可以获取前端 Controller ,但我无法获取 Bootstrap (通过 getInvokeArg 或前端 Controller )。

这适用于正常的生产和开发环境。我的 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>

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

最佳答案

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

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

在 Bootstrap.php 中初始化前端 Controller :

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

编辑:

我快速谷歌了一下这个问题。我有一个类似的workaround (几乎与我上面提到的相同),根据 this answer,stackoverflow 上的这个答案可能会对您有所帮助。可以通过操作助手调用它。

关于php - 无法在 Controller 操作中获取 Bootstrap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12063337/

相关文章:

php - 多项选择 PHP

php - 如何在数据库中写入字符

PHP:跳到 strpos() 给出的位置

zend-framework - 如何在 Zend Framework 1.11.x 中的 application.ini 中注册自定义应用程序资源

php - 尝试获取ErrorController.php的非对象属性

php - (PHP) : Testing models with Zend_Test_PHPUnit_DatabaseTestCase

php - Zendframework Rowset 按键选择

php - 如何在 php 中访问私有(private)范围的命名空间数组数据?

PHP 匹配至少两种流派 (mysql)

php - 如何最好地在 Zend Framework 中添加此 Action Helper 路径