Installing PHPUnit in wamp in Windows7

Installing PEAR package manager

Add php directory to your Windows’ Environment Variables PATH, if it is not already added. My wamp php path is “C:\wamp\bin\php\php5.3.13”. Check Adding to Environment Variables Path if you are not sure how to add a value to Environment Variables Path in Windows 7.

Download http://pear.php.net/go-pear.phar to some location in your system. I downloaded it to C:\wamp\bin\php\php5.3.13

Open a new command prompt as an administrator and cd to the directory where go-pear.phar is placed. I did:

cd c:\wamp\bin\php\php5.3.13

and ran

php go-pear.phar

It will ask you for install as option for which i chose “local” which is recommended. Follow the rest of process and it should install pear in a matter of few seconds.

Installing PHPUnit

Open a new command prompt as an administrator and run:

pear config-set auto_discover 1
pear install pear.phpunit.de/PHPUnit

Specify your in use php.ini file so the PHPUnit could make changes to it. Follow the rest of process normally and it will install the PHPUnit.

Running test case in command prompt

Create a php file test.php with the following code and place it at server root i.e. C:\wamp\www or similar:

<?php
class MyTest extends PHPUnit_Framework_TestCase
{
public function testOneEqualsOne()
{
$this->assertEquals(1, 1);
}
}

?>

Now open the command prompt as an administrator and run:

$ cd c:/wamp/www 
$ phpunit test.php

It should output the test results as:

PHPUnit 3.6.11 by Sebastian Bergmann.

.

Time: 1 second, Memory: 3.50Mb

OK (1 test, 1 assertion)

Running test case in CakePHP

NOTE: This is tricky.. This comes into play when you try to run tests through your web browser, for example running CakePHP unit tests. As the wamp installer had created two copies of php.ini file, one placed at C:\wamp\bin\apache\apache2.2.22\bin and another at C:\wamp\bin\php\php5.3.13\. In my case, the pear installer detected php.ini file placed at the later location and made changes to it. I guess it might have asked me to choose php.ini file while installing but i ignored it and it chose the file it suggested and thus the following changes to add pear to include_path were made to C:\wamp\bin\php\php5.3.13\php.ini file.

;***** Added by go-pear
include_path=".;C:\wamp\bin\php\php5.3.13\pear"
;*****

Make sure that you had chosen the right file. Double check the ini.php file used by wampserver. If the instaler didn’t make changes to php.ini file used by wampserver you need to copy these changes. Just copy these three lines of code to C:\wamp\bin\apache\apache2.2.22\bin\php.ini file and restart the wamp server.

Visit an url of CakePHP application while debug mode set to greater than 0. For example:

http://mysite.localhost/test.php

And it should show the test results.

Leave a Reply