writing unit tests for php - example 1

In an earlier tutorial (in the amfPHP section), I created an object called Alpha that had 3 methods for testing RPC between Flex and PHP. In this tutorial I will show how you can write tests for your class(es) to ensure that they behave in accordance with what you're expecting.

First you'll want to review the Alpha and AlphaData class found in this tutorial. There are 3 methods in this class, and we're going to write a series of tests that covers all the methods. The code below does just that. I placed this test code in a sub-directory of where the object I want to test is found. I did that for simplicity of demonstrating this tutorial.

AlphaTest.php

<?php
 
require '../Alpha.php';
 
//---------------------------------------------------------
class AlphaTest extends PHPUnit_Framework_TestCase
//---------------------------------------------------------
{
private $alpha;
 
//---------------------------------------------------------
protected function setUp()
//---------------------------------------------------------
{
$this->alpha = new Alpha();
}
 
//---------------------------------------------------------
public function testSimpleCall()
//---------------------------------------------------------
{
$this->assertEquals('Hello World', $this->alpha->simple_call());
} 
 
//---------------------------------------------------------
public function testObjectCall()
//---------------------------------------------------------
{
$this->assertInstanceOf('AlphaData', $this->alpha->object_call());
$this->assertClassHasAttribute('lastName', 'AlphaData');
} 
 
/**
* @depends testObjectCall
*/
public function testObjectCall2()
//---------------------------------------------------------
{
$data = $this->alpha->object_call();
$this->assertEquals('Knigge', $data->lastName);
} 
 
//---------------------------------------------------------
public function testArrayCall()
//---------------------------------------------------------
{
$data = $this->alpha->array_call();
$this->assertInternalType('array', $data);
$this->assertCount(3, $data);
}
 
};
?>

A quick break-down on what we've accomplished here and tips on understanding the code:

  • The filenames are always *Test.php
  • Your test class is the same name as the file, and it extends (usually) from PHPUnit_Framework_TestCase
  • Your *test* methods that get called automatically start with test
  • Chapter 4 of the phpUnit manual has detailed explanations of assertions. Compare those to what I have used in this example.
  • Assertions typically accept 2 arguments in order of: Expected value, Actual value.
  • The @depends is an annotation, and details of their use can be found in the appendix

Running the Test

Providing that you have set-up phpUnit correctly, it is fairly straight forward to run the test.

phpUnit AlphaTest


The 4 dots that appear on the screen indicate successful tests. You can modify the Alpha or AlphaTest to check failures (or errors if you mess up your test code). Chapter 5 details the different outputs and what they mean.

Comments

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.