Zend_Test_PHPUnit_ControllerTestCase 和 Zend_Layout,运行测试时无法找到布局插件

发布于 2024-09-13 15:00:07 字数 1745 浏览 8 评论 0原文

我开始使用 Zend Framework 1.10.6 和 Zend_Test_PHPUnit_ControllerTestCase 为控制器类编写一些测试用例。我遇到了一项问题,即在测试用例运行时,Zend_Controller_Action_HelperBroker 找不到布局操作助手。

以下是我的测试用例的基本内容:

require_once 'PHPUnit/Framework.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
require_once 'controllers/IndexController.php';

class Application_Controllers_IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase {

    public $_application;

    protected function setUp() {
        $this->bootstrap = array($this, 'appBootstrap');
        parent::setUp ();
    }

    public function appBootstrap() {
        // Create application, bootstrap, but don't run
        $this->_application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
        );
        $this->_application->bootstrap();
        $this->getFrontController()->setParams($this->_application->getOptions())
             ->addControllerDirectory(APPLICATION_PATH . '/controllers');
    }

    public function testIndexAction() {
        $this->dispatch('/index/index');
        $this->assertController('index');
        $this->assertAction('index');
    }

}

运行测试用例时出现异常:

Zend_Controller_Action_Exception: Action Helper by name Layout not found

当我注释掉类 Zend_Controller_Action_HelperBroker 中的两行以尝试找到该行周围的源时368,我得到:

Zend_Loader_PluginLoader_Exception:在注册表中找不到名为“Layout”的插件;使用的路径: Zend_Controller_Action_Helper_: Zend/Controller/Action/Helper/

布局脚本的加载在我的应用程序中运行时工作正常,在 PHPUnit 下运行测试时似乎无法找到 Zend_Controller_Action_Helper 的正确路径或注册表,因此布局插件可以不被加载。

我已经验证 Zend 安装正确并且 Layout.php 位于正确的位置。

有什么想法吗?

德尔

I am starting to write some test cases for controller classes using Zend Framework 1.10.6 and Zend_Test_PHPUnit_ControllerTestCase. I am having problems with one item, which is that while the test cases are running, Zend_Controller_Action_HelperBroker can't find the Layout action helper.

Here are the bare bones of my test case:

require_once 'PHPUnit/Framework.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
require_once 'controllers/IndexController.php';

class Application_Controllers_IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase {

    public $_application;

    protected function setUp() {
        $this->bootstrap = array($this, 'appBootstrap');
        parent::setUp ();
    }

    public function appBootstrap() {
        // Create application, bootstrap, but don't run
        $this->_application = new Zend_Application(
            APPLICATION_ENV,
            APPLICATION_PATH . '/configs/application.ini'
        );
        $this->_application->bootstrap();
        $this->getFrontController()->setParams($this->_application->getOptions())
             ->addControllerDirectory(APPLICATION_PATH . '/controllers');
    }

    public function testIndexAction() {
        $this->dispatch('/index/index');
        $this->assertController('index');
        $this->assertAction('index');
    }

}

I get an exception when I run the test case:

Zend_Controller_Action_Exception: Action Helper by name Layout not found

When I comment out the two lines in class Zend_Controller_Action_HelperBroker to try to find the source of this around line 368, I get:

Zend_Loader_PluginLoader_Exception: Plugin by name 'Layout' was not found in the registry; used paths:
Zend_Controller_Action_Helper_: Zend/Controller/Action/Helper/

The loading of layout scripts works fine in my application when running, it appears that the correct path or registry for the Zend_Controller_Action_Helper can't be found while running tests under PHPUnit and therefore the Layout plugin can't be loaded.

I have verified that Zend is installed correctly and that Layout.php is in the correct place.

Any ideas?

Del

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半世晨晓 2024-09-20 15:00:07

appBootstrap() 的末尾放置以下行:

    Zend_Controller_Action_HelperBroker::addHelper(new Zend_Layout_Controller_Action_Helper_Layout);

In you appBootstrap() at the end place this line:

    Zend_Controller_Action_HelperBroker::addHelper(new Zend_Layout_Controller_Action_Helper_Layout);
从来不烧饼 2024-09-20 15:00:07

您在什么时候添加布局代码?

请记住,运行 PHPUnit 测试时,“引导”是不同的,并且在主应用程序中引导的内容在作为 PHPUnit 测试运行时可能不会。

At which point do you add your Layout code?

Remember that the 'boostraping' is different when running a PHPUnit test, and that things that are bootstrapped in your main app might not be when running as a PHPUnit test.

稳稳的幸福 2024-09-20 15:00:07

我的解决方法:

function someAction() {
    // workaround for unit tests 'Action Helper by name Layout not found'
    if ($this->_helper->hasHelper('layout')) {
        $this->_helper->layout->disableLayout(); // disable layouts
    }
    ...

My workaround:

function someAction() {
    // workaround for unit tests 'Action Helper by name Layout not found'
    if ($this->_helper->hasHelper('layout')) {
        $this->_helper->layout->disableLayout(); // disable layouts
    }
    ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文