php PHPUnit 通过 Composer 和 PhpStorm
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12814263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPUnit via Composer and PhpStorm
提问by shredding
I'm now trying for hours to setup PhpStorm for unit testing. Whatever I do, I get this
我现在花了几个小时来设置 PhpStorm 进行单元测试。无论我做什么,我都明白
Process finished with exit code 1
Cannot find PHPUnit in include path ...
PHPUnit is (via command line) accessible from anywhere; I've set the correct include path and have added PHPUnit to the external libraries. No chance.
PHPUnit 可以(通过命令行)从任何地方访问;我已经设置了正确的包含路径并将 PHPUnit 添加到外部库中。没有机会。
Is it possible at all or does it only work with installations via PEAR? I've worked through all related questions here, am on Mac and PHPUnit is running smoothly via CLI.
有可能吗,还是只能通过 PEAR 安装?我在这里解决了所有相关问题,我在 Mac 上,PHPUnit 通过 CLI 顺利运行。
采纳答案by aderuwe
Patrick Visma posted the solution on http://youtrack.jetbrains.com/issue/WI-13429:
Patrick Visma 在http://youtrack.jetbrains.com/issue/WI-13429上发布了解决方案:
Edit the configuration for your phpunit.xml in PHPStorm, and set the Interpreter options like this:
在 PHPStorm 中编辑 phpunit.xml 的配置,并像这样设置解释器选项:
-d auto_prepend_file=/path/to/vendor/autoload.php
回答by Adam Lynch
@aderuwe's answer works but that config isn't scoped per project, it's for all projects. Since PHPStorm 6 (actually even EAP version PS-126.260) you can do the following:
@aderuwe 的答案有效,但该配置不是针对每个项目的,而是针对所有项目的。从 PHPStorm 6(实际上甚至是 EAP 版本PS-126.260)开始,您可以执行以下操作:
- Go to
File->Settings->PHP->PHPUNIT. - Select
Use custom loaderand then enter theautoload.phpfile from your projects vendor directory. This autoloader will autoload all the dependencies managed by Composer (including PHPUnit). - Specify the default configuration file (this is usually
phpunit.xml.distfrom your project'sappdirectory).
- 去
File->Settings->PHP->PHPUNIT。 - 从您的项目供应商目录中选择
Use custom loader并输入autoload.php文件。此自动加载器将自动加载 Composer 管理的所有依赖项(包括 PHPUnit)。 - 指定默认配置文件(这通常
phpunit.xml.dist来自您的项目app目录)。


That needs to be repeated per project, but the following needs to be done once.
这需要在每个项目中重复,但以下需要完成一次。
You need to configure you defaults in PHPStorm correctly.
您需要在 PHPStorm 中正确配置默认值。
- Go to
Run->Edit Configurations...or just click the dropdown menu on the toolbar and selectEdit Configurations.... - Go to
Defaults->PHPUnit - Under
Test Scope, selectDefined in configuration file - Make sure the
Interpreter optionstextfield is empty - Go to
PHPUnit(aboveDefaults) and delete any entry under it (so that next time you a test on a particular file, etc. it'll set up a new one but using the new default).
- 转到
Run->Edit Configurations...或仅单击工具栏上的下拉菜单并选择Edit Configurations...。 - 去
Defaults->PHPUnit - 在 下
Test Scope,选择Defined in configuration file - 确保
Interpreter options文本字段为空 - 转到
PHPUnit(上方Defaults)并删除其下的任何条目(以便下次对特定文件等进行测试时,它将设置一个新文件,但使用新的默认值)。


回答by Seldaek
I would guess PhpStorm relies on finding PHPUnit in the include_path of php.ini itself, not of your project.
我猜 PhpStorm 依赖于在 php.ini 本身的 include_path 中查找 PHPUnit,而不是在您的项目中。
You can try adding your local project's vendor/bin directory to php.ini's include_path, but ultimately this seems like PhpStorm might benefit from supporting composer installs and run the phpunit of the project itself if it's available. There actually is an issue opened on their tracker so I would recommend you login there and upvote it: http://youtrack.jetbrains.com/issue/WI-13429
您可以尝试将本地项目的 vendor/bin 目录添加到 php.ini 的 include_path,但最终这似乎 PhpStorm 可能会受益于支持 composer 安装并运行项目本身的 phpunit(如果可用)。他们的跟踪器上实际上存在一个问题,因此我建议您登录并对其进行投票:http: //youtrack.jetbrains.com/issue/WI-13429
回答by Vasiliy P
I want to add that current version of PHPStorm (7.1) works well only with the previous version of PHPUnit(3.7). And it fails to work with 4.0 and above. If you have downloaded phpunit.phar from http://phpunit.de, you can point PHPStorm to it via Settings => PHP => PHPUnit => Path to phpunit.phar
我想补充一点,当前版本的 PHPStorm (7.1) 仅适用于以前版本的 PHPUnit(3.7)。它无法与 4.0 及更高版本一起使用。如果你已经从http://phpunit.de下载了 phpunit.phar ,你可以通过 Settings => PHP => PHPUnit => Path to phpunit.phar 指向 PHPStorm
回答by edorian
PHPStorm has a custom PHPUnit runner script ( ide-phpunit.php) that it uses internally to deal with different versions of PHPUnit and to do the IDE integration and that is just relying on the include path and the PEARfolder layout.
PHPStorm 有一个自定义的 PHPUnit 运行脚本 ( ide-phpunit.php),它在内部使用它来处理不同版本的 PHPUnit 并进行 IDE 集成,这仅依赖于包含路径和PEAR文件夹布局。
To do that it executes:
为此,它执行:
public static function checkIncludePath()
{
//check include path
$PHPUnitParentDirectory = self::getPHPUnitParentDirectory();
if (is_null($PHPUnitParentDirectory)) {
echo "Cannot find PHPUnit in include path (" . ini_get('include_path') . ")";
exit(IDE_PHPUnit_Loader::FAILURE_EXIT);
}
}
/**
* @return null | string
*/
private static function getPHPUnitParentDirectory()
{
$pathArray = explode(PATH_SEPARATOR, ini_get('include_path'));
foreach ($pathArray as $path)
{
if (file_exists($path . DIRECTORY_SEPARATOR . 'PHPUnit/')) {
return $path;
}
}
return null;
}
You could just install it via composer and add it as a normal executable (ant/phing/native task) but you will loose all the shiny features PHPStorm offers like the progress bar, jumping to a failing test and the code coverage support.
您可以通过 composer 安装它并将其添加为普通的可执行文件(ant/phing/native 任务),但是您将失去 PHPStorm 提供的所有闪亮功能,例如进度条、跳转到失败的测试和代码覆盖率支持。
So from the code you can tell taht you need to add a folder called named PHPUnitto one of the places in your include path. Adding vendor might help there but keep in mind it needs to match casewhen doing so.
因此,从代码中您可以看出您需要将名为 named 的文件夹添加PHPUnit到包含路径中的某个位置。添加供应商可能会有所帮助,但请记住,这样做时需要匹配大小写。
Until PHPStorm supports using PHPUnit from composer or a phar I don't think you will get better integration ( http://youtrack.jetbrains.com/issue/WI-13429) with using it from pear but hacking your include path might work out.
在 PHPStorm 支持使用来自 Composer 或 phar 的 PHPUnit 之前,我认为您不会http://youtrack.jetbrains.com/issue/WI-13429通过使用 pear获得更好的集成 ( ),但破解您的包含路径可能会奏效。
The PHPStorm guys did some updates on the runner for 3.7 and are usually quite responsive to changes in PHPUnit :)
PHPStorm 人员对 3.7 的运行程序进行了一些更新,并且通常对 PHPUnit 中的更改非常敏感 :)
回答by b01
I have found that if I have all of my in the "Test" directory, and a test suite defined in a configuration file "Test/phpunit.xml"; I can simply setup a "Run Configuration" like so:
我发现如果我在“Test”目录中拥有我的所有内容,并且在配置文件“Test/phpunit.xml”中定义了一个测试套件;我可以简单地设置一个“运行配置”,如下所示:
- Open the Run/Debug Configuration dialog box by clicking Run | Edit Configurations.
- Click +on the toolbar and choose PHPUnit(for unit tests executed locally).
- Fill in the Nameat the top with what ever you like.
- Set Test scopeto the directory where your test are located.
- Click OK
- Then run the new configuration.
- 单击运行 |打开运行/调试配置对话框。编辑配置。
- 单击工具栏上的+并选择PHPUnit(用于本地执行的单元测试)。
- 在顶部填写您喜欢的名称。
- 将测试范围设置为您的测试所在的目录。
- 单击确定
- 然后运行新配置。


Phpstorm Run|Configuration dialog
Phpstorm 运行|配置对话框

