Henrik Volckmer | I solve things.

Mar/12

19

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:

  • phpunit
  • php-code-coverage
  • php-file-iterator
  • php-text-template
  • php-timer
  • php-token-stream
  • phpunit-mock-objects
Unzip all of it so your directory structure looks like this:

Your PHPUnit folder should look like this.

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.

Changing phpunit.bat

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"

Final Steps

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.

· · · ·

  • Vasudev

    If you are using “WAMPserver”:

    then change content of phpunit.bat to

    @echo off
    set PHPBIN=”D:\wamp\bin\php\php5.3.8\php.exe”
    D:\wamp\bin\php\php5.3.8\php.exe -d safe_mode=Off “D:\wamp\bin\php\php5.3.8\phpunit.php” %*

    and add that .bat file to windows\system32 folder

  • Aki

    Thanks for this article. It works for me but only in the case when I add PHPUnit folder to include_path in php.ini. You probably should mention it!

  • Gaurav

    Great, works flawlessly.

    Thanks for sharing.

  • Andrew

    Thank you so much! I spend a half day to solve this task before stuck to your post.

  • http://rentalum.com/ Pasha

    Thank you very much! It worked with few minor corrections including advice from Vasudev’s comment.

  • Giuseppe Romano

    PHP Warning: require_once(File/Iterator/Autoload.php): failed to open stream: No such file or directory in C:phpPHPUnitCodeCoverageAutoload.php on line 50

    Warning: require_once(File/Iterator/Autoload.php): failed to open stream: No such file or directory in C:phpPHPUnitCodeCoverageAutoload.php on line 50

    PHP Fatal error: require_once(): Failed opening required ‘File/Iterator/Autoload.php’ (include_path=’.;C:phppear’) in C:phpPHPUnitCodeCoverageAutoload.php on line 50

    Fatal error: require_once(): Failed opening required ‘File/Iterator/Autoload.php’ (include_path=’.;C:phppear’) in C:phpPHPUnitCodeCoverageAutoload.php on line 50

  • Giuseppe Romano

    SOLVED: I changed php.ini file in my PHP installation dir (C:phpphp.ini) instead that in WINDOWS dir (C:Windowsphp.ini). Make sure of move phpunit.bat in your PHP installation dir(C:php in my case), before run it.

  • Giuseppe Romano

    For working with Yii framework (1.1.14 in my case):

    - copy file Timer.php in C:phpPHPUnit dir

    - download phpunit-selenium repository from https://github.com/sebastianbergmann then unzip all files from phpunit-selenium-masterPHPUnitExtensions in C:phpPHPUnitExtensions

    - open YiiRootframeworktestCWebTestCase.php, than change line 12 so:
    require_once(‘Extensions/SeleniumTestCase.php’);

    -open YiiRootframeworktestCTestCase.php, than change line 11 and 12 so:
    require_once(‘Runner/Version.php’);
    require_once(‘Util/Filesystem.php’);

<<

>>

Theme Design by devolux.nh2.me