I have been developing with PHP for about a year now, and whenever an unexpected result comes out, I normally do something like echo $var; or var_dump($var, $some_var);. If the error occurred on a production site, I might have remembered to add something of an IP Checker to ensure that it only appeared on my own computer.¬ This approach to testing led to some embarrassing outputs when I uploaded a file to production to find a hidden var_dump(); in some obscure function called once in a while.

About 3 months ago, I knew that there had to be a better approach and I had heard some of my friends who use other languages talk about Unit Testing. I learned that Unit Testing merely provided a set of automated tests for the smallest piece of testable software. This seemed like a miracle, I would save so much time by letting my computer run my code and check rather than me manually checking it in the browser. It seemed like a great idea, and so I started researching similar testing extensions available for PHP. I discovered the automated testing framework PHPUnit. I started trying to figure out how it was supposed to be used, and I got lost in all of the steps, requiring me to use a multitude of different tutorials and gave up. It seemed to me that to get all of these things working I needed to use a *nix machine.

I recently decided to give it another try and now have now figured out how to install PHPUnit on Windows. So this is a guide for all of you wondering how to start testing with PHPUnit on Windows 7. I imagine that the steps required for other versions remain roughly the same.

So first open a command prompt window. And type in the following

php -v

you should see something along the line of

PHP 5.3.4
Copyright 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright 1998-2010 Zend Technologies
    With Xdebug v2.1.0, Copyright 2002-2010, by Derick Rethans

If you have everything installed correctly, if this is the case (it was not for me) you can skip ahead to Installing PEAR. If not and you get a message such as:

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

We need to add the PHP executable file to your system path.

First we need to find your PHP executable file, so open up the start menu and search for php.exe and instead of clicking on it use a right click and open file location and copy the location from the new explorer window.


No head over to Start > Control Panel > System > Advanced > Environment Variable and in the list of system variables scroll down until you see Path and append

;C:\path\to\php

where C:\path\to\php is the location you copied from the previous step. The executable is now in your path, now try opening a new Command Prompt and type in php –v it should work now.

Now we are going to install PEAR. PEAR is “The PHP Extension and Application Repository” it is “a framework and distribution system for reusable PHP components.” PEAR is required to install the required components of PHPUnit. PEAR allows PHPUnit to install all of its dependencies rather than asking you to go out scouting for it. So point your browser to http://pear.php.net/go-pear.phar and save that file somewhere easy to find. Now using a Command Prompt navigate to your downloads folder — for those of you new to the command line interface on windows you can change directory by entering

cd C:\path\to\pear\download

and you can see the files in a directory by typing in dir followed by enter. Once you are at the correct directory type in

php go-pear.phar

followed by enter.

The installer should now be running, it will be asking you a few questions about how you’d like the PEAR Package to be installed. Most of the questions are straightforward (such as if you are doing a system wide install or a local install). After that it will ask you about the file layout for your PEAR installation. These are the preferences that I decided to go with, it is easiest to type in all to edit.

  • Option 1 (alias $prefix): Installation Prefix — This is base location for all of your files for PEAR, I decided to go with something simple C:\pear.
  • Option 4: Binaries Location — Binaries actually mean PHP scripts, shell scripts and so on. This directory will need to go into your systems path variable (using the same procedure I outlined above). I decided to go with the normal convention of a \bin directory for all binaries. So enter $prefix\bin.
  • Option 11: Name of Configuration File —The default location for this is inside your C:\Windows directory. I wanted to move it so that all PEAR related files were in the same directory and did not have to worry about loose files so I entered $prefix\pear.ini. Wherever you decide to place this file, you need to remember that it is looking for the name of a file not just a directory
  • Option 12: Path to CLI php.exe — This is the location for php.exe, we can use the copy and pasted value from above when we added the directory to our path. PEAR auto populates this value with the PATH value from above, but like Option 11 it requires the name of a file so I put in C:\path\to\php\php.exe

We are almost there; the Installer will attempt to modify your php.ini file. If you have WAMP installed there is a good chance that it will auto-detect the incorrect php.ini file, as WAMP does not use the php.ini file located in the php bin but in the Apache Bin. So open up the correct php.ini file, if you have WAMP just click on the icon and navigate to `PHP > php.ini` and at the end you need to insert the following

include_path = ".;c:\pear\lib" ;

Once you finish inserting that, save the file and restart Apache.

Once again navigate to Start > Control Panel > System > Advanced > Environment Variableand in the list of system variables scroll down until you see `Path` and append

;C:\path\to\pear

now we have PEAR installed.

Now we can install PHPUnit, this is when it gets easy. Open up a new command prompt and type in `pear channel-discover pear.phpunit.de`. This tells PEAR to add this channel to its list of PHP projects to search. Now that has to be done only once. Okay we are finally installing PHPUnit, type in

pear install –a –f phpunit/PHPUnit

this will install PHPUnit and all of its dependencies.

Now we can start using PHPUnit to test our PHP Scripts. So I’m copying this test unit’s PHPUnits’ website. The only difference is that instead of `require_once 'PHPUnit/Framework.php';` we need to `require_once "PHPUnit/Autoload.php";`. Now everything should work.

<?php
require_once "PHPUnit/Autoload.php";
class ArrayTest extends PHPUnit_Framework_TestCase {
    public function testNewArrayIsEmpty()  {
        // Create the Array fixture.  $fixture = array();
        // Assert that the size of the Array fixture is 0.
        $this->assertEquals(0, sizeof($fixture));
    }

    public function testArrayContainsAnElement() {
        // Create the Array fixture.
        $fixture = array();

        // Add an element to the Array fixture.
        $fixture[] = 'Element';

        // Assert that the size of the Array fixture is 1.
        $this->assertEquals(1, sizeof($fixture));
    }
}

Now I was trying to test PHPUnit in the browser, but I would see nothing happening. In order to test a PHP script with PHPUnit you need to open a command prompt and type in

phpunit C:\path\to\script

So if I run the test script supplied by PHPUnit I can see that it had no failures. However if I change line 25 to

$this->assertEquals(2, sizeof($fixture));

I get the following message when I run the PHPUnit test.

PHPUnit announces that there was a failed test and describes the error, that we had created. This approach to testing seems to be easier, and in the future I plan on showing you how to write your own tests with PHPUnit.

  • http://teknologi.com.br Leonardo

    Great job about PHPUnit post, if you are behind a proxy, you can type that before the channel-dicover command:
    pear config-set http_proxy http://username:password@yourproxy:80

    Also, I want to know if I can re-publish your post on my blog, translated to portuguese and with credits of your blog.

    Thank-you.

    • Aramael Pena-Alcantara

      Leonardo,

      Absolutley, send me a link when you finish translating.