Exception 得到一个;没有为此应用程序定义默认模块“;在zend framework中运行控制器单元测试时发生异常
我有一个默认目录结构的应用程序,用于没有自定义模块的应用程序(请参见最后的结构图) 我已经按照许多教程中的说明编写了ControllerTestCase.php文件,并且还创建了相应的引导文件(再次,请参见末尾的图) 我已经编写了一些运行正常的模型测试,但是当我开始编写索引控制器测试文件时,只有一个测试,其中只有一行($this->dispatch('/');”),在运行phpunit时,我遇到了以下异常(但是使用浏览器导航到同一位置-一切都很好并且正常): 为什么会这样?我做错了什么 附录:Exception 得到一个;没有为此应用程序定义默认模块“;在zend framework中运行控制器单元测试时发生异常,exception,zend-framework,phpunit,Exception,Zend Framework,Phpunit,我有一个默认目录结构的应用程序,用于没有自定义模块的应用程序(请参见最后的结构图) 我已经按照许多教程中的说明编写了ControllerTestCase.php文件,并且还创建了相应的引导文件(再次,请参见末尾的图) 我已经编写了一些运行正常的模型测试,但是当我开始编写索引控制器测试文件时,只有一个测试,其中只有一行($this->dispatch('/');”),在运行phpunit时,我遇到了以下异常(但是使用浏览器导航到同一位置-一切都很好并且正常): 为什么会这样?我做错了什么 附录:
目录结构:
-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
[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
<?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';
<?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');
}
}
(基本上,我在models目录中还有一些文件,但这与这个问题无关。)
application.ini文件:
-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
[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
<?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';
<?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');
}
}
测试/application/bootstrap.php文件:
-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
[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
<?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';
<?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());
}
}
}
我的解决方案是在parent::setUp()之后加上这一行
事实上,我使用的是zend framework 1.10.2,所以你所说的这个bug根本不存在,但是-你确实让我找到了解决方案,那就是我在appBootstrap方法中缺少$this->\u application->bootstrap()调用。请编辑您的答案以删除对错误的引用,因为事实并非如此。谢谢,我现在是1.10.6,但我似乎仍然经历过这个错误。。。不知道我是否做错了什么。如果我删除该修复程序,它将被删除fails@jiewmeng请看Matthew描述的设置: