【问题标题】:Can't extend PHPUnit_Extensions_Database_TestCase无法扩展 PHPUnit_Extensions_Database_TestCase
【发布时间】:2012-04-10 09:28:07
【问题描述】:

我已经完全相信 TDD 并尝试按照书本使用它,期待有接近 100% 的测试覆盖率,并且总是在新编码之前编写测试。

我在 ZF 上使用 phpUnit,虽然有时我能感觉到一些进步,但在其他时候我觉得我完全被卡住了。

我的应用程序非常以数据库为中心,我需要开始测试(和编码...)数据库持久性。

我正在查看来自Testing Lamp Appplications Presentation by Stephenn Bergmann 的幻灯片,它似乎非常清晰明了如何测试数据库交互。

但是,我总是尝试使用扩展 PHPUnit_Extensions_Database_TestCase 的类我收到关于在 'PHPUnit_Extensions_Database' 命名空间中找不到某些类的错误消息。

可能我错过了一些关键点,因为当我尝试遵循我能找到的任何其他解决方案和建议时遇到同样的问题,例如由于我的声誉而无法在此处链接的答案和演示文稿...

在所有情况下,当我扩展 Database_TestCase 时,都找不到某些类。

我怀疑与我的引导方式有关,但我找不到我做错了什么。我编写的扩展 ControllerTestCase 的测试运行良好。

我正在运行 Xampp。 phpUnit版本os 3.6.10,php 5.3.8,Zend Framework 1.11.11

我正在使用以下 phpunit.xml 作为 php 配置:

<phpunit bootstrap="./application/bootstrap.php" colors="true">
<testsuite name="Some">
    <directory>./</directory>
</testsuite>

<filter>
    <whitelist>
        <directory suffix=".php">../application/</directory>
        <directory suffix=".php">../library/MyApp/</directory>
        <exclude>
            <directory suffix=".phtml">../application/</directory>
            <file>../application/Bootstrap.php</file>
            <file>../application/modules/default/controllers/ErrorController.php</file>
        </exclude>
    </whitelist>
</filter>

<logging>
    <log type="coverage-html" target="./log/report" charset="UTF-8" yui="true" hightlight="true" lowupperbound="50" highlowerbound="80">
    <log type="testdox" target="./log/testdox.html">
</log></log></logging>

我的 Bootstrap.php 是:

    error_reporting(E_ALL | E_STRICT);

// 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/Application.php';
require_once 'ControllerTestCase.php';

和ControllerTestCase.php

require_once 'Zend/Application.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
    /**
     * @var Zend_Application
    */
    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');
        $this->application->bootstrap();
    }
}

当我创建一个测试时,例如,这个 tryTest.php

    require_once 'PHPUnit/Extensions/Database/TestCase.php';

class TryTest extends PHPUnit_Extensions_Database_TestCase {
    /*** any test or even just getConnection ***/
}

我收到以下错误消息或以其他方式尝试的类似错误消息:

致命错误:在 C:\xampp\php\PEAR\PHPUnit\Extensions\Database\TestCase.php 中找不到类 'PHPUnit_Extensions_Database_DefaultTester' 第 132 行

调用堆栈: 0.0003 326968 1. {main}() C:\xampp\php\phpunit:0 0.0141 745152 2. PHPUnit_TextUI_Command::main() C:\xampp\php\phpunit:46 0.0141 745568 3. PHPUnit_TextUI_Command->run() C:\xampp\php\PEAR\PHPUnit\TextUI\Command.php:130 0.4012 5280032 4. PHPUnit_TextUI_TestRunner->doRun() C:\xampp\php\PEAR\PHPUnit\TextUI\Command.php:192 0.7182 5750296 5. PHPUnit_Framework_TestSuite->run() C:\xampp\php\PEAR\PHPUnit\TextUI\TestRunner.php:325 2.4058 11441872 6. PHPUnit_Framework_TestSuite->run() C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php:705 2.4060 11442104 7. PHPUnit_Framework_TestSuite->runTest() C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php:745 2.4060 11442104 8. PHPUnit_Framework_TestCase->run() C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php:772 2.4061 11442104 9. PHPUnit_Framework_TestResult->run() C:\xampp\php\PEAR\PHPUnit\Framework\TestCase.php:751 2.4066 11441136 10. PHPUnit_Framework_TestCase->runBare() C:\xampp\php\PEAR\PHPUnit\Framework\TestResult.php:649 2.4079 11494800 11. PHPUnit_Extensions_Database_TestCase->setUp() C:\xampp\php\PEAR\PHPUnit\Framework\TestCase.php:801 2.4080 11494832 12. PHPUnit_Extensions_Database_TestCase->getDatabaseTester() C:\xampp\php\PEAR\PHPUnit\Extensions\Database\TestCase.php:202 2.4080 11494832 13. PHPUnit_Extensions_Database_TestCase->newDatabaseTester() C:\xampp\php\PEAR\PHPUnit\Extensions\Database\TestCase.php:92

在 C:\xampp\php\PEAR\PHPUnit\Extensions\Database 有一个 DefaultTester.php,我不知道发生了什么...

我在遵循这个 [Jon Lebensold 的 Zendcast][2] 之后得到了这个配置。在那一刻,我在尝试解决一些问题时遇到了一些困难,但在@edorian 的帮助下,我发现主要问题出在我的椅子和键盘之间...... :-/ 现在可能同样的情况......

我什至尝试以与 Jon 构建他的 ControllerTestCase.php 文件类似的方式创建 MapperTestCase,但这对我没有帮助。

所以,现在我在这里,试图获得一些帮助,以找出我的环境或方法有什么问题。

【问题讨论】:

    标签: php tdd persistence phpunit dbunit


    【解决方案1】:

    不太确定它是否解决了所有问题,但在 Stackoverflow 上搜索,我会发现这个 question that reported something similar 是我的:

    require_once 'Zend/Loader/AutoLoader.php';
    $autoloader = Zend_Loader_AutoLoader::getInstance();
    $autoloader->registerNamespace('PHPUnit_');
    

    当我尝试将上面的代码放在我的 bootstrap.php 中时,我至少可以使 $connection = new PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection($pdo, 'mydb'); 工作

    我仍然不明白为什么找不到 PHPUnit 扩展,但是,至少我可以前进...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 2014-10-30
      • 2015-03-13
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多