Using Debugging and Unit Testing tools with your PHP application: Installing PHPUnit
One of the things that developers love to hate has to be testing their code. Any process that can make this part of the development life cycle as short and painless as possible is bound to be embraced by coders. If only there was a way where you could just write a test for the various scripts that a PHP application would have and save it so that any changes could be tested with just a click of a button?
Well now there is, and it is called PHPUnit.
First we’ll need to install it on our system, and for that we’ll need PEAR. Fortunately, WAMPServer has PEAR already installed as part of its default package so that’s one hurdle down. Also handy to know should other PEAR derived products be needed in our development as we progress. However we’ll need to have PEAR set up so that our system knows where to find it when executed. For that we’ll need to run go-pear.bat, which can be found on C:/wamp/bin/php/php5.3.0 for a default install of WAMPServer. Before executing however, a change will be needed to the file itself. The bolded text is where the change is needed:
%PHP_BIN% -d output_buffering=0 -d phar.require_hash=0 PEARgo-pear.phar
Save this and then execute via the command line. It will ask you a whole bunch of questions that you will want to just hit enter too to have the default settings kept. Enter Y -> Enter when prompted to alter php.ini. When it is finished, it will create a PEAR_ENV.reg file in the PHP folder that you are currently using. Take this file, and edit it using notepad or whatever else you wish. Here’s what it should look like when your done (changes in bold, using the WAMPServer default install paths)
Then we’ll need to edit the pear.bat file to allow it to find the php executable. Look for this section of the file (it’s near the top) and edit accordingly:
REM Check PEAR global ENV, set them if they do not exist
IF “%PHP_PEAR_INSTALL_DIR%”==”" SET “PHP_PEAR_INSTALL_DIR=C:\wamp\bin\php\php5.3.0\pear”
IF “%PHP_PEAR_BIN_DIR%”==”" SET “PHP_PEAR_BIN_DIR=C:\wamp\bin\php\php5.3.0” IF “%PHP_PEAR_PHP_BIN%”==”" SET “PHP_PEAR_PHP_BIN=C:\wamp\bin\php\php5.3.0\php.exe”
Once saved, then we’ll need to add our PEAR directory as a system environment variable, or windows is not going to know what to run when we type in per commands. For this we go to the desktop and right click on my computer and select properties. Here we’ll select the advanced tab, then environment variables. Under system variables, you’ll see an entry for path. Select it, then click on edit, and then add the following to the end of the variable value box, add this line:
…where the php directory would be the version of PHP that you are using. So if you are using the default 5.3, the %phpdir% would be php5.3.0 which tells windows where the pear executable can be found.
Now go to the command line and type in pear. If you get a long scrolling of various options that you could use, congrats, you’ve correctly setup up pear for your computer. If there’s an incorrect configuration, it’ll say that there’s an error in your configuration, and to verify the previous steps. If it says it’s an unrecognized command, make sure your environment variables are set.
Run the following from the command line to make sure you have the most up to date version of PEAR.
Next step is to have pear see the site, or channel, where we will be downloading the code for PHPUnit. Here we’ll go to the command line and type in the following commands:
pear channel-discover pear.PHPUnit.de
pear channel-discover pear.symfony-project.com
This should give you a message that the discovery of these channels is successful. If not, check to make sure you can access the sites that you are attempting to discover. If successful, these steps will not be needed again.
Now to install PHPUnit itself. Type the following line into the command prompt:
pear install PHPUnit/PHPUnit
Now that PHPUnit is installed, it’s time to configure some items before we take it for a spin. Navigate to C:\wamp\bin\php\php5.3.0 and look for a file named PHPUnit.bat. This is what is executed every time you run PHPUnit from the command line, but even if you run it from the browser, it’s good to have just in case. In the file, make sure that the exact path to php.exe is set, like so:
%PHPBIN% “C:\wamp\bin\php\php5.3.0\PHPUnit” %*
Changes from the default are in bold.
Also we will need to set the command line php.ini file to be able to use xdebug. For this we just copy the xdebug data that we inserted into the php.ini file previously into this one, which can be usually found in c:\wamp\bin\php\php5.3.0\php.ini.
Now we go to a new command line window and type in PHPUnit. If you get a big long list of flags that you could use, congrats it’s now ready to go! Next we’ll get some use out of it, which we’ll explain in detail in the following section.