Thursday, July 14, 2011

Install PHPUnit on Windows

Install Steps:


Windows:
-------------

1. PHP Install:

    1.1 Precautions:  Before installing, first check is php available in your system. If so either delete or continue with existing one.
    1.2 Check system | enviroment variables, is path or variables are set, either delete or continue with same.
     1.3 NowiInstall PHP [ visit site - http://windows.php.net/downloads/releases/ download either msi or zip package.  Preferably in C:\php folder]

2. PEAR install:

     2.1  Precautions:  First check in your system, PEAR already exists.  If so continue or delete.
            If u r deleting means, need to delete all system environment variable those are set earlier.

     2.2  Open cmd prompt, goto php folder & install PEAR package.
             C:\php>go-pear.bat

    This will install the pear package, but some times for the packages latest PEAR version might be needed.  So visit the site, http://pear.php.net/package/PEAR/download to check the latest versions. & then type command as mentioned in the site.

        2.3  While installing, pear.bat will prompt to know the location for installing.
                 Carefully set the locations, by default choose C:\php

        2.4  To know, PEAR is install successfully, type in cmd prompt, C:\php> pear -config
               It displays all config settings & commands.

3. PHPUnit Install:

      3.1  Check PHPUnit package already exists in C:\php\PEAR folder.  If not, proceed as below.
   
      3.2  Before installing PHPUnit, need to initialize the channels(websites) from which dependency packages are to be downloaded.  To do this,

              C:\php> pear update-channels or pear channel-update      ; it will update all channels.
               OR
              discover each channel as below

                  pear channel-discover pear.phpunit.de
                  pear channel-discover pear.phpunit.com
                  pear channel-discover pear.symfony-project.com
                  pear channel-discover components.ez.no

       3.3  Now install PHPUnit.   C:\php> pear install phpunit/PHPUnit
               make sure u will get the message - install ok:  channel:<>/PHPUnit<version>
               If you did not get this message, instead any error message means, follow them carefully & try to succeed in installing the PHPUnit.
               After installing successfully, to check phpunit is installed correctly, then try to locate the file phpunit.bat in C:\php folder. If exists means, it is installed correctly.
               

                Example of errors i faced & fixed are:

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




C:\php>pear install phpunit/PHPUnit
Failed to download pear/HTTP_Request2 within preferred state "stable", latest re
lease is version 2.0.0RC1, stability "beta", use "channel://pear.php.net/HTTP_Re
quest2-2.0.0RC1" to install
phpunit/PHPUnit can optionally use PHP extension "curl"
phpunit/PHPUnit can optionally use PHP extension "dbus"
pear/XML_RPC2 requires package "pear/HTTP_Request2" (version >= 0.6.0)
phpunit/PHPUnit requires package "pear/XML_RPC2"
No valid packages found
install failed






C:\php>pear channel-discover pear.php.net
Channel "pear.php.net" is already initialized


C:\php>pear install HTTP_Request2-2.0.0RC1
Failed to download pear/Net_URL2 within preferred state "stable", latest release
 is version 0.3.1, stability "beta", use "channel://pear.php.net/Net_URL2-0.3.1"
 to install
pear/HTTP_Request2 requires package "pear/Net_URL2" (version >= 0.3.0)
pear/HTTP_Request2 can optionally use PHP extension "curl"
pear/HTTP_Request2 can optionally use PHP extension "fileinfo"
pear/HTTP_Request2 can optionally use PHP extension "openssl"
No valid packages found
install failed


C:\php>pear install NET_URL2
Failed to download pear/NET_URL2 within preferred state "stable", latest release
 is version 0.3.1, stability "beta", use "channel://pear.php.net/NET_URL2-0.3.1"
 to install
install failed






C:\php>pear install NET_URL2-0.3.1
downloading Net_URL2-0.3.1.tgz ...
Starting to download Net_URL2-0.3.1.tgz (8,488 bytes)
.....done: 8,488 bytes
install ok: channel://pear.php.net/Net_URL2-0.3.1


C:\php>pear install HTTP_Request2-2.0.0RC1
pear/HTTP_Request2 can optionally use PHP extension "curl"
pear/HTTP_Request2 can optionally use PHP extension "fileinfo"
pear/HTTP_Request2 can optionally use PHP extension "openssl"
downloading HTTP_Request2-2.0.0RC1.tgz ...
Starting to download HTTP_Request2-2.0.0RC1.tgz (95,353 bytes)
.....................done: 95,353 bytes
install ok: channel://pear.php.net/HTTP_Request2-2.0.0RC1


C:\php>pear install phpunit/PHPUnit
phpunit/PHPUnit can optionally use PHP extension "curl"
phpunit/PHPUnit can optionally use PHP extension "dbus"
downloading PHPUnit-3.5.14.tgz ...
Starting to download PHPUnit-3.5.14.tgz (118,697 bytes)
..........................done: 118,697 bytes
downloading XML_RPC2-1.1.1.tgz ...
Starting to download XML_RPC2-1.1.1.tgz (68,431 bytes)
...done: 68,431 bytes
install ok: channel://pear.php.net/XML_RPC2-1.1.1
install ok: channel://pear.phpunit.de/PHPUnit-3.5.14


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4. Write & Execute PHP unit tests.

       4.1 Open any editor & write php test code.  Example:

             <?php
require_once 'PHPUnit/Framework.php';

class StackTest extends PHPUnit_Framework_TestCase
{
    public function testPushAndPop()
    {
        $stack = array();
        $this->assertEquals(0, count($stack));

        array_push($stack, 'foo');
        $this->assertEquals('foo', $stack[count($stack)-1]);
        $this->assertEquals(1, count($stack));

        $this->assertEquals('foo', array_pop($stack));
        $this->assertEquals(0, count($stack));
    }
}
?>            

   4.2 Save file as Class name, StackTest.php in the c:\php folder.

   4.3  Run the test, C:\php> phpunit StackTest.php, will get result as below:

            PHPUnit 3.5.14 by Sebastian Bergmann.
Time: 1 second, Memory: 3.00Mb

OK (1 test, 5 assertions)




5. Explore more about testing, assertions -  http://www.phpunit.de/manual/3.4/en/writing-tests-for-phpunit.html



             
     
           

   
       

No comments:

Post a Comment