RoughDraft:PHPUnit

From Tech Elephant

Draft

Contents

[edit] Overview

PHPUnit[1] is a unit testing framework for PHP. It provides a simple API to create a set of tests and provides a means to run those tests in a repeatable fashion. Specifically, PHPUnit allows PHP developers the ability to refactor[2] an API and ensure the core functionality does not break (e.g., regression testing[3]).

[edit] Installing PHPUnit

The easiest way to get started is by installing PHPUnit via PEAR[4].

First, you will need to add the PHPUnit PEAR channel to your local repository:

pear channel-discover pear.phpunit.de

You will see output similar to:

Adding Channel "pear.phpunit.de" succeeded
Discovery of channel "pear.phpunit.de" succeeded

Next, install PHPUnit via PEAR:

pear install -a phpunit/PHPUnit

Note: The -a argument will install the following required and optional dependencies:

  • pear/Image_GraphViz
  • dom
  • pear/Testing_Selenium
  • xdebug

You will see output similar to:

Failed to download pear/Testing_Selenium within preferred state "stable", latest release is version 0.3.1, stability "beta", use "channel://pear.php.net/Testing_Selenium-0.3.1" to install
phpunit/PHPUnit requires PHP extension "dom"
phpunit/PHPUnit can optionally use package "pear/Testing_Selenium" (version >= 0.2.0)
phpunit/PHPUnit can optionally use PHP extension "xdebug" (version >= 2.0.0RC2)
downloading Image_GraphViz-1.2.1.tar ...
Starting to download Image_GraphViz-1.2.1.tar (Unknown size)
........done: 23,040 bytes
install ok: channel://pear.php.net/Image_GraphViz-1.2.1

[edit] Testing your code

[edit] Classes used

HelloWorld:: This is a custom class that should be similar to any basic class you wish to test.

<?php
/**
 * The 'Hello World' class
 */
class HelloWorld
{
    /**
     * The message to say when we 'speak'
     * 
     * @var string
     * @access public
     */
    public $message;

    /**
     * The constructor
     */
    public function __construct()
    {
        $this->message = 'Hello World!';
    }

    /**
     * Speak: say something
     * 
     * @return string
     */
    public function speak()
    {
        $this->message;
    }
}
?>

PHPUnit_Framework_TestCase:: A single object containing (multiple) tests (assertions).

<?php
/**
 * Include the PHPUnit Framework
 */
require_once 'PHPUnit/Framework.php';

/**
 * Include the HelloWorld class
 * This is the class this test will be testing
 */
require_once 'HelloWorld.php'

/**
 * The 'Hello World' PHPUnit Test
 */
class HelloWorldTest extends PHPUnit_Framework_TestCase
{
    protected $helloWorld;

    /**
     * The constructor
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Setup the test
     * This method will be executed for each test* method
     */
    public function setup()
    {
        $this->helloWorld = new HelloWorld();
    }

    /**
     * Tear-down the test
     * This method will be executed for each test* method
     */
    public function tearDown()
    {
        unset($this->helloWorld);
    }

    /**
     * 'Say Hello' test
     */
    public function testSayHello()
    {
        $this->assertTrue(FALSE, 'Hello World!');
    }
}
?>

PHPUnit_Framework_TestSuite:: A single object containing (multiple) tests (PHPUnit_Framework_TestCase).

<?php
/**
 * Include the PHPUnit Framework
 */
require_once 'PHPUnit/Framework.php';

/**
 * Include the TextUI TestRunner, as these tests will be run on the CLI
 */
require_once 'PHPUnit/TextUI/TestRunner.php';

/**
 * Include the test class
 */
require_once 'HelloWorldTest.php';

/**
 * Create a test suite that contains the tests
 * from the HelloWorldTest class.
 */
$testSuite = new PHPUnit_Framework_TestSuite('HelloWorldTest');
 
/**
 * Run the tests from the suite
 */
PHPUnit_TextUI_TestRunner::run($testSuite);
?>

PHPUnit_Framework_TestResult:: A test result (from assertions).

[edit] Designing tests

[edit] Case study

  1. The official PHPUnit site is located at http://www.phpunit.de/
  2. Code Refactoring is any change to a computer program which improves its readability or simplifies its structure without changing its results.
  3. Regression testing is any type of software testing which seeks to uncover regression bugs. Regression bugs occur whenever software functionality that previously worked as desired stops working or no longer works in the same way that was previously planned. Typically regression bugs occur as an unintended consequence of program changes.
  4. [| PEAR] is a shared PHP module library installer