Using PHPUnit with Eclipse PDT (PHP Development Tools)
Eclipse PHP Developer Tools (PDT) is a very capable and mature platform for developing PHP applications. For those that develop only in PHP, an integrated installation is available that includes the Eclipse platform and any dependencies. For developers that may already be using Eclipse for development in another language, PDT can be installed using Eclipse's software update facility.
The one feature that PDT lacks is integration with PHPUnit. This can be a real hindrance for teams using Test Driven Development, and discourages everyone from frequently running their unit test suite while developing.
Luckily, the Eclipse platform has a mechanism for adding external tools to a project, and makes it very easy to call these tools once they are configured. In this post, I'll outline the steps I took to add PHPUnit support to my installation of PDT.
This post assumes that you have already installed Eclipse, and have a local development environment for PHP and PHPUnit. Jonathan Tran has a handy tutorial on installing PHPUnit on a Windows machine that I found very helpful.
In my PHP projects, I keep my unit tests in a directory called /tests. To start, select any unit test file - I chose AllTests.php, which executes a test suite containing all my unit tests for the project. Once the file is selected, select the menu item Run->External Tools->External Tools Configurations... That brings up a dialog box similar to this.
Select the Program configuration category, and click on the first icon - New Launch Configuration. Name your new configuration PHPUnit.
The location field should contain the full path to your phpunit shell script. On windows, this will be called phpunit.bat. On my computer, the correct location is c:\pear\phpunit.bat; however, this will vary by system.
The Working Directory should be set to the base location of your unit test files. In my case, this is the fully qualified \tests path.
For the Arguments list, you should have one entry - ${resource_name}. This tells Eclipse to pass the name of the currently highlighted unit test to phpunit.bat. To add this, click on the Variables... button and find resource_name in the displayed list.
The remainder of the tabs can be left with their default values. Your finished configuration should look like this:
Press Apply to save your changes. You can now press the Run button to execute PHPUnit immediately, or press Close to exit this dialog box.
After the first time you run PHPUnit using the External Tools Configurations dialog box, your PHPUnit configuration will be associated with the Run External Tools icon on you Eclipse workspace - this is the green Play Button icon decorated with a little red toolbox. To execute any unit test script as you develop, simply select the script in the PHP Explorer and press the Run External Tools icon.


