SIMPLE TEST CHEAT SHEET

TEST CASE
<?php
require_once('simpletest/autorun.php');
require_once('../classes/example.php');
 
class ExampleTestCase extends UnitTestCase {
 
    function ExampleTestCase() {
        $this->UnitTestCase('Example test');
    }
    
    function setUp() {
    // initial conditions
    }
    
    function tearDown() {
    // remove initial conditions
    }
    
    function testSomething() {
    // do assertions
    }
}
ASSERTIONS
assertTrue($x)Fail if $x is false
assertFalse($x)Fail if $x is true
assertNull($x)Fail if $x is set
assertNotNull($x)Fail if $x not set
assertIsA($x, $t)Fail if $x is not the class or type $t
assertNotA($x, $t)Fail if $x is of the class or type $t
assertEqual($x, $y)Fail if $x == $y is false
assertNotEqual($x, $y)Fail if $x == $y is true
assertWithinMargin($x, $y, $m)Fail if abs($x - $y) < $m is false
assertOutsideMargin($x, $y, $m)Fail if abs($x - $y) < $m is true
assertIdentical($x, $y)Fail if $x == $y is false or a type mismatch
assertNotIdentical($x, $y)Fail if $x == $y is true and types match
assertReference($x, $y)Fail unless $x and $y are the same variable
assertClone($x, $y)Fail unless $x and $y are identical copies
assertPattern($p, $x)Fail unless the regex $p matches $x
assertNoPattern($p, $x)Fail if the regex $p matches $x
expectError($x)Swallows any upcoming matching error
assert($e)Fail on failed expectation object $e
TEST SUITE
<?php
require_once('simpletest/unit_tester.php');
require_once('simpletest/reporter.php');
require_once('example_test.php');
 
$test = new TestSuite('All file tests');
$test->addTestCase(new ExampleTestCase());
$test->addTestFile('another_example_test.php');
$test->run(new HtmlReporter());
MOCK OBJECTS
Creating mock objects
Mock::generate('Example')Create MockExample mock from Example class
Mock::generate('Example', 'MyMock', array('set'))Create MyMock mock from Example class with set behave like in Example
Using mocks
$mock = new MockExample();Instanciates the mock object
setReturnValue($m, $v, $args);Set the return value of $m (method) when is call with $args arguments
setReturnReference($m, $r, $args);Set the return $r (reference) in $m
setReturnValueAt($x, $m, $v);Set the return value of $m when is call at $x time
setReturnReferenceAt($x, $m, $r);Same that setReturnValueAt but by reference
setReturnValue($m, $v, array(1, "*");Return $v when is called in $m with 1 in the first argument and anything in the second
Expectations (needs tally())
expect($m, $args) No
expectAt($timing, $m, $args) No
expectCallCount($m, $count) Yes
expectMaximumCallCount($m, $count) No
expectMinimumCallCount($m, $count) Yes
expectNever($m) No
expectOnce($m, $args) Yes
expectAtLeastOnce($m, $args) Yes
By Rodrigo Arce. Based on SimpleTest documentation. Used Leslie Franke's Firefox Cheat Sheet as template and HiLiteMe to format PHP code. Created 06.12.08