0

我打电话

$this->application->bootstrap()->run(); 

从我的测试设置功能内部,没有输出。你应该run()在测试环境中调用吗?

我的测试有效,但前端控制器插件从未执行(这对我的应用程序来说是必需的)。Putting->run()执行插件,但 phpunit 只是停止并且没有输出。

我有工作测试,但我需要前端控制器插件才能在测试环境中执行。Bootstrap 执行,但插件不执行。当他们通过->run()时,没有输出

有什么建议么?

编辑:添加setup()示例

require_once realpath(__DIR__.'/../../').'/TestBackendConfiguration.php';

abstract class Controllers_Backend_BaseControllerTest extends Zend_Test_PHPUnit_ControllerTestCase {

    protected $config;
    protected $application;
    protected $users;
    protected static $iterations = 0;

    public function setUp() {
        $this->bootstrap = array($this, 'appBootstrap');
        parent::setUp();
        $this->configureUsers();
        $this->configureACL($this->users);
    }

    public function appBootstrap() {
        $this->config = new Zend_Config_Ini(CORE_PATH . '/configs/common.ini', APPLICATION_ENV, true);
        $this->config->merge(new Zend_Config_Ini(CORE_PATH . '/configs/backend.ini', APPLICATION_ENV));
        $this->config->merge(new Zend_Config_Ini(CORE_PATH . '/configs/application.ini', APPLICATION_ENV));

        // Reset bootstraps
        $this->config->bootstrap->path = CORE_PATH . '/backend/Bootstrap.php';
        $this->config->resources->frontController->controllerDirectory = CORE_PATH . '/backend/controllers';
        $this->config->resources->frontController->actionHelperPaths->Frontend_Controller_Action_Helper = CORE_PATH . '/backend/controllers/Action/Helper';
        $this->config->resources->frontController->baseUrl = "/admin";
        $this->config->resources->layout->layoutPath = CORE_PATH . '/backend/layouts/scripts';


        $this->application = new Zend_Application( APPLICATION_ENV, $this->config );

        $this->application->bootstrap()->run(); 
     }
}

调整的原因$this->config是因为我有前端和后端相关的测试,它们需要不同的配置。他们有自己的Configuration.php脚本,它们都声明APPLICATION_PATH,因此第二次运行的测试无法重新定义此 const,并且应用程序无法正确引导。

现在,我主要担心的是我的插件没有在测试环境中调度,这会破坏我的应用程序,因为我有数据添加到我需要访问的注册表中,但显然不存在。想法?

添加->run()调度插件,但我的测试没有输出。我的后端控制器测试都扩展了这个负责设置后端测试环境的类。

4

2 回答 2

1

我最近也遇到类似的问题,终于找到了解决办法。我希望它对某人有用。

测试环境中资源的问题是 bootstrap 对象在方法中被分配为 FrontController 参数Zend_Application_Bootstrap_Bootstrap::run(),但此方法从未在测试环境中执行。这就是通过引导程序获取任何资源时出错的原因(我假设您Zend_Controller_Front::getInstance()->getParam('bootstrap')在代码中的某处使用)。

帮助我的解决方法是在单元测试中手动将引导参数设置为 FrontController,如下所示:

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');
    $this->application->bootstrap();
    // set bootstrap param
    $bootstrap = $this->application->getBootstrap();
    $front = $bootstrap->getResource('FrontController');
    $front->setParam('bootstrap', $bootstrap);
}
于 2014-06-23T15:24:22.383 回答
0

setup 方法实际上不应引导应用程序,只需创建类以允许测试执行此操作。所以我认为你需要做的就是删除这一行:

$this->application->bootstrap()->run(); 

为了安全起见,我还将parent::setUp();在您的设置方法中移动该行,以便在函数结束时调用它(在您的两个配置方法之后)。如果这些方法依赖于那里的引导程序,则将它们移到appBootstrap函数中。

于 2013-03-03T12:56:37.137 回答