PHP警告包括PHPUnit_Extensions_Story_TestCase.php无法打开流

时间:2014-03-13 03:43:40

标签: php unit-testing phpunit

我使用php Yii框架并运行功能测试。 我已经安装了PHPUnit_Story

pear install phpunit/PHPUnit_Story

,扩展名在那里

/usr/share/php/PHPUnit/Extensions/Story

但我得到错误:

PHP Warning:  include(PHPUnit_Extensions_Story_TestCase.php): failed to open stream: No such file or directory in /var/www/yii/framework/YiiBase.php on line 427
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:581
PHP   3. PHPUnit_TextUI_Command->run() phar:///usr/bin/phpunit/phpunit/TextUI/Command.php:132
PHP   4. PHPUnit_TextUI_TestRunner->doRun() phar:///usr/bin/phpunit/phpunit/TextUI/Command.php:179
PHP   5. PHPUnit_TextUI_ResultPrinter->printResult() phar:///usr/bin/phpunit/phpunit/TextUI/TestRunner.php:425
PHP   6. PHPUnit_TextUI_ResultPrinter->printFailures() phar:///usr/bin/phpunit/phpunit/TextUI/ResultPrinter.php:177
PHP   7. PHPUnit_TextUI_ResultPrinter->printDefects() phar:///usr/bin/phpunit/phpunit/TextUI/ResultPrinter.php:322
PHP   8. PHPUnit_TextUI_ResultPrinter->printDefect() phar:///usr/bin/phpunit/phpunit/TextUI/ResultPrinter.php:243
PHP   9. PHPUnit_TextUI_ResultPrinter->printDefectTrace() phar:///usr/bin/phpunit/phpunit/TextUI/ResultPrinter.php:254
PHP  10. PHPUnit_Util_Filter::getFilteredStacktrace() phar:///usr/bin/phpunit/phpunit/TextUI/ResultPrinter.php:290
PHP  11. PHPUnit_Util_Blacklist->isBlacklisted() phar:///usr/bin/phpunit/phpunit/Util/Filter.php:105
PHP  12. PHPUnit_Util_Blacklist->initialize() phar:///usr/bin/phpunit/phpunit/Util/Blacklist.php:74
PHP  13. class_exists() phar:///usr/bin/phpunit/phpunit/Util/Blacklist.php:110
PHP  14. spl_autoload_call() phar:///usr/bin/phpunit/phpunit/Util/Blacklist.php:110
PHP  15. YiiBase::autoload() phar:///usr/bin/phpunit/phpunit/Util/Blacklist.php:0
PHP Warning:  include(): Failed opening 'PHPUnit_Extensions_Story_TestCase.php' for inclusion (include_path='.:/var/www/yii-example/protected/components:/var/www/yii-example/protected/models:/usr/share/php:/usr/share/pear') in /var/www/yii/framework/YiiBase.php on line 427
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:581
PHP   3. PHPUnit_TextUI_Command->run() phar:///usr/bin/phpunit/phpunit/TextUI/Command.php:132
PHP   4. PHPUnit_TextUI_TestRunner->doRun() phar:///usr/bin/phpunit/phpunit/TextUI/Command.php:179
PHP   5. PHPUnit_TextUI_ResultPrinter->printResult() phar:///usr/bin/phpunit/phpunit/TextUI/TestRunner.php:425
PHP   6. PHPUnit_TextUI_ResultPrinter->printFailures() phar:///usr/bin/phpunit/phpunit/TextUI/ResultPrinter.php:177
PHP   7. PHPUnit_TextUI_ResultPrinter->printDefects() phar:///usr/bin/phpunit/phpunit/TextUI/ResultPrinter.php:322
PHP   8. PHPUnit_TextUI_ResultPrinter->printDefect() phar:///usr/bin/phpunit/phpunit/TextUI/ResultPrinter.php:243
PHP   9. PHPUnit_TextUI_ResultPrinter->printDefectTrace() phar:///usr/bin/phpunit/phpunit/TextUI/ResultPrinter.php:254
PHP  10. PHPUnit_Util_Filter::getFilteredStacktrace() phar:///usr/bin/phpunit/phpunit/TextUI/ResultPrinter.php:290
PHP  11. PHPUnit_Util_Blacklist->isBlacklisted() phar:///usr/bin/phpunit/phpunit/Util/Filter.php:105
PHP  12. PHPUnit_Util_Blacklist->initialize() phar:///usr/bin/phpunit/phpunit/Util/Blacklist.php:74
PHP  13. class_exists() phar:///usr/bin/phpunit/phpunit/Util/Blacklist.php:110
PHP  14. spl_autoload_call() phar:///usr/bin/phpunit/phpunit/Util/Blacklist.php:110
PHP  15. YiiBase::autoload() phar:///usr/bin/phpunit/phpunit/Util/Blacklist.php:0

1 个答案:

答案 0 :(得分:8)

看起来PHPUnit和Yii自动加载器之间存在一些问题。 PHPUnit正在转换为使用单个phar存档,因此Yii尝试包含的文件不再存在。

现在推荐的安装PHPUnit和依赖项的方法是使用Composer。我花了几天时间使用PEAR与它的所有怪癖作斗争,但是这个作曲家和Yii的教程让我在几分钟内再次运行单元测试:

http://blog.mediasuite.co.nz/tip-how-to-set-up-phpunit-for-yii-using-composer/

基本步骤:

在项目根目录中创建composer.json文件:

{
    "require-dev": {
        "phpunit/phpunit": "3.7.*",
        "phpunit/phpunit-selenium": ">=1.2",
        "phpunit/dbunit": ">=1.2",
        "phpunit/phpunit-story": "*",
        "phpunit/php-invoker": "*"
    },
    "autoload": {
        "psr-0": {"": "src"}
    },
    "config": {
        "bin-dir": "bin/"
    }
}

使用以下方法将composer安装到您的项目中:

curl -sS https://getcomposer.org/installer | php

确保composer可执行:

chmod +x composer.phar

让composer安装依赖项:

./composer.phar install --dev

检查您是否安装了项目特定的phpunit版本:

./bin/phpunit --version

现在,在protected / tests中,您可以使用以下命令运行单元测试:

../../bin/phpunit unit

和你的硒测试

../../bin/phpunit functional

如果您正在使用版本控制,最好提交composer.json和composer.lock文件,但忽略/ vendor文件夹的内容