问题描述
有时,IntelliJ IDEA 在运行、调试或显示 PHPUnit 测试的代码覆盖率时会出现问题.当它生成的类与您拥有的 PHPUnit 版本不兼容时,就会发生这种情况.
Sometimes IntelliJ IDEA has problems running, debugging, or showing code-coverage for PHPUnit tests. This can occur when the classes it generates are not compatible with the version of PHPUnit you have.
就我而言,它是 IntelliJ IDEA 12.1.6 与 PHPUnit 4.0.14,它总是失败并显示以下消息:
In my case, it's IntelliJ IDEA 12.1.6 versus PHPUnit 4.0.14, which always fails with this message:
/usr/bin/php /tmp/ide-phpunit.php --configuration /home/username/Documents/stuff/phpunit.xml.dist
Testing started at 5:32 PM ...
PHP Fatal error: Class IDE_PHPUnit_Framework_TestListener contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (PHPUnit_Framework_TestListener::addRiskyTest) in /tmp/ide-phpunit.php on line 496
PHP Stack trace:
PHP 1. {main}() /tmp/ide-phpunit.php:0
Process finished with exit code 255
它在 /tmp/ide-phpunit.php
中生成的代码不包含 PHPUnit 4.x 所需的新方法.
The code it is generating in /tmp/ide-phpunit.php
does not contain a new method required by PHPUnit 4.x.
我尝试过但不起作用的事情:
- 手动修复文件并将其标记为只读会导致 IntelliJ 停止并抱怨它无法替换文件.
- 设置run-before"命令来自动修补文件是不够的,因为它似乎不适用于调试或代码覆盖,只能正常运行.
推荐答案
我发现的最佳解决方案是手动修复您的 IntelliJ 安装.这些说明假设 Linux 路径,但在 Windows 上应该可以执行相同的基本过程.
The best solution I've found is to patch your IntelliJ installation with a manual fix. These instructions assume Linux paths, but the same basic process should be possible on Windows.
首先,在您的 IntelliJ 安装中找到 php.jar
文件.JAR 文件是一种 ZIP 文件,您可以使用相同的工具打开(和修改)它们.在我的系统上,它出现在:
First, find the php.jar
file in your IntelliJ installation. JAR files are a kind of ZIP file, you can open (and modify) both of them with the same tools. On my system, it was present at:
/home/username/.IntelliJIdea12/config/plugins/php/lib/php.jar
备份php.jar
,因为我们要编辑它.
Make a backup of php.jar
, since we're going to edit it.
使用流行的 ZIP 文件工具(如 7-Zip)打开 php.jar
,并找到里面的压缩文件:
Using a popular ZIP-file tool (like 7-Zip) open php.jar
, and find the compresesd file inside called:
scripts/phpunit.php
将这个文件解压到一个你可以编辑的临时位置.
Extract this file to a temporary location where you can edit it.
在文件中,我们需要找到 IDE_PHPUnit_Framework_TestListener
类,在我的例子中它位于 303
行附近.在那个类上,我们需要添加一个新方法:
Inside the file, we need to find the class IDE_PHPUnit_Framework_TestListener
, which in my case is around line 303
. On that class, we need to add a new method:
public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time){}
完成后保存文件.
现在用您的新版本覆盖 JAR 中的 scripts/phpunit.php
.根据您的 ZIP 工具,这可能就像双击文件打开它、保存更改并单击确认提示一样简单,但这取决于您使用的是什么.
Now overwrite scripts/phpunit.php
inside the JAR with your new version. Depending on your ZIP tool, this might have been as easy as double-clicking the file to open it, saving your changes, and clicking a confirmation prompt, but it depends on what you're using.
现在你应该完成了!使用 PHPUnit 运行、调试或生成代码覆盖率数据应该只是一个方便的点击按钮.
Now you should be done! Running, debugging, or generating code-coverage data with PHPUnit should be just a convenient click of a button.
请注意,如果您更新 PHP 插件,它可能会覆盖修复程序,您需要再次重新应用它.
Note that if you update your PHP plugin, it will probably overwrite the fix and you'll need to re-apply it again.
这篇关于IntelliJ IDEA 不会运行 PHPUnit 4.0 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!