问题描述
有时,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
它在<$生成的代码c $ c> /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.
我所做的事情我试过不工作:
Things which I've tried that don't work:
- 手动修复文件并将其标记为只读会导致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
,找到里面的compresesd文件:
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测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!