PHPUnit:无法打开所需的`PHPUnit\u Extensions\u Story\u TestCase.php`
我安装了PHPUnit:无法打开所需的`PHPUnit\u Extensions\u Story\u TestCase.php`,php,phpunit,Php,Phpunit,我安装了PHPUnit,带有: wget https://phar.phpunit.de/phpunit.phar chmod +x phpunit.phar mv phpunit.phar /usr/local/bin/phpunit 尝试运行简单测试,但得到: Fatal error: require_once(): Failed opening required 'PHPUnit_Extensions_Story_TestCase.php' 如何安装PHPUnit\u扩展\u故事\u测
PHPUnit
,带有:
wget https://phar.phpunit.de/phpunit.phar
chmod +x phpunit.phar
mv phpunit.phar /usr/local/bin/phpunit
尝试运行简单测试,但得到:
Fatal error: require_once(): Failed opening required 'PHPUnit_Extensions_Story_TestCase.php'
如何安装PHPUnit\u扩展\u故事\u测试用例
这个测试很简单:
class TestFunctions extends PHPUnit_Framework_TestCase {
public function test_str() {
$this->assertEquals('foo', 'bar');
}
}
您需要安装phpunit/phpunit_故事包:
sudo pear channel-discover pear.phpunit.de
sudo pear install phpunit/PHPUnit_Story
或者从手动操作。不幸的是,建议的修复方案对我都不起作用。典型的响应是安装phpunit/phpunit_故事模块。虽然这会让你走上正确的方向,但并没有解决我的问题 我在boostrap.php文件中注册了一个自动加载函数。这很可能取代了PHPUnit注册的自动加载函数,该函数用于自动加载PHPUnit的类。我评论了我的自动加载函数实现,问题就消失了 编辑 回应@user3265472;我已经有一段时间没有做过这方面的工作了,但我想说的是,“修复”是在bootstrap.php文件的开头设置include路径,然后像通常一样手动加载类:
/**
* Configure include paths used by the unit tests.
*
* @return void
*/
function configure_include_paths()
{
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . "/mylib");
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . "/mylib2");
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(dirname(__FILE__)) . "/lib");
}
configure_include_paths();
这使我可以在每个文件的开头执行以下操作:
require_once("MyClass.php");
而不必确定类相对于当前类文件的位置
我还想说,无论我做了什么,我都不能让类自动加载像我希望的那样工作。我希望这会有所帮助。就像Bakyt Abdrasulov提到的那样,这似乎是一个自动加载器问题 见他的评论:
“我通过在自动加载功能中使用@include\u once而不是require\u once修复了它。”如果使用composer安装phpunit,这些错误不会出现 步骤1: 在项目根目录中创建composer.json文件:
{
"require-dev": {
"phpunit/phpunit": "4.6.*",
"phpunit/phpunit-selenium": ">=1.4",
"phpunit/dbunit": ">=1.3",
"phpunit/phpunit-story": "*",
"phpunit/php-invoker": "*"
},
"autoload": {
"psr-0": {"": "src"}
},
"config": {
"bin-dir": "bin/"
}
}
步骤2:
使用以下方法将composer安装到项目中:
curl -sS https://getcomposer.org/installer | php
确保composer是可执行的:
chmod +x composer.phar
让composer安装依赖项:
./composer.phar install --dev
检查是否安装了特定于项目的phpunit版本:
bin/phpunit --version
上面指定的是软链接
ls -la bin/phpunit
bin/phpunit -> ../vendor/phpunit/phpunit/phpunit
之后,您可以将“phpunit”的软链接从供应商目录转到正在使用的php目录中。
这将删除与此相关的所有警告
PHP Warning: include(classes/PHPUnit_Extensions_Story_TestCase.php)
PHP Warning: include(): Failed opening 'classes/PHPUnit_Extensions_Story_TestCase.php'
PHP Warning: include(classes/Composer\Autoload\ClassLoader.php)
我能够安装PHPUnit\u Story,但仍然得到:
无法打开所需的“PHPUnit\u Extensions\u Story\u TestCase.php”
。pear在哪里安装的?/usr/share/pear
但是PHPUnit
的内容只是Framework/MockObject
。我是否必须手动require()
PHPUnit\u Extensions\u Story\u TestCase。谢谢你的帮助。我想你需要通过pear
重新安装整个PHPUnit包。因为它将正确安装所有依赖项:sudo pear install pear.phpunit.de/phpunit
。根据phpunit网站,pear安装程序对phpunit的支持将在2014年底取消。已经有使用该方法生成的警告消息要安装。Justin正在使用新的经批准的安装方法,并且可能需要“新的*.phar案例安装”说明。我知道我知道!:)嗨,我也在我的引导程序中注册了自动加载。我想我也有同样的问题。如果我对它进行注释,我将无法加载我的应用程序类。你是怎么做到的?嗨。我也有同样的问题。我在自动加载功能中使用了@include_once
而不是require_once
来修复它。