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

Component

Class RComponentUnitTest

Executes a set of unit tests for one component (and sub-components) which is a module, a class or a function. Each component can have setup and cleanup functions that are called before and after all unit tests for that component. Component tests are usually stored in physical locations identified by a path (e.g. lib/module/class.php, test/module/class/function1.php etc.). Typically component classes are constructed by the binary: bin/runit.php.

Example
<?php

require_once('rawdev/RawDev.php');
require_once(RAWDEV_LIB.'/Test/Unit/Component.php');

# class from the previous example
class Car {

var $miles = 0;

function drive($miles) {
  if ($miles <= 0) throw new RException('car_illegal_miles', 'Miles should be more than zero.'); 
  $this->miles += $miles;
}

}

# this shows what your test harnass will look like
class TCarDrive extends RComponentUnitTest {

  var $car;
  var $f;

function setup() {
  $this->car = new Car();
  $this->f = array($this->car, 'drive');
}

#Drive10 will show up as the title of the unit test!
function testDrive10() {
  $t = $this->unit($this->f)->in(10)->out()->test();
  $t->assert(10, $this->car->miles);
}

#Negative: test for an exception
function testNegative() {
  $t = $this->unit($this->f)->in(-1)->exception('car_illegal_miles')->test();
}

}

#run the harnass, in real life this is done throug: bin/runit.php
$c = new TCarDrive('function');
$c->test();
$c->display();

?>

Vars

Functions

RComponentUnitTest function __construct($path, $type, $testall, $parent)

Constructor of a component. You'd rarely call this function. Rather use initialize (constructs all tests for the path). (Well: use bin/runit.php really ...)

$pathstringFull path of the component (e.g. Core/ErrorReporting).
$typestringType of the component (module, class or function).
$testallboolWheter all test functions should be tested in the component (false for parents and single function tests).
$parentRComponentUnitTestHandle to the parent test.
returnsRComponentUnitTest

string function getName()

Returns the name of the component (if the path is set).

returnsstring

static RComponentUnitTest function construct($path, $type, $testall, $parent)

Factory method that constructs a component based on a path (and other params). You'd rarely call this function. Rather use initialize (constructs all tests for the path). (Well: use bin/runit.php really ...)

$pathstringFull path of the component (e.g. Core/ErrorReporting).
$typestringType of the component (module, class or function).
$testallboolWheter all test functions should be tested in the component (false for parents and single function tests).
$parentRComponentUnitTestHandle to the parent test.
returnsRComponentUnitTest

string[] function getDependencies()

Return both explicit and implicit dependencies using the source parser.

returnsstring[]Returns a list of object functions that this function immediately depends on (ordered implicit, explicit).

static string function initialize()

Static constructor method that constructs the component, initializes it's parents and sets it's children. If a single function is tested then this is indicated. Only the returned component and all it's children are fully tested. All parent of the component are setup and cleaned up. @param string Full path of the file.

returnsstringName of the class.

static array function getNodes()

Returns all the nodes of the path the rule is: module+,(class,function?)?. In other words of each component (module, class or function, a single test function can be tested or the entire component (module, class or function). @path The path of the component or single test function to test.

returnsarrayAn array of array($piece, $type, $testClass).

static bool function isModule()

Returns wheter the component is a module @path The path of the component.

returnsbool

static bool function isClass()

Returns wheter the component is a class @path The path of the component.

returnsbool

static bool function isFunction()

Returns wheter the component is a function @path The path of the component.

returnsbool

static string function getType()

Returns the type of the component (module, class or function). @path The path of the component.

returnsstringEnum(module,class,function)

string[] function getChildren()

For module and class components, reads the directories and returns all the component components in that directory

returnsstring[]An array of immediate children identifiers (e.g. Util).

void function setup()

When testing, this method is called before the tests of this component are run. You can overwrite this function with setup logic.

void function cleanup()

When testing, this method is called after the tests of this component are run. You can overwrite this function with cleanup logic.

RComponentUnitTest function getRoot()

Returns the root parent (or self if no parent exists).

returnsRComponentUnitTest

void function test()

Tests the component and all it's children. First the component is setup, then either a single function is called or all test functions for the component are called if necessary. If the component has children and it's not a single function test then all children are called as well.

RUnitTest function unit()

Returns a function unit test object. @param string $function The function that should be tested. @param string $title Optional title overwriting the default function name as the title.

returnsRUnitTestarray An array of array($piece, $type, $testClass).

void function display()

Displays both all icons as well as more information for tests that were not OK

void function processLog()

Processes the log for the component and all it's children. @var $index int The nested (0, 1, 2 ...) (since a sub component can be printed this is not necessary equal to $this->level). @var &$tree string[] An array of component identier + icons, one line for each component (altered by children + returned). @var &$icons string[] An array of character icons (altered by children + returned). @var &$messages string[] An array of failed test messages (altered by children + returned).

static string function readClassName()

Returns the name of the class given a file. @var $file string Full path of the file.

returnsstringName of the class.
Comments