writing unit tests for php - example 2

In an earlier tutorial (in the amfPHP section), I created an object called Bravo that demonstrated a technique for transferring a date object from PHP to Flex. In this tutorial I'm going add a few more methods, try out some more phpUnit assertions and annotations and set myself up to examine code-coverage.

First you'll want to review the amended Bravo class. I've added quite a few methods to demonstrate various techniques. Next we can review the BravoTest class.

BravoTest.php

<?php
 
/*
* @codeCoverageIgnore
*/
require 'amf_types/Date.php';
 
require '../Bravo.php';
 
//---------------------------------------------------------
class BravoTest extends PHPUnit_Framework_TestCase
//---------------------------------------------------------
{
private $bravo;
 
//---------------------------------------------------------
protected function setUp()
//---------------------------------------------------------
{
$this->bravo = new Bravo();
}
 
//---------------------------------------------------------
public function testGetDate()
//---------------------------------------------------------
{
$data = $this->bravo->get_date();
$this->assertInstanceOf('DataLine', $data);
$this->assertInstanceOf('Amfphp_Core_Amf_Types_Date', $data->some_date);
} 
 
//---------------------------------------------------------
public function testGetRecordAt()
//---------------------------------------------------------
{
$data = $this->bravo->get_record_at(1);
$this->assertInstanceOf('DataLine', $data);
$this->assertInstanceOf('Amfphp_Core_Amf_Types_Date', $data->some_date);
} 
 
/**
* @depends testGetRecordAt
*/
public function testGetRecordAt2()
//---------------------------------------------------------
{
$data = $this->bravo->get_record_at(1);
$this->assertInternalType('int', $data->some_int);
$this->assertGreaterThan(5, $data->some_int);
} 
 
//---------------------------------------------------------
public function testGetAllRecords()
//---------------------------------------------------------
{
$data = $this->bravo->get_all_records();
$this->assertInternalType('array', $data);
 
//assertContains 					is comparing literals
//assertContainsOnly 				is comparing basic types (i.e. string, int)
//assertContainsOnlyInstanceOf		is comparing classes
 
$this->assertContainsOnlyInstancesOf('DataLine', $data);
}
 
//---------------------------------------------------------
public function testTokenCall()
//---------------------------------------------------------
{
$this->assertStringEndsWith('A', $this->bravo->token_call('A'));
$this->assertStringStartsWith('a bit', $this->bravo->token_call('B'));
}
 
/**
* @expectedException     Exception
* @expectedExceptionCode 100
*/
//---------------------------------------------------------
public function testTokenCall2()
//---------------------------------------------------------
{
$this->bravo->token_call(33);
}
 
 
};
 
?>

A quick break-down on what we've accomplished here and tips on understanding the code (from example 1):

  • 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

A quick break-down on what we've accomplished that is new for this example:

  • The testTokenCall2 method is testing to see if we get an appropriate exception
  • I am using a wider variety of assertions. Check them out.
  • The Bravo class has special comments added to skip over certain code blocks for code coverage (covered in another tutorial).

Running the Test

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

phpUnit BravoTest


The 4 dots that appear on the screen indicate successful tests. You can modify the Bravo or BravoTest 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.