Netbeans with PHPUnit and Selenium

| Tags: PHP, Tutorial

It took me some time to finally implement PHPUnit tests into NetBeans, my IDE of choice. So here is a small tutorial.

I tried to follow the installation instructions from PHPUnit but it simply didn't work. Via Google I found a nice tutorial which made PEAR work perfectly on my Mac. I used a MacBook Air and a Mac Mini with Mac OS X Tiger (10.7.3), NetBeans 7.1 and PHP 5.3.6. Make sure you have administrator rights for installation.

1. Install PEAR

Mac OS Tiger does already support PEAR, so it's very easy to install.
sudo php /usr/lib/php/install-pear-nozlib.phar

The next part is a bit tricky. We have to modify our PHP settings. If you don't have a php.ini yet, create a new one by renaming (ok... it's copying, i know) the default php.ini.default to php.ini.
sudo cp /etc/php.ini.default /etc/php.ini

Open your php.ini and search for the include_path. We have to change the path to the PEAR suggested one: include_path = ".:/php/includes:/usr/lib/php/pear".

Finally restart the Apache to reload our settings.
sudo /usr/sbin/apachectl restart

PEAR should be installed now.

2. Install PHPUnit

Now that we installed PEAR, we need the PHPUnit package to be installed too. Load the packages via
pear config-set auto_discover 1
sudo pear install pear.phpunit.de/PHPUnit

PHPUnit is now installed and we can configure Netbeans. Go to "Netbeans → Preferences → PHP → Unit Testing" and click on "Search...". Netbeans should now find PHPUnit by itself (/usr/bin/phpunit). Click "OK" to save changes.

Yeehaw. PHPUnit is now perfectly integrated into NetBeans. To run tests, right click on a project and choose "Test". At the first time you click on "Test" you will be asked for a directory with test files.

3. Setup Selenium for NetBeans

To use Selenium with NetBeans we need an additionally package: PHPUnit_Selenium. Install it via
sudo pear install phpunit/PHPUnit_Selenium

NetBeans has no native support for Selenium. But there are Plugins developed by Jindrich Sedek and Martin Fousek. Click on "Tools → Plugins → Available Plugins" and install "Selenium Server" and "Selenium Module for PHP". Restart your IDE and right click on a project. You should now see a "Run Selenium Tests" option.