How to install PHPUnit on Windows

11

After about ten hours of fighting with PEAR and PHPUnit, I think I have it installed correctly as my test case was able to run. There are a couple “official” documentation sources on how to install PHPUnit, but they only bother to cover installing it on linux. Further, PHPUnit has evolved quite a bit over the past few years and its configuration seems to have changed. I wouldn’t even bother installing PEAR if you don’t want to because the PHPUnit version from PEAR is grossly outdated. As of now, the currently version of PHPUnit is 3.4.9. If you install pear and phpunit from the command line as I tried a couple times, the phpunit.bat file will not be updated as it should, so phpunit will not work. If you open phpunit.bat and see these lines “set PHPBIN=”@php_bin@”
%PHPBIN% “@bin_dir@\phpunit” %*”, then you know something didn’t work right.

  1. To install manually as I did, go to http://pear.phpunit.de/get/ to download the latest version of PHPUnit. Extract the files to a directory that is listed in the include_path of your php.ini configuration file. If you’d like to place it in a custom location, add that location to the include_path in your php.ini file.
  2. Find the phpunit.bat file that you extracted, open it with a text editor and change the @php_bin@ string in it with the path to php.exe. Get rid of this line “%PHPBIN% “@bin_dir@\phpunit” %*” as it won’t be needed. On the line below, add the following line to execute phpunit.php with php ‘php -d safe_mode=Off “C:\Program Files (x86)\Zend\Apache2\htdocs\library\pear\pear\phpunit.php” %*’. Remove the single quotes I added here and change the path to phpunit.php to the file’s location on your computer (the directory you extracted phpunit to).

In the end, your phpunit.bat file should look similar to this (with that paths to php.exe and phpunit.php changed to your file locations)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@echo off
REM PHPUnit
REM
REM Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
REM All rights reserved.
REM
REM Redistribution and use in source and binary forms, with or without
REM modification, are permitted provided that the following conditions
REM are met:
REM
REM   * Redistributions of source code must retain the above copyright
REM     notice, this list of conditions and the following disclaimer.
REM 
REM   * Redistributions in binary form must reproduce the above copyright
REM     notice, this list of conditions and the following disclaimer in
REM     the documentation and/or other materials provided with the
REM     distribution.
REM
REM   * Neither the name of Sebastian Bergmann nor the names of his
REM     contributors may be used to endorse or promote products derived
REM     from this software without specific prior written permission.
REM
REM THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
REM "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
REM LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
REM FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
REM COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
REM INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
REM BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
REM LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
REM CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC
REM LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
REM ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
REM POSSIBILITY OF SUCH DAMAGE.
REM
 
set PHPBIN="C:\Program Files (x86)\Zend\ZendServer\bin\php.exe"
php -d safe_mode=Off "C:\Program Files (x86)\Zend\Apache2\htdocs\library\pear\pear\phpunit.php" %*

To test your install, go to the command line on windows and type “phpunit” and hit enter. If you don’t see any errors, then it’s installed correctly.

Other Issues
Using Zend Server 5.0, Zend framework 1.10, and PHPUnit 3.4.9, I had to apply a couple patches or bug fixes to get things to work.

  1. I received the error “Fatal error: Cannot redeclare class PHPUnit_Framework_TestCase” . The Zend Studio template for a test case automatically includes
    1
    
    require_once 'PHPUnit\Framework\TestCase.php'; .

    Change this to

    1
    
    require_once 'PHPUnit\Framework.php';

    for each test case. The bug report for this issue is located at http://framework.zend.com/issues/browse/ZF-9380

Posted in: Development, PHP
Tags:

