我正在练习非常基本的单元测试,并且代码工作正常。但每次测试失败时,我都会收到以下错误,导致无法显示测试的完整失败消息:
C:\wamp\www\project-phpunit>phpunit
PHPUnit 3.7.38 by Sebastian Bergmann.
Configuration read from C:\wamp\www\project-phpunit\phpunit.xml
.....
Time: 156 ms, Memory: 2.25Mb
OK (5 tests, 5 assertions)
C:\wamp\www\project-phpunit>phpunit
PHPUnit 3.7.38 by Sebastian Bergmann.
Configuration read from C:\wamp\www\project-phpunit\phpunit.xml
....F.
Time: 109 ms, Memory: 2.25Mb
There was 1 failure:
1) CardTest::testIsInMatchingSet with data set #2 (Card, false, 'should not match')
Warning: require(PHP\Invoker.php): failed to open stream: No such file or directory in C:\wamp\www\project-phpunit\test-bootstrap.php on line 5
Call Stack:
0.0000 127408 1. {main}() C:\phpunit\vendor\phpunit\phpunit\composer\bin\phpunit:0
0.0000 333752 2. PHPUnit_TextUI_Command::main() C:\phpunit\vendor\phpunit\phpunit\composer\bin\phpunit:63
0.0000 337152 3. PHPUnit_TextUI_Command->run() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\Command.php:129
0.0312 1643576 4. PHPUnit_TextUI_TestRunner->doRun() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\Command.php:176
0.1092 2224520 5. PHPUnit_TextUI_ResultPrinter->printResult() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\TestRunner.php:356
0.1248 2224704 6. PHPUnit_TextUI_ResultPrinter->printFailures() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\ResultPrinter.php:163
0.1248 2224832 7. PHPUnit_TextUI_ResultPrinter->printDefects() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\ResultPrinter.php:307
0.1248 2225088 8. PHPUnit_TextUI_ResultPrinter->printDefect() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\ResultPrinter.php:225
0.1404 2225560 9. PHPUnit_TextUI_ResultPrinter->printDefectTrace() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\ResultPrinter.php:238
0.1404 2239120 10. PHPUnit_Util_Filter::getFilteredStacktrace() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\ResultPrinter.php:274
0.1404 2239400 11. PHPUnit_Util_GlobalState::phpunitFiles() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\Util\Filter.php:76
0.2340 2322608 12. PHPUnit_Util_GlobalState::addDirectoryContainingClassToPHPUnitFilesList() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\Util\GlobalState.php:389
0.2340 2322672 13. class_exists() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\Util\GlobalState.php:410
0.2340 2322856 14. spl_autoload_call() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\Util\GlobalState.php:410
0.2340 2322968 15. PHPUnit_Util_Fileloader::{closure:C:\wamp\www\project-phpunit\test-bootstrap.php:3-6}() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\Util\GlobalState.php:0
Fatal error: require(): Failed opening required 'PHP\Invoker.php' (include_path='C:\wamp\www\project-phpunit\src;C://phpunit//vendor;C:\wamp\www\project-phpunit\src;C://phpunit//vendor;C:\phpunit\vend
or/phpunit/php-text-template;C:\phpunit\vendor/phpunit/phpunit-mock-objects;C:\phpunit\vendor/phpunit/php-timer;C:\phpunit\vendor/phpunit/php-token-stream;C:\phpunit\vendor/phpunit/php-file-iterator;C
:\phpunit\vendor/phpunit/php-code-coverage;C:\phpunit\vendor/phpunit/phpunit;C:\phpunit\vendor/symfony/yaml;.;C:\php\pear') in C:\wamp\www\project-phpunit\test-bootstrap.php on line 5
Call Stack:
0.0000 127408 1. {main}() C:\phpunit\vendor\phpunit\phpunit\composer\bin\phpunit:0
0.0000 333752 2. PHPUnit_TextUI_Command::main() C:\phpunit\vendor\phpunit\phpunit\composer\bin\phpunit:63
0.0000 337152 3. PHPUnit_TextUI_Command->run() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\Command.php:129
0.0312 1643576 4. PHPUnit_TextUI_TestRunner->doRun() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\Command.php:176
0.1092 2224520 5. PHPUnit_TextUI_ResultPrinter->printResult() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\TestRunner.php:356
0.1248 2224704 6. PHPUnit_TextUI_ResultPrinter->printFailures() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\ResultPrinter.php:163
0.1248 2224832 7. PHPUnit_TextUI_ResultPrinter->printDefects() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\ResultPrinter.php:307
0.1248 2225088 8. PHPUnit_TextUI_ResultPrinter->printDefect() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\ResultPrinter.php:225
0.1404 2225560 9. PHPUnit_TextUI_ResultPrinter->printDefectTrace() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\ResultPrinter.php:238
0.1404 2239120 10. PHPUnit_Util_Filter::getFilteredStacktrace() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\TextUI\ResultPrinter.php:274
0.1404 2239400 11. PHPUnit_Util_GlobalState::phpunitFiles() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\Util\Filter.php:76
0.2340 2322608 12. PHPUnit_Util_GlobalState::addDirectoryContainingClassToPHPUnitFilesList() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\Util\GlobalState.php:389
0.2340 2322672 13. class_exists() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\Util\GlobalState.php:410
0.2340 2322856 14. spl_autoload_call() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\Util\GlobalState.php:410
0.2340 2322968 15. PHPUnit_Util_Fileloader::{closure:C:\wamp\www\project-phpunit\test-bootstrap.php:3-6}() C:\phpunit\vendor\phpunit\phpunit\PHPUnit\Util\GlobalState.php:0
答案 0 :(得分:2)
此PHP_Invoker错误的根本原因似乎是在自动加载器中。
当PHPunit运行时,它会使用class_exists函数检查某些类是否存在。默认情况下,此函数尝试使用自动加载器加载类。
如果自动加载器实现在找不到类时失败,那么您将遇到此错误。
导致故障的自动加载器实现示例如下。如果找不到该文件,require_once将失败。
function AutoLoader($className)
{
$file = __DIR__ . DIRECTORY_SEPARATOR . str_replace('\\',DIRECTORY_SEPARATOR,$className) . ".php";
require_once($file);
}
如果您有自己的自动加载器,一种解决方案是在那里添加file_exists检查:
function AutoLoader($className)
{
$file = __DIR__ . DIRECTORY_SEPARATOR . str_replace('\\',DIRECTORY_SEPARATOR,$className) . ".php";
if(file_exists($file)
require_once($file);
}
参考:YII框架问题跟踪器中的讨论:https://github.com/yiisoft/yii/issues/1907
答案 1 :(得分:0)
PHP_Invoker是PHPUnit的可选依赖项。您不应手动包含PHP/Invoker.php
(或任何软件包的源文件)。