October 18th, 2012 Add Your Comments

The de facto standard unit testing framework for PHP is PHPUnit. You can find it here (GitHub repository). Even if the documentation explains how to install it on your system, it may not be as self-explanatory as it looks. Here is a little how-to for Mac OSX.

Get PEAR.
Open a Terminal window and type :

curl http://pear.php.net/go-pear.phar > go-pear.phar
This will copy the file we need to install PEAR on your local hard drive, in your User directory.

Install PEAR.
Still in Terminal, type :

sudo php go-pear.phar
Change the installation path (option 1) to

/usr/local
and launch installation.
After installation has completed, you can safely delete the go-pear.phar file.

Tell PHP CLI where PEAR is.
By default, PHP CLI (Command Line Interface) php.ini does not exist, but a template is available on the system. We have to copy and edit it. Type :

sudo cp /etc/php.ini.default /etc/php.ini
sudo nano /etc/php.ini
Uncomment the include_path line and make it look like this :

include_path = “.:/usr/local/share/pear”
Tell Terminal where PEAR is.
To let Terminal know where is your PEAR path and be able to launch PHPUnit right from the console, type (in the console) :

sudo nano /etc/profile
and add

export PATH=$PATH:/usr/local/bin
to the end of the file.
Save and exit. Close your Terminal window.

Install PHPUnit.
Open a new Terminal window and type :
sudo pear config-set auto_discover 1
sudo pear install pear.phpunit.de/PHPUnit
This will install all needed files for PHPUnit to run.

Enjoy.
Everything should be fine. In your Terminal, type :
phpunit
You should get a list of all available PHPUnit functions. Happy testing !