We are going to:
Please consider...
The existing unit testing libraries (most of them) require some installation steps, or initial configurations, what in many cases has a great overhead escpecially in small projects, or when testing small units.
This unit testing class is very simple, and starts his way with no more than 4 assertion functions:
The class will be extended during the time, but its main goal is to stay as simple as possible (as now).
All you need to run it, is to extend it. So here is the class (The code is also available in http://pastebin.com/TEruA7wv):
<?php
/**
* implementation of a very basic unit testing class
*/
abstract class UnitTest {
/**
*
* check the value is true
* @param mixed $expr
*/
public function assertTrue($expr) {
if (!$expr) {
$this->_printAssertResult("{$expr} - assertTrue failed!");
}
}
/**
*
* check the value is false
* @param mixed $expr
*/
public function assertFalse($expr) {
if ($expr) {
$this->_printAssertResult("{$expr} - assertFalse failed!");
}
}
/**
*
* check the two values are equal
* @param mixed $expr1
* @param mixed $expr2
*/
public function assertEquals($expr1, $expr2) {
if ($expr1 != $expr2) {
$this->_printAssertResult("{$expr1} not equals {$expr2}");
}
}
/**
*
* check the two values are equal and have the same type
* @param mixed $expr1
* @param mixed $expr2
*/
public function assertEqualsStrict($expr1, $expr2) {
if ($expr1 !== $expr2) {
$this->_printAssertResult("{$expr1} not equals {$expr2}");
}
}
/**
*
* execute the tests in the extending class.
* All testing method should start with "test" (lowercase)
*/
public function run() {
echo '---- Starting the Test (',date('d.m.Y H:i:s'),') -----', "\r\n";
// read all methods, and execute tests
$methods_list = get_class_methods($this);
if (!empty($methods_list)) {
foreach ($methods_list as $method) {
if (preg_match('%^test%', $method)) {
echo 'testing ', preg_replace('%^test%', '', $method), "\r\n";
$this->$method();
}
}
}
}
/** ************ PROTECTED SECTION *************** **/
/**
*
* print assertion result
* @param string $message
*/
protected function _printAssertResult($message) {
echo "{$message}\n";
}
}