Wednesday 28 November 2012

configure phpunit with xampp, netbean

step 1
Download the latest version of XAMPP (zip) and extract it to C:\xampp.

step 2
Download and install the free version of NetBeans for PHP development.

step 3
Add C:\xampp\php\ to your environment PATH.

step 4
Fire up a windows command prompt as an administrator and run the command “pear -V” to see the version of PEAR installed with XAMPP and to verify that step 3 worked. If all is successful, you will see a pear version 1.8.1 or higher and php 5.3.0 or higher.

C:\xampp\php>pear -V
PEAR Version: 1.8.1
PHP Version: 5.3.0
Zend Engine Version: 2.3.0
Running on: Windows NT VAIO 6.1 build 7100 ((null)) i586

If you see a message stating unknown command, repeat [step 3] and reboot your system, than try this step again.

step 5
Fire up a windows command prompt as an administrator and install PHPUnit via the following commands:

pear channel-discover pear.phpunit.de
pear install --alldeps phpunit/PHPUnit


If you get an error during this installation it is likely that your PEAR version was less than 1.8.1 and you’ll need to upgrade it before proceeding. The PEAR upgrade can be tricky on windows, and I found installing the latest version of XAMPP to be easier. In theory though, this is how to upgrade an existing PEAR installation:

pear upgrade pear 


if you get error like 

ERROR: failed to mkdir C:\php\pear\docs\Archive_Tar\docs
ERROR: unable to unpack C:\Users\Home539\AppData\Local\Temp\pear\download\Structures_Graph-1.0.4.tgz
ERROR: failed to mkdir C:\php\pear\docs\PEAR

then fire  command

mkdir c:\php
pear upgrade pear


or alternative

Try installing pear freshly. Download and save this as go-pear.php under c:\xampp\php and run 'php go-pear.php' from there. This should work.

To verify that PHPUnit is installed, run the command “phpunit –version”. You should see version 3.4.3 or higher installed.

step 6
Now let’s make some changes to the php.ini file in c:\xampp\php\. These are the lines you’ll need to un-comment in php.ini (by removing the semicolon).

zend_extension = "\xampp\php\ext\php_xdebug.dll"
xdebug.remote_enable = 0
xdebug.remote_handler = "dbgp"
xdebug.remote_host = "localhost"
xdebug.remote_port = 9000

step 7
Start or re-start Apache and verify that xdebug is turned on by visiting the XAMPP home page at http://localhost and clicking on the phpinfo() link. Xdebug status will be further down on the phpinfo page and it should read version 2.0.5 or higher.

step 8
Now, with Apache running, fire up NetBeans 6.7. Click Tools->Options and ensure that the path to your phpunit.bat (created during the PHPUnit installation) in the PHPUnit Script field is valid. Also verify that your the Session ID is set tonetbeans-xdebug and the Debugger Port is 9000. Then click OK.

Step 9
(Optional) Create a test project and a class named “Calculator” with the contents below:

<?php
class Calculator
{
    /**
     * @assert (0, 0) == 0
     * @assert (0, 1) == 1
     * @assert (1, 0) == 1
     * @assert (1, 1) == 2
     * @assert (1, 2) == 4
     */
    public function add($a, $b)
    {
        return $a + $b;
    }
}
?>

Save the class file as Calculator.php, right-click it in your project browser and choose Tools->Create PHPUnit tests. You’ll be asked to specify a directory for said unit tests.

Notice that a new CalculatorTest.php file has been generated with a unit test for each @assert annotation in the Calculator.php test class.

step 10
You can now right-click the Calculator.php class file and choose Test to perform the generated unit tests on the class. Notice how the last test failed since we asserted that 1 + 2 = 4 (which is not true).

No comments:

Post a Comment