获取“没有为此应用程序定义的默认模块"在 zend 框架中运行控制器单元测试时出现异常 [英] Getting a "No default module defined for this application" exception while running controller unit tests in zend framework

查看:27
本文介绍了获取“没有为此应用程序定义的默认模块"在 zend 框架中运行控制器单元测试时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有默认目录结构的应用程序,用于没有自定义模块的应用程序(参见最后的结构图).

I have an application with the default directory structure, for an application without custom modules (see structure figure at the end).

我已经按照许多教程中的说明编写了一个 ControllerTestCase.php 文件,并且我还创建了适当的引导文件(再次,请参见最后的图).

I have written a ControllerTestCase.php file as instructed in many tutorials, and I've created the appropriate bootstrap file as well (once again, see figures at the end).

我已经编写了一些运行良好的模型测试,但是当我开始编写索引控制器测试文件时,其中只有一个测试,其中只有一行 ("$this->dispatch('/');"),运行 phpunit 时出现以下异常(但使用浏览器导航到同一位置 - 一切正常且正常工作):

I've written some model tests which run just fine, but when I started writing the index controller test file, with only one test in it, with only one line in it ("$this->dispatch('/');"), I'm getting the following exception when running phpunit (but navigating with the browser to the same location - all is good and working):

1) IndexControllerTest::test_indexAction_noParams
Zend_Controller_Exception: No default module defined for this application

这是为什么?我做错了什么?

Why is this ? What have I done wrong ?

附录:
目录结构:

-proj/
  -application/
    -controllers/
      -IndexController.php
      -ErrorController.php
    -config/
    -layouts/
    -models/
    -views/
      -helpers/
      -scripts/
        -error/
          -error.phtml
        -index/
          -index.phtml
    -Bootstrap.php
  -library/
  -tests/
    -application/
      -controllers/
        -IndexControllerTest.php
      -models/
      -bootstrap.php
      -ControllerTestCase.php
    -library/
    -phpunit.xml
  -public/
    -index.php

(基本上我在模型目录中还有一些文件,但这与这个问题无关.)

(Basically I have some more files in the models directory, but that's not relevant to this question.)

application.ini 文件:

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "My"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] = 
phpSettings.date.timezone = "America/Los_Angeles"

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

tests/application/bootstrap.php 文件:

<?php 
error_reporting(E_ALL);
// 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'), // this project' library
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

require_once 'ControllerTestCase.php';

ControllerTestCase.php 文件:

<?php
require_once ('Zend/Test/PHPUnit/ControllerTestCase.php');
class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {
    /**
     * 
     * @var Zend_Application
     */
    protected $application;

    protected 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 is a known bug.
Here is my workaround to fix this issue:

<?php
require_once 'Zend/Application.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';

abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    protected $_application;

    protected 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();

        /**
         * Fix for ZF-8193
         * http://framework.zend.com/issues/browse/ZF-8193
         * Zend_Controller_Action->getInvokeArg('bootstrap') doesn't work
         * under the unit testing environment.
         */
        $front = Zend_Controller_Front::getInstance();
        if($front->getParam('bootstrap') === null) {
            $front->setParam('bootstrap', $this->_application->getBootstrap());
        }
    }
}

这篇关于获取“没有为此应用程序定义的默认模块"在 zend 框架中运行控制器单元测试时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