Saturday, 16 April 2011

Installing PHPUnit On Windows

This section includes the installation of PHPUnit on windows. PHPUnit is testing framework for PHP. So lets start with the procedure :
  •  Download and Install WAMP.
  •  Once you have installed WAMP on your machine, open up the command prompt and go to your php directory in WAMP. Suppose Wamp is installed in C drive.       
          C:\> cd wamp\bin\php\php5.2.8
  • From here you must run the go-pear.bat file to install PEAR and all the files needed for it.
          C:\wamp\bin\php\php5.2.8>go-pear.bat
  •  When executing above command, it will ask you a series of question to set itself up correctly, if you don’t know what to do, then just accept the defaults. But if you are using more than one version of PHP, then select “local” in [system/local] wide copy configuration.
  •  It’ll install pear 1.7.2. If you want to install PHPUnit 3.5, that package is available with PEAR 1.9.1 only. So upgrade it to higher version. To upgrade any pear, Use following command :

          C:\wamp\bin\php\php5.2.8>pear upgrade pear

          You can check the information of pear from any of the command.

          C:\wamp\bin\php\php5.2.8>pear info pear
          C:\wamp\bin\php\php5.2.8>pear -V

  •             Once installed you must run the PEAR_ENV.reg which will create the environment variables for the user, so that PEAR can be called in any directory on the command line.

      C:\wamp\bin\php\php5.2.8>PEAR_ENV.reg     
          If you cannot call PEAR from the command prompt, then you must manually add 
          directory to the path in the System Environment Variables list, then add the directory
         "C:\wamp\bin\php\php5.2.8" to the 'PATH' & 'INCLUDE_PATH' variable. Or double click on the file 
         "PEAR_ENV.reg" in PHP folder, it'll register the environment variables in system. 

  • Once you have PEAR setup, then you must register the PHPUnit channel with PEAR.
    C:\wamp\bin\php\php5.2.8>pear channel-discover pear.phpunit.de
    C:\wamp\bin\php\php5.2.8>pear channel-discover components.ez.no
    C:\wamp\bin\php\php5.2.8>pear channel-discover pear.symfony-project.com

  • Now you can use the PEAR to install packages from the PHPUnit channel.
                C:\wamp\bin\php\php5.2.8>pear install --alldeps phpunit/PHPUnit

                You can check the version of PHPUnit with:
          C:\wamp\bin\php\php5.2.8>phpunit --version

          It’ll download and install PHPUnit package 1.3.1 that is compatible with PHP 4. 
          So to download package PHPUnit 3.5 we need to install it forcefully using command:

    C:\wamp\bin\php\php5.2.8>pear install -f  phpunit/PHPUnit

          Note: -f is used for forcefully installation of package

  • Now your test environment is set and you can start writing testcases
  • If you want to run php or phpunit from any of the location, make sure that "path" and "include_path" environment variables are properly set to php directory.

15 comments:

  1. Hi,

    I have installed wampserver 2.2 and I don't found any "go-pear.bat" file there in "php/bin" directory.

    Do I need to install it externally or is there configuration setting available in PHP.ini file?

    Thanks

    ReplyDelete
    Replies
    1. If it's not there, then you can externally install it from pear site as Pear is required for PHPUnit.
      You can download go-pear.phar file from http://pear.php.net/go-pear.phar. Put it into php/bin directory and then run it

      Delete
    2. This comment has been removed by the author.

      Delete
    3. try this one
      http://www.viper007bond.com/2012/08/21/installing-phpunit-on-windows/

      Delete
  2. Using

    C:\wamp\bin\php\php5.2.8>pear channel-discover pear.symfony-project.com

    totally failed to install phpunit.

    I used

    C:\wamp\bin\php\php5.2.8>pear channel-discover pear.symfony.com

    and everything worked fine.

    Thanks for the info!

    ReplyDelete
    Replies
    1. It may be that pear.symfony-project.com channel may be down/out of service now, otherwise it was in use in past.

      Delete
    2. http://stackoverflow.com/questions/12536668/how-do-i-correctly-install-phpunit-with-pear

      i tried this and worked for me, when C:\wamp\bin\php\php5.2.8>pear channel-discover pear.symfony-project.com got error

      Delete
  3. Why bother with PEAR when you can use Composer?

    ReplyDelete
    Replies
    1. Support for Composer and PHP Archive (PHAR) was added in PHPUnit 3.7 (and is known to be stable since PHPUnit 3.7.5). Earlier releases of PHPUnit are not available through these distribution channels.
      If you are using >=3.7 version, you can use anyone PEAR or composer.

      Delete
  4. After I install phpUnit, and check the version, I get this error
    did the searching a solution, some said about config incude_path to PEAR
    but I already did that on php.ini, nothing happen
    kind of don't know which file do I have to config?

    C:\wamp\bin\php\php5.2.17_vc6_32bit>phpunit --version

    Warning: require_once(File/Iterator/Autoload.php): failed to open stream: No suc
    h file or directory in C:\wamp\bin\php\php5.2.17_vc6_32bit\PEAR\PHPUnit\Autoload
    .php on line 45

    Fatal error: require_once(): Failed opening required 'File/Iterator/Autoload.php
    ' (include_path='.;C:\php5\pear') in C:\wamp\bin\php\php5.2.17_vc6_32bit\PEAR\PH
    PUnit\Autoload.php on line 45

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. hello,
    there is no pear-go.bat file in php5.4.12 folder ...
    please help me

    ReplyDelete