<?php
namespace GazelleTest;
/**
* Root Unit Test class. Gazelle uses a system independent from
* dependencies other than PHP itself, and this class it at the
* heart of it. Each Gazelle Unit Test is to have the same name
* as the class that it tests, though each class exists in
* separate namespaces.
*
* @author Michael
* @package Gazelle Testing
*/
abstract class UnitTest {
/**
* Test Status codes. These form a bitfield, so the next
* one should be 4...
*/
const PASSED = 0;
const FAIL = 1;
const WRONG_EXCEPTION = 2;
/**
* Tests to run.
* @var array
*/
protected $tests = array();
/**
* Error record.
*/
public $errors = array();
/**
* Setup Procedures
*/
protected abstract function setUp();
/**
* Setup a test batch. Each test should have the object that
* is being tested and parameters to test it against. Each
* Unit test needs at least one test batch. The returned
* batch needs to be an array with three elements -
* obj => object being tested.
* params => parameters under testing.
* tests => Tests to be performed.
*
* @param Name of the group
* @param Object under test
* @param Array of parameters to use for testing.
*/
protected abstract function setUpTests( $name, $obj, $params );
public function __construct() {
$this->setUp();
}
/**
* Set up and run the tests. Final because I do NOT want this methodology
* swapped on the child tests at all.
*/
public final function run() {
/* Iterate over the tests - each test should be a closure that
* takes the class to test as the sole argument. Return errors
* encountered in the test as a bitfield, or 0 for no errors.
*/
foreach ($this->tests as $group => $batch ) {
foreach ($batch['tests'] as $name => $test ) {
$error = $test();
if ($error) {
$msg = $name;
if ( $error & self::PASSED ) {
$msg .= ' PASSED';
}
if ( $error & self::FAIL ) {
$msg .= ' FAIL';
}
if ($error & self::WRONG_EXCEPTION) {
$msg .= ' WRONG_EXCEPTION';
}
$this->errors[$group][] = array (
'message' => $msg,
'object' => $batch['object'],
'params' => $batch['params']
);
}
}
}
return count($this->errors) == 0;
}
/**
* Return the fully qualified class name of our counterpart
* in the framework.
*/
protected function getClassName() {
return '\\Gazelle\\'.substr( get_class($this), strlen(__NAMESPACE__)+1 );
}
}
Bookmarks