我正在尝试使用Zend Test和我的Zend Framework应用程序使用PHPUnit。我可以从命令行phpunit --configuration phpunit.xml
运行PHPUnit命令。我试过跟随基于Matthew Weier O'Phinney this tutorial的blog post。当PHPUnit尝试写日志文件时,我收到错误。这是我的phpunit.xml
<phpunit bootstrap="./Bootstrap.php" colors="true">
<testsuite name="Zend Framework Tests">
<directory>./</directory>
</testsuite>
<!-- Optional filtering and logging settings -->
<filter>
<whitelist>
<directory suffix=".php">../library/</directory>
<directory suffix=".php">../application/</directory>
<exclude>
<directory suffix=".phtml">../application/</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./log/report" charset="UTF-8" yui="true" highlight="true" lowUpperBound="50" highLowerBound="80"/>
<log type="testdox-html" target="./log/testdox.html"/>
</logging>
</phpunit>
我的测试引导程序:
<?php
//Set app paths and environment
define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
define('APPLICATION_PATH', BASE_PATH . '/application');
define('TEST_PATH', BASE_PATH . '/tests');
define('APPLICATION_ENV', 'testing');
//Set include path
set_include_path('.' . PATH_SEPARATOR . BASE_PATH . '/library' . PATH_SEPARATOR . get_include_path());
//Set the default timezone
date_default_timezone_set('America/Chicago');
?>
我的ControllerTestCase,我希望我的测试控制器扩展:
<?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()
{
//Override the parent to solve an issue with not finding the correct module
$this->bootstrap = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
parent::setUp();
}
}
?>
PHPUnit尝试写日志文件时得到的错误是:
Fatal error: Class 'Symfony\Component\Console\Command\Command' not found in C:\repositories\myfirstzend.com\includes\library\Doctrine\DBAL\Tools\Console\Command\ImportCommand.php on line 38
有关我做错的任何线索?我在PHP 5.4,Windows 7,XAMPP 8.0,Pear是最新的,我有最新的PHPUnit。
更新如果我从Matthew Weier O'Phinney的博客中将我的Bootstrap.php更改为以下内容:
<?php
/*
* Start output buffering
*/
ob_start();
/*
* Set error reporting to the level to which code must comply.
*/
error_reporting( E_ALL | E_STRICT );
/*
* Set default timezone
*/
date_default_timezone_set('GMT');
/*
* Testing environment
*/
define('APPLICATION_ENV', 'testing');
/*
* Determine the root, library, tests, and models directories
*/
$root = realpath(dirname(__FILE__) . '/../');
$library = $root . '/library';
$tests = $root . '/tests';
$models = $root . '/application/models';
$controllers = $root . '/application/controllers';
/*
* Prepend the library/, tests/, and models/ directories to the
* include_path. This allows the tests to run out of the box.
*/
$path = array(
$models,
$library,
$tests,
get_include_path()
);
set_include_path(implode(PATH_SEPARATOR, $path));
/**
* Register autoloader
*/
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
/**
* Store application root in registry
*/
Zend_Registry::set('testRoot', $root);
Zend_Registry::set('testBootstrap', $root . '/application/bootstrap.php');
/*
* Unset global variables that are no longer needed.
*/
unset($root, $library, $models, $controllers, $tests, $path);
我继续从Doctrine获得有关Symfony的错误。我确保我也安装了pear.symfony.com/Yaml。所以仍然破碎。
如果我从我的app.ini中删除了我正在测试的应用程序的Doctrine引用(这意味着它没有加载),我仍然会收到错误。几乎感觉就像这三个部分(PHPUnit,ZF,Doctrine)中的每个部分的加载器都在互相争斗。有办法解决这个问题吗?
第二次更新:我将PHPUnit降级为3.4.15,我仍然遇到此问题。我的下一步是从PHP 5.4到5.3.x。
第三次更新:我现在使用的是PHP 5.3.10并且看到同样的错误。
如果您需要更多信息,请告诉我。
答案 0 :(得分:3)
我错过了在你的引导程序中加载应用程序。
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
为了让您了解我在tests / bootstrap.php中的内容(这是自发行版1.11.4以来由zend工具自动生成的)
<?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(),
)));
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance();
如Zend Framework手册所述,最好是使用PHPUnit 3.4.15虽然我可以使用PHPUnit 3.6.12运行我的测试,因为我正在隔离我的测试以专注于我的业务逻辑而不是Zend Framework的逻辑
我还将我的phpunit.xml修改为以下内容:
<phpunit bootstrap="./bootstrap.php" colors="true">
<testsuite name="Application Test Suite">
<directory>./application</directory>
</testsuite>
<testsuite name="Library Test Suite">
<directory>./library</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../../library</directory>
<directory suffix=".php">../../application</directory>
<exclude>
<directory suffix=".php">../../library/Zend</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
我希望这能解决你现在面临的许多问题。
致以最诚挚的问候,
米开朗基罗