我有一个带有模块的 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();
}
}
引导程序.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
一个模块中的所有测试都工作正常,但是当我开始测试模块控制器时,我得到了这个错误。我在整个互联网上搜索但找不到解决此错误的方法,所以我希望有人能帮助我。