This article has 11 comments

  1. dan n 08/11/2010, 8:12 pm:

    Thanks much for this post!

    I’d been bumbling with PHPUNIT setup on windows for over 2 days myself.

    That 2nd line in the bat file is JUST what I needed. Kudos to you! Go Farve!

    D

  2. ncorpse 08/13/2010, 5:30 pm:

    Hi Nick
    This has been really useful to me.
    After reading through various linux related tutorials and struggling with the official pear-version I finally got it working

  3. Mike 11/21/2010, 9:39 pm:

    Thank you for this article, I have finally installed phpunit on windows :) Thank you, once again!

  4. gilad 02/09/2011, 10:42 am:

    hi,
    when i press phpunit i get this error:
    PHP Warning: require_once(PHP/CodeCoverage/Filter.php): failed to open stream:
    No such file or directory in C:\wamp\www\trackstar\PHPUnit-3.5.10\PHPUnit-3.5.10
    \phpunit.php on line 38
    PHP Stack trace:
    PHP 1. {main}() C:\wamp\www\trackstar\PHPUnit-3.5.10\PHPUnit-3.5.10\phpunit.ph
    p:0

    Warning: require_once(PHP/CodeCoverage/Filter.php): failed to open stream: No su
    ch file or directory in C:\wamp\www\trackstar\PHPUnit-3.5.10\PHPUnit-3.5.10\phpu
    nit.php on line 38

    Call Stack:
    0.0007 328552 1. {main}() C:\wamp\www\trackstar\PHPUnit-3.5.10\PHPUnit
    -3.5.10\phpunit.php:0

    PHP Fatal error: require_once(): Failed opening required ‘PHP/CodeCoverage/Filt
    er.php’ (include_path=’.;C:\wamp\www\trackstar\PHPUnit-3.5.10\PHPUnit-3.5.10\PHP
    Unit’) in C:\wamp\www\trackstar\PHPUnit-3.5.10\PHPUnit-3.5.10\phpunit.php on lin
    e 38
    PHP Stack trace:
    PHP 1. {main}() C:\wamp\www\trackstar\PHPUnit-3.5.10\PHPUnit-3.5.10\phpunit.ph
    p:0

    Fatal error: require_once(): Failed opening required ‘PHP/CodeCoverage/Filter.ph
    p’ (include_path=’.;C:\wamp\www\trackstar\PHPUnit-3.5.10\PHPUnit-3.5.10\PHPUnit’
    ) in C:\wamp\www\trackstar\PHPUnit-3.5.10\PHPUnit-3.5.10\phpunit.php on line 38

    Call Stack:
    0.0007 328552 1. {main}() C:\wamp\www\trackstar\PHPUnit-3.5.10\PHPUnit
    -3.5.10\phpunit.php:0

  5. Saul 02/14/2011, 10:17 am:

    Things have changed in the mean time – the download site is currently at http://pear.phpunit.de/

    In my case I also had to install File_Iterator and PHP_CodeCoverage packages in addition to PHPUnit.

  6. Mukatale 03/16/2011, 1:09 am:

    Hi guys, but seriously, it doesn’t work for me. When I rum cmd and type phpunit, nothing happens, meaning the command is being recognised but its not executing anything. Then when I change it abit, it kind of executes the phpunit.php but it only displays errors.

  7. Rory 08/10/2011, 2:14 pm:

    You rock! I’ve hacked with that bat file and gave up. What you did worked great – the first time! Thanks.

  8. Vijay 08/12/2011, 1:12 am:

    Hi,

    I tried what you told in the tutorial and i downloaded the PHPUnit version 3.5.14 and uploaded into my E:\xampp\php\PEAR folder and i changed the phpunit.bat as you told. And when i go the cmd line and typed phpunit it showed the following error

    ‘phpunit’ is not recognized as an internal or external command,operable program or batch file.

    but when i go the path E:\xampp\php\PEAR\ and then type phpunit it didn’t showed any error. So i thought it was installed and tried to create a sample file and run , I created a file name UnitTest.php under the folder E:\xampp\php\PEAR and tried to run as “phpunit UnitTest” and also tried “phpunit UnitTest UnitTest.php” but it also didn’t print anything!! Why?

  9. Jean-Pierre Schnyder 02/25/2012, 5:04 pm:

    I have been wrestling with pear for days now. But it looks I finally found the silver bullet: when pear fails, saying for example

    No releases available for package “pear.phpunit.de/PHPUnit”
    install failed

    or displaying crap ending with – Possible symlink attack error

    just do

    pear clean-cache and then, “magically” pear does what it is supposed to do !

Trackbacks/Pings

Leave a Reply

Your email address will not be published. Required fields are marked *

*




You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">