Installing and Using PHPUnit with MAMP

Unit testing is becoming more and more popular in every day web development. I decided to see what all the fuss was about. So here's a quick tutorial on getting PHPUnit up and running with MAMP on OSX.

First you need to grab a PHPUnit release. You need a command line utility called pear-phpunit to run tests and also the PHP files that make up the framework. I recommend first doing an svn export somewhere to get the command line script and then doing another export to just export the framework library.

MAMP's default include path is /Applications/MAMP/bin/php5/lib/php and the Pear installation is there already so that's where you should install PHPUnit. So, let's start by grabbing the PHPUnit Framework:

$ cd /Applications/MAMP/bin/php5/lib/php
$ svn export svn://svn.phpunit.de/phpunit/phpunit/branches/release/3.3/PHPUnit
# ...
# Exported revision 4387.

You can use their Trac Browser to decide which release you want to work with. If you ever want to upgrade, you can just delete this folder and export a different release.

In the resulting folder, open PHPUnit/Util/Fileloader.php and do a find and replace for @php_bin@ with the path to your PHP command-line interpreter which for php5 in MAMP should be /Applications/MAMP/bin/php5/bin/php.

Next export or download the command line script:

svn export svn://svn.phpunit.de/phpunit/phpunit/branches/release/3.3/pear-phpunit

Open pear-phpuint and replace @php_bin@ with /Applications/MAMP/bin/php5/bin/php (same as the earlier file).

Next, copy the pear-phpunit executable to a location that exists in your unix PATH environment variable. My PATH already included a /usr/local/bin and I had installed a different version of Pear there earlier so I went ahead and moved it there (you may have to create a directory or put it in another directory in your path). At this point you should rename the file to "phpunit" as well. You can accomplish this via command line or (as I like to sometimes do) go to Finder > Go > Go To Folder... and type in /usr. This will allow you to use the OS GUI to copy the file over and rename it (providing you enter your password). Now you have to make the file executable to be able to run it:

sudo chmod +x /usr/local/bin/phpunit

So at this point you should have:

  • a) The PHPUnit framework in /Applications/MAMP/bin/php5/lib/php/PHPUnit
  • b) The phpunit executable moved, renamed and chmod'ed in /usr/local/bin/phpunit

If everything worked correctly, you should be able to type "phpunit" anywhere and see the "useage" information. I used the following link to set this up, so refer to that if you have any other troubles.

Here's my first couple tests for testing the Json export class in the in-progress Asra 2.0. Though I would not see myself doing this for "normal" websites, I can see how useful it is in application development. I already found a missing required class and a possible bug in just creating this simple test case.

<?php

/**
* test setup ~ /tests/Tests.php
*/
require_once dirname(__FILE__) . '/../../../Tests.php';

/**
* @see Asra.Export
*/
require_once 'Asra/Export/Data.php';
require_once 'Asra/Export/Format/Json.php';

/**
* @see PHPUnit_Framework
*/
require_once 'PHPUnit/Framework.php';

class Asra_Export_Format_JsonTest extends PHPUnit_Framework_TestCase
{
    /**
     * assert that export returns json and is valid json
     */
    public function testExportData()
    {
        $array = array("hello, world!", "hello, again!");
        $data = new Asra_Export_Data($array);
        $format = new Asra_Export_Format_Json($data);
        $this->assertNotNull($format->export());
        $this->assertNotNull(json_decode($format->export()));
    }

    /**
     * assert that passing a non-array to format throws an exception
     */
    public function testDataNotArray()
    {
        //$this->setExpectedException('Asra_Export_Exception');
        $data = "This is not an array!";
        try
        {
            $format = new Asra_Export_Format_Json($data);
        }
        catch (Asra_Export_Exception $e)
        {
            return;
        }
        $this->fail("An expected exception has not been raised.");
    }
}
July 20th, 2009 | Permalink