我有一個模塊的Zend Framework應用程序,我想設置PHPUnit測試。使用PHPUnit測試模塊化的Zend Framework應用程序
這是項目文件夾
- 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
所有測試在一個模塊內工作正常,但是當我開始測試模塊控制器時,我得到這個錯誤。 我在互聯網上搜索了所有內容,但找不到此錯誤的修復程序,所以我希望有人能幫助我。
這就解釋了爲什麼這不能正確工作。但是我對測試用例做了一些更改,所以我對/ mycore/actions/index進行了調度(通常應用程序應該重定向到/ user/login)。但是現在,如果我檢查模塊「mycore」控制器「操作」和操作「索引」,測試不會失敗。我什至沒有得到任何迴應(沒有或F或E)。我怎樣才能調試這個得到一個明確的錯誤信息?提前致謝! – acrobat
這意味着測試正在通過。您似乎沒有在您的phpunit.xml中設置任何日誌記錄。看看[這是如何設置它的例子](http://stackoverflow.com/a/10286326/212940)。 – vascowhite
難道你不會得到'。'當測試通過?索引控制器得到一個。這一個沒有得到任何迴應。好吧,我剛剛看到你的編輯,我會檢查。謝謝! – acrobat