Class RUnitTest
Executes one unit test. Offers functionality to run a specific function with specific in parameters, check output as well as exception raised. In addition, the ability to assert the impact the function had on object vars and global state.
Example
<?php
require_once('rawdev/RawDev.php');
require_once(RAWDEV_LIB.'/Test/Unit/Test.php');
function sum() {
$params = func_get_args();
if (count($params) < 2) throw new RException('math_too_few_params', 'Two or more parameters please.');
$sum = 0;
foreach($params as $param) $sum += $param;
return $sum;
}
# test 1
$t = RUnitTest::construct('sum')->title('Two Params')->in(1, 2)->out(3)->test();
print $t->icon;
# test 2
$t = RUnitTest::construct('sum')->title('One Params')->in(1)->exception('math_too_few_params')->test();
print "$t->iconn";
?>
Vars
static array $icons
The icons that are displayed for the statuses of the function test results.
var callback $function
callback The function to be tested. Either a function name or an array(class/object,name).
var string $title
The title of the test (e.g. "No params test").
var mixed[] $in=array()
Array of in parameters to be tested.
var mixed $out
The expected returned result.
var bool $checkOutput
Wheter the output should be checked.
var mixed $exception
The expected thrown exception.
var mixed $result
The result that was returned from the test.
var mixed $caught
The exception that was caught (if one was raised).
var string $status
The status of the executed test (ok, fail, exception, no_exception).
var string $icon
The icon that is associated with the status of the executed test.
var string $message
More information on what went wrong in the test after execution when the status is not 'ok'.
var int $asserts=0
Count the number of asserts
Functions
RUnitTest function __construct($function)
Creates a test object for the function to be tested. This constructor shouldn't be used rather use the factory method construct
that properly initializes the object as well.
$function | callback | The function that is tested. Either a function name or an array(class/object,name). |
returns | RUnitTest | |
static RUnitTest function construct($function) [fluid]
Returns an instanciated object RUnitTest for the function.
$function | callback | Function to be tested. Either a function name or an array(class/object,name). |
returns | RUnitTest | Returns test object for function. |
RUnitTest function in($in[, $...]) [fluid]
Sets the in params.
$in | mixed | Zero or more in parameters can be passed in. |
returns | RUnitTest | |
RUnitTest function out($expected, $partial, $exactType) [fluid]
Sets the expected out/return result.
$expected | mixed | The value that is expected (the definition) (e.g. 1 or array(1, 2) or array(1, 2, new RMatchRegex("/test/"))). |
$partial | bool | Turning this on will loosen the exact match: selected keys in $expected are checked (not all keys are required) |
$exactType | bool | Turning this on tighten exact match: all scalar types will need to match exactly (e.g. 1 != "1") |
returns | RUnitTest | |
RUnitTest function exception($exception) [fluid]
Sets the expected exception type that will be thrown.
$exception | string | The expected exception type that is expected to be thrown. |
returns | RUnitTest | |
RUnitTest function title($title) [fluid]
Sets the title of the function test. This can be helpful to debug when a function test fails.
$title | string | The title of the function test. |
returns | RUnitTest | |
bool function test()
Executes the test based on the in, out and exception.
returns | bool | Whether the execution status is ok. |
void function setStatus($status) [fluid]
Sets the status of the test and the appropriate icon for the result
$status | string | Status string ('ok', 'fail', 'fail_assert', 'exception', 'no_exception') |
RUnitTest function assert($title, $message) [fluid]
Asserts an expected value against an actual value after the test has been completed. This is useful when testing state external to the function.
$title | string | The title of the function test. |
$message | string | Optional custom message. |
returns | RUnitTest | |