Installing PHPUnit on Windows without PEAR
Since PHP 5.4 came out just recently I’ve decided to upgrade my PHP installation a little as far as my desktop is concerned. While many academics and avid coders will have a machine primarily running Linux for development of new code, I often prefer using the machine I also use for PC gaming to do some light coding. I’ve got a basic WAMP stack going on it, but it lacks some stuff like PHPUnit, Xdebug, etc. Tonight I’ve got PHPUnit working, all without installing PEAR, and I’m going to show you how to do it. Special thanks to Nick Bartlett’s slightly dated tutorial for pointing me in the right direction. Since it was written almost two years ago, it doesn’t have any guidance on getting the necessary packages PHPUnit is now dependent on.
Getting the necessary files
The first thing you need to do is get all the files required by PHPUnit. You’ll want to go to Sebastian Bergmann’s github page here: https://github.com/sebastianbergmannGet zips of the following repositories:
You may have to remove some parent folders from the individual packages, but thanks to drag and drop that should be a piece of cake.
With PHPUnit for windows you’ll also have a phpunit.bat file lying around in the archive. If you don’t, that’s fine. Here is the complete contents that you want the file to have:
@echo off set PHPBIN="C:\PHP\php.exe" php -d safe_mode=Off "C:\PHP\phpunit.php" %*
If the file doesn’t look like that, change it so it does. Make sure the path to php.exe is correct and that a copy of phpunit.php is in the same directory.
Changing autoload.php (phpunit)
Replace the require_once segment beginning at line 45 with the following:
require_once 'File/Iterator/Autoload.php'; require_once 'CodeCoverage/Autoload.php'; require_once 'Timer/Autoload.php'; require_once 'Framework/MockObject/Autoload.php'; require_once 'Text/Template/Autoload.php';
Changing autoload.php (codecoverage)
Similarly, change the Autoload.php file in CodeCoverage to have this:
require_once 'File/Iterator/Autoload.php'; require_once 'Token/Stream/Autoload.php'; require_once 'Text/Template/Autoload.php';
You know the place.
Add PHPUnit to php.ini
Open up php.ini in your favorite editor and search for “Paths and Directories”. Include your phpunit folder under include_path. Mine looks like this:
; Windows: "\path1;\path2" include_path = ".;c:\php\includes;c:\php\phpunit"
At this point all your files should be set up correctly. Now there are just a couple more things to do:
- Run phpunit.bat
- Test phpunit by opening up a command line prompt and typing “phpunit”.
If there are any errors it should be pretty straightforward to fix the filepaths at this point. If this little tutorial is inaccurate in any way, please let me know in the comments below.
Update: As Aki has pointed out in the comments below, you may need to include your phpunit folder in the PHP include path. This tutorial has been updated to reflect that.