我已经阅读了很多关于在 Zend Framework 中设置单元测试的帖子,但我什至无法运行一个简单的单元测试。问题在于设置和测试引导环境。我尝试了使用 ZFW 文档的最简单方法,但我总是收到此错误:
Zend_Config_Exception:parse_ini_file(/usr/local/zend/apache2/htdocs/APPBASE/tests/application.ini[function.parse-ini-file]:打开流失败:没有这样的文件或目录
这是 phpunit.xml:
<phpunit bootstrap="./application/bootstrap.php" colors="true">
<testsuite name="ApplicationTestSuite">
<directory>./application/</directory>
<directory>./library/</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../application</directory>
<directory suffix=".php">../application/library</directory>
<exclude>
<directory suffix=".phtml">../application/views</directory>
<file>../application/Bootstrap.php</file>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./log/coverage" charset="UTF-8"
yui="false" highlight="false" lowUpperBound="35" highLowerBound="70"/>
</logging>
</phpunit>
这是我的引导程序(tests/application/bootstrap.php):
<?php
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/library'),
get_include_path(),
)));
?>
我要测试的控制器(tests/application/controllers/AuthControllerTest.php):
<?php
require_once 'ControllerTestCase.php';
/**
* AuthController test case.
*/
class AuthControllerTest extends ControllerTestCase
{
/**
* @var AuthController
*/
private $AuthController;
/**
* Prepares the environment before running a test.
*/
public function setUp ()
{
parent::setUp();
// TODO Auto-generated AuthControllerTest::setUp()
$this->AuthController = new AuthController(/* parameters */);
}
/**
* Cleans up the environment after running a test.
*/
public function tearDown ()
{
// TODO Auto-generated AuthControllerTest::tearDown()
$this->AuthController = null;
parent::tearDown();
}
public function testCallWithoutActionShouldRedirectToLoginAction()
{
$this->dispatch('/auth');
$this->assertController('auth');
$this->assertAction('login');
}
}
和 ControllerTestCase.php(在 /test/application/controllers 中):
<?php
require_once 'Zend/Application.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
public $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'
);
$this->application->bootstrap();
}
public function tearDown()
{
Zend_Controller_Front::getInstance()->resetInstance();
$this->resetRequest();
$this->resetResponse();
$this->request->setPost(array());
$this->request->setQuery(array());
}
}
我的 application.ini (APPBASE/configs/application.ini):
[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 = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] = ""
resources.view.doctype = "XHTML1_STRICT"
phpSettings.date.timezone = 'America/Chicago';
[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
请注意,错误消息中的路径与我的引导程序中指定的路径不一致。我一度认为“$this->application->bootstrap();”这一行 可能正在执行我的常规应用程序的引导程序并更改应用程序路径,因此我将其注释掉,但无论如何我都有相同的错误。如果我在 Zend Studio 中“作为 PHP 单元测试运行”并注释掉,我会得到原始的 Zend Config 异常。如果我从命令行运行 phpunit,它在我的应用程序中找不到任何控制器。当我取消注释并从命令行运行时,我得到 Zend Config Exception。在 Zend Studio 中运行总是会导致 Zend Config 异常。
谁能提供一些关于为什么我无法正确设置应用程序路径的见解?