If you are following Test Driven Development(TDD) in developing a software, you must use a testing framework suitable for your programming language. For PHP one such popular framework is PHPUnit. In this tutorial I describe how to install PHPUnit in Windows.
(Before you proceed please note that the example file paths I use here are valid only if you are using WAMP server. If you are not using WAMP, please use file paths relative to your PHP installation.)
As the first step we have to install PEAR – PHP Extension and Application Repository. PEAR is a distributed system of pre-built packages that can be used to extend the functionality of PHP. Since PHPUnit is one such packages, PEAR should be installed first.
Installing PEAR
- Go to the PHP installation folder. (ex: C:\wampbinphpphp<version>)
- Check if there exists a file called go-pear.phar, if not download it from here.
- Create a new folder named pear inside your php installation folder and copy the downloaded file into that folder.
- Open the command prompt as Administrator and use cd command to change the working directory to the newly created pear folder.
cd C:\wampbinphpphp<version>pear - Run the dowloaded file using the following command
php go-pear.phar - Installer will ask whether you want to install PEAR system wide. Guessing that you have no objections, press ENTER to accept.
- Then it will calculate a list of paths and show them. Check them and if they are OK press ENTER again to continue.
- Installer will finish creating a registry file in your pear folder. The purpose of this file is to add Environment Variables to your system. Therefore go to that directory and execute the reg file named PEAR_ENV.reg by double-clicking on it.
- We have almost finished installing PEAR. But for safety we will add the path to the PEAR to our system path. For that open your PATH variable. For that right click Computer -> Properties -> Advanced system settings and hit the Environment Variables button in windows 7. Or click Settings on Charms Bar -> PC info -> Advanced system settings and hit the Environment Variables button in Windows 8.
- Append the path of the PEAR directory to the PATH variable.
ex: ; C:\wampbinphpphp<version>pear - We have finished installing PEAR. To test whether it installed correctly, restart WAMP. Then type pear in command prompt and press ENTER. If the installation is successful a list of options will appear.
Installing PHPUnit
- Open commad prompt as Administrator and navigate to the PHP installation folder.
(ex: C:\wampbinphpphp<version>) - To make sure that PEAR is updated, execute the follwing command.
pear upgrade-all - Then execute the follwing commads to add several channels.
pear channel-discover components.ez.no
pear channel-discover pear.phpunit.de
pear channel-discover pear.symfony.com - To install PHPUnit, execute the following command.
pear install –alldeps phpunit/PHPUnit - That’s all for installing PHPUnit. As usual to test whether it installed correctly, type phpunit in command prompt and press ENTER. If the installation is successful a list of options will appear.