Zend Framework 测试控制器 Zend_Controller_Exception:没有为此应用程序定义默认模块
我尝试为控制器编写测试。我使用 Windows 操作系统,zend 框架,我的库位于 C:/library 中,它被添加到 php.ini 的 include_path 中。 当我运行测试 testLoginAction
时,出现错误:没有为应用程序定义默认模块。但我根本不使用模块。你知道如何解决这个问题吗?
IndexControllerTest.php
class Controller_IndexControllerTest extends ControllerTestCase
{
public function testIsEverythingOK()
{
$this->assertTrue(true);
}
public function testLoginAction()
{
$this->dispatch('/login/index');
$this->assertModule('default');
$this->assertController('login');
$this->assertAction('index');
}
}
ControllerTestCase.php
require_once 'Zend/Application.php';
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
protected $_application;
public function setUp()
{
$this->bootstap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
// require dirname(__FILE__) . '/bootstrap.php';
$this->front->setControllerDirectory('../application/controllers');
$this->_application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini');
$this->_application->bootstrap();
}
}
我的 phpunit.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit bootstrap="./application/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true"
syntaxCheck="true">
<!-- запускаем все тесты из корневой директории -->
<testsuite name="Main Test Suite">
<directory>./</directory>
</testsuite>
<filter> <!-- смотрим лишь на следующие директории -->
<whitelist>
<directory suffix=".php">../application</directory>
<!-- <directory suffix=".php">../library</directory>-->
<exclude>
<directory suffix=".phtml">../application</directory>
<directory>../application/forms</directory>
<directory>../application/models</directory>
<directory>../library</directory>
<file>../application/Bootstrap.php</file>
</exclude>
</whitelist>
</filter>
<logging>
<!-- логирование и создание отчета -->
<log type="coverage-html" target="./report" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70"/>
</logging>
</phpunit>
测试/应用程序中的我的 bootstrap.php:
// 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'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
require_once 'ControllerTestCase.php';
我的测试基类:
require_once 'Zend/Application.php';
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
protected $_application;
public function setUp()
{
$this->bootstap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
// require dirname(__FILE__) . '/bootstrap.php';
$this->front->setControllerDirectory('../application/controllers');
$this->_application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini');
$this->_application->bootstrap();
}
}
最好的问候,Oleg。
I try to write test for controller. I use OS Windows, zend framework and my libraries are in C:/library
which is added to the include_path of php.ini.
When I run test testLoginAction
I get an error No default module define for the application. But I don't use modules at all. Do you know how to solve this problem?
IndexControllerTest.php
class Controller_IndexControllerTest extends ControllerTestCase
{
public function testIsEverythingOK()
{
$this->assertTrue(true);
}
public function testLoginAction()
{
$this->dispatch('/login/index');
$this->assertModule('default');
$this->assertController('login');
$this->assertAction('index');
}
}
ControllerTestCase.php
require_once 'Zend/Application.php';
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
protected $_application;
public function setUp()
{
$this->bootstap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
// require dirname(__FILE__) . '/bootstrap.php';
$this->front->setControllerDirectory('../application/controllers');
$this->_application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini');
$this->_application->bootstrap();
}
}
My phpunit.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<phpunit bootstrap="./application/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true"
syntaxCheck="true">
<!-- запускаем все тесты из корневой директории -->
<testsuite name="Main Test Suite">
<directory>./</directory>
</testsuite>
<filter> <!-- смотрим лишь на следующие директории -->
<whitelist>
<directory suffix=".php">../application</directory>
<!-- <directory suffix=".php">../library</directory>-->
<exclude>
<directory suffix=".phtml">../application</directory>
<directory>../application/forms</directory>
<directory>../application/models</directory>
<directory>../library</directory>
<file>../application/Bootstrap.php</file>
</exclude>
</whitelist>
</filter>
<logging>
<!-- логирование и создание отчета -->
<log type="coverage-html" target="./report" charset="UTF-8" yui="true" highlight="true" lowUpperBound="35" highLowerBound="70"/>
</logging>
</phpunit>
My bootstrap.php in tests/application:
// 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'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
require_once 'ControllerTestCase.php';
My Base class for tests:
require_once 'Zend/Application.php';
require_once 'Zend/Controller/Action.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
protected $_application;
public function setUp()
{
$this->bootstap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
// require dirname(__FILE__) . '/bootstrap.php';
$this->front->setControllerDirectory('../application/controllers');
$this->_application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini');
$this->_application->bootstrap();
}
}
Best regards, Oleg.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保您的 application.ini 不包含诸如
resources.frontController.moduledirectory = APPLICATION_PATH "/modules"
或resources.frontController.params.prefixDefaultModule = 1
或之类的配置resources.modules[] =
Make sure your application.ini does not contain configuration like
resources.frontController.moduledirectory = APPLICATION_PATH "/modules"
orresources.frontController.params.prefixDefaultModule = 1
orresources.modules[] =
您需要在 Parent::setUp() 行之后修改 ControllerTestCase 类中的 setUp() 方法:
$this->getFrontController()->setControllerDirectory(APPLICATION_PATH . '/controllers');
并从 appBoostrap 方法中删除以下行:
$this->front->setControllerDirectory('../application/controllers');
You need to modify your setUp() method from the ControllerTestCase class, after parent::setUp() line:
$this->getFrontController()->setControllerDirectory(APPLICATION_PATH . '/controllers');
and remove the following line from your appBoostrap method:
$this->front->setControllerDirectory('../application/controllers');
$this->front->
不起作用,因为前端控制器属性是$this->frontController->
或$this-> ;getFrontController()->
。即便如此,由于您的引导方式,您的测试可能无论如何都不会起作用。您是否从 ini 配置文件配置前端控制器?如果是,那么您不需要在测试中配置 frontController,您需要通过
Zend_Application
来配置引导程序。您不需要在
Zend_Application
实例上调用bootstrap()
,因为Zend_Test_PHPUnit_ControllerTestCase
会在您调度时执行此操作一个请求。因此,您的基类可能如下所示:
扩展基类的控制器测试将如下所示:
完成。
资源
引导您的测试用例
$this->front->
won't work because the front controller property is$this->frontController->
or$this->getFrontController()->
.Even so, your tests probably won't work anyways because of the way you're bootstrapping. Are you configuring your front controller from an ini configuration file? If yes, then you don't need to configure the frontController in your tests, you need to configure your bootstrap by way of
Zend_Application
.You won't need to call
bootstrap()
on yourZend_Application
instance becauseZend_Test_PHPUnit_ControllerTestCase
will do that when you dispatch a request.So your base class might loook like this:
And your controller test, extending your base class, will look like this:
Done.
Resources
Bootstraping your TestCase