Api‎ > ‎Test‎ > ‎Unit‎ > ‎


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.



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";



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


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.

$functioncallbackThe function that is tested. Either a function name or an array(class/object,name).

static RUnitTest function construct($function) [fluid]

Returns an instanciated object RUnitTest for the function.

$functioncallbackFunction to be tested. Either a function name or an array(class/object,name).
returnsRUnitTestReturns test object for function.

RUnitTest function in($in[, $...]) [fluid]

Sets the in params.

$inmixedZero or more in parameters can be passed in.

RUnitTest function out($expected, $partial, $exactType) [fluid]

Sets the expected out/return result.

$expectedmixedThe value that is expected (the definition) (e.g. 1 or array(1, 2) or array(1, 2, new RMatchRegex("/test/"))).
$partialboolTurning this on will loosen the exact match: selected keys in $expected are checked (not all keys are required)
$exactTypeboolTurning this on tighten exact match: all scalar types will need to match exactly (e.g. 1 != "1")

RUnitTest function exception($exception) [fluid]

Sets the expected exception type that will be thrown.

$exceptionstringThe expected exception type that is expected to be thrown.

RUnitTest function title($title) [fluid]

Sets the title of the function test. This can be helpful to debug when a function test fails.

$titlestringThe title of the function test.

bool function test()

Executes the test based on the in, out and exception.

returnsboolWhether the execution status is ok.

void function setStatus($status) [fluid]

Sets the status of the test and the appropriate icon for the result

$statusstringStatus 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.

$titlestringThe title of the function test.
$messagestringOptional custom message.