我有一个带模块的Zend Framework应用程序,我想设置PHPUnit测试。
这是项目文件夹
- project/
-application/
- controllers/
- indexController.php
- modules/
- mycore/
- controllers/
- ActionsController.php
- views/
- ...
- ...
- tests/
- application/
- controllers/
-IndexControllerTest.php
- modules/
- mycore/
- controllers/
- ActionsControllerTest.php
ControllerTestCase.php
Bootstrap.php
phpunit.xml
这是测试文件夹中每个安装文件的内容
ControllerTestCase.php
require_once 'Zend/Application.php';
require_once 'Zend/Auth.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase {
protected $application
public function setUp() {
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap() {
$this->application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini');
$bootstrap = $this->application->getBootstrap()->bootstrap();
$front = Zend_Controller_Front::getInstance();
$front->setControllerDirectory(APPLICATION_PATH . '/controllers','default');
$front->addModuleDirectory(APPLICATION_PATH . '/modules');
return $bootstrap;
}
public function tearDown() {
Zend_Auth::getInstance()->clearIdentity();
$this->resetRequest();
$this->resetResponse();
parent::tearDown();
}
protected function _doLogin($identity = null) {
if ( $identity === null ) {
$identity = $this->_generateFakeIdentity();
}
Zend_Auth::getInstance()->getStorage()->write( $identity );
}
protected function _generateFakeIdentity() {
$identity = new stdClass();
$identity->RecID = 3;
$identity->user_firstname = '****';
$identity->user_lastname = '********';
$identity->confirmed = true;
$identity->enabled = true;
return $identity;
}
protected function _doLogout() {
Zend_Auth::getInstance()->clearIdentity();
}
}
bootstrap.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') : 'development'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
require_once 'Zend/Loader/Autoloader.php';
phpunit.xml
<phpunit bootstrap="./bootstrap.php" colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true" >
<testsuite name="Application Test Suite">
<directory>./</directory>
</testsuite>
<testsuite name="Library Test Suite">
<directory>./library</directory>
</testsuite>
<filter>
<!-- If Zend Framework is inside your project's library, uncomment this filter -->
<!--
<whitelist>
<directory suffix=".php">../../library/Zend</directory>
</whitelist>
-->
</filter>
</phpunit>
这是模块测试的内容
ActionsControllerTest.php
class Mycore_ActionsControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
public $module;
public function setUp()
{
$this->module = 'mycore';
$this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
$_SERVER['HTTP_HOST'] = 'unittest_host';
$_SERVER['REQUEST_URI'] = '/';
parent::setUp();
}
public function testIndexAction()
{
$this->dispatch("/");
// assertions
$this->assertModule('mycore');
$this->assertController('actions');
$this->assertAction('index');
}
}
这就是结果:
Starting test 'IndexControllerTest::testIndexAction'.
.
Starting test 'Mycore_ActionsControllerTest::testIndexAction'.
F
Time: 1 second, Memory: 14.00Mb
There was 1 failure:
1) Mycore_ActionsControllerTest::testIndexAction
Failed asserting last module used <"default"> was "mycore"
/project/library/Zend/Test/PHPUnit/ControllerTestCase.php:929
/project/tests/application/modules/mycore/controllers/ActionsControllerTest.php:21
模块中的所有测试都工作正常但是当我开始测试模块控制器时,我得到了这个错误。 我在互联网上搜索过但找不到修复此错误,所以我希望有人可以帮助我。
答案 0 :(得分:3)
你得到的不是错误,这是一个失败的测试。
这可能正在发生,因为您正在调度'/',它会在默认模块的默认控制器中查找默认操作。如果您发送到'/ mycore'或'/ mycore / actions / index',您可能会找到测试通行证。
为了让您的测试无需更改即可通过,您需要change your default route指向'/ mycore / actions / index'。
答案 1 :(得分:0)
我有同样的问题。 对我来说,这是因为请求被重定向到'ErrorController'。 您可以通过注释assertModule行来检查这一点。 如果您遇到类似这样的错误'使用最后一个控制器失败&lt;“错误”&gt;是“行动”,我们处于相同的情况。 这种错误可能有多种原因。 你必须抓住这个错误。 在发送之后,您可以检索这样的例外:
$response = $this->getResponse();
$exceptions = $response->getException();
// do whatever you want, mail, log, print