IntelliJ IDEA won't run PHPUnit 4.0 tests

2019-01-18 00:15发布

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.

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

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:

  • Fixing the file manually and marking it read-only causes IntelliJ to stop and complain that it cannot replace the file.
  • Setting up a "run-before" command to automatically patch the file is insufficient, because it doesn't seem to work for debugging nor code-coverage, only regular runs.

1条回答
再贱就再见
2楼-- · 2019-01-18 00:38

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.

Find the JAR

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

Make a backup of php.jar, since we're going to edit it.

Extract the template

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.

Add the method to the template

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){}

Save the file when you are done.

Update the JAR with the new template

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.

Restart IntelliJ

Now you should be done! Running, debugging, or generating code-coverage data with PHPUnit should be just a convenient click of a button.

Note that if you update your PHP plugin, it will probably overwrite the fix and you'll need to re-apply it again.

查看更多
登录 后发表回答