在zend框架中运行控制器单元测试时,获得“没有针对此应用程序定义的默认模块”异常

前端之家收集整理的这篇文章主要介绍了在zend框架中运行控制器单元测试时,获得“没有针对此应用程序定义的默认模块”异常前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个具有默认目录结构的应用程序,对于没有自定义模块的应用程序(参见结尾图).

我已经按照许多教程中的说明编写了一个ControllerTestCase.PHP文件,并且我也创建了相应的引导文件(再次参见末尾的数字).

我编写了一些运行正常的模型测试,但是当我开始编写索引控制器测试文件时,只有一个测试,其中只有一行(“$this-> dispatch(‘/’)); “),运行PHPunit时,我得到以下异常(但浏览器浏览到相同的位置 – 一切都很好,工作):

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

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

附件:
目录结构:

-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

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

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');

    }
}
这是一个已知的错误.
以下是解决此问题的解决方法
<?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());
        }
    }
}

猜你在找的PHP相关文章