Public Member Functions | |
__construct ($appName, $testClass) | |
Create a new Unit test controller. | |
__destruct () | |
errorHandler ($errno, $errmsg, $filename, $linenum, $vars) | |
The custom error handler. Don't use this. | |
exceptionHandler ($e) | |
The custom exception handler. Don't use this. | |
passed () | |
Mark a single test as having passed. | |
failed ($e) | |
Mark a single test as having failed. It will mark the test as such and generate a (useable) backtrace. | |
assert ($bool) | |
Assert a boolean expression. If it returns 'true', the test will be marked as passed. Otherwise it will be marked as failed. | |
dumpHtml ($hidePassed=false, $sortGroups=false) | |
Generate a beautiful dump of the test results in HTML format. This outputs a single HTML page. | |
dumpPrettyText ($hidePassed=false) | |
Generate a beautiful dump of the test results in Text format. |
This class can be used as a very simple Unit tester. It simply runs all methods on a class (both static and non-static) and catches all thrown exceptions and PHP errors. Alternatively, you can call $test->assert(ASSERTION);
from the methods in the class to test for things.
If the test class you pass to the constructor contains a property named 'testNames', it will be used to display pretty names for the methods (tests) in the class. testNames should be public, should be an associative array where the keys match the methodnames and should look like this: public $testNames = array("GroupName_TestName" => "test the foobar", ...);
For each called method in the class to be tested, the first parameter passed to the method will be the Tester class. You can use this to assert things from your test case like this: public function MyTestCase($test) { $test->assert(a != b); }
You can subdivide test methods in groups by putting underscores in the method names. Methods are called in the order as they appear in the test class but may be sorted by group in the final results.
An example test class could look like this:
class TestMe { public $testNames = array( "User_Load" => "Load a user", "User_Save" => "Save a user", "Grant_User" => "Give rights to a user", "Grant_Group" => "Give rights to a group", ); public function User_Load($test) { $this->user = new MyProgramUser("john"); $test->assert($this->user->getUserId() == "john"); } public function User_Save($test) { $this->user->save(); } public function Grant_User($test) { $this->user->grantPrivilge(READ); } public function Grant_Group($test) { $this->user->group->grantPrivilge(READ); } public function User_Load_NonExisting($test) { // Test PASSES if an error is thrown! (it's supposed to do so) $test->failed(new Exception("Non existing user loaded.")); try { new MyProgramUser("AmeliaEarhart"); catch (Exception $e) { $test->passed(); } } }
Example output could look like this:
Nr | Test | Passed | Result ----+-----------------------------+--------+----------------------------------------- 1 | User:Load a user | passed | 2 | User:Save a user | passed | 3 | User:Load_NonExisting | FAILED | Non existing user loaded. 4 | Grant:Give rights to a user | passed | 5 | Grant:Give rights to a group| passed | 6 | Group:Add user to group | passed |
Reports can be generated from the results using the ->dumpFoo() methods.
Definition at line 356 of file tlib.php.
TLUnitTest::__construct | ( | $ | appName, | |
$ | testClass | |||
) |
Create a new Unit test controller.
$appName | (string) The name of the application you're testing. | |
$testClass | (string or object instance) An instance of or name (when only using static methods) of a class to test. |
TLUnitTest::assert | ( | $ | bool | ) |
TLUnitTest::dumpHtml | ( | $ | hidePassed = false , |
|
$ | sortGroups = false | |||
) |
Generate a beautiful dump of the test results in HTML format. This outputs a single HTML page.
$hidePassed | (Boolean) If true, passed tests will not be shown. This is useful when you've got alot of testcases and only want the failed ones to show up. | |
$sortGroups | (Boolean) If true, the results will be sorted by group. Otherwise the results will be listed in the same order they where executed. |
TLUnitTest::dumpPrettyText | ( | $ | hidePassed = false |
) |
TLUnitTest::errorHandler | ( | $ | errno, | |
$ | errmsg, | |||
$ | filename, | |||
$ | linenum, | |||
$ | vars | |||
) |
TLUnitTest::exceptionHandler | ( | $ | e | ) |
TLUnitTest::failed | ( | $ | e | ) |
Mark a single test as having failed. It will mark the test as such and generate a (useable) backtrace.
$e | (Exception) The exception that occured. |
You can safely call this method to set the default pass/fail status of a test method and later on in the test mark it as having passed.
Definition at line 468 of file tlib.php.
Referenced by errorHandler(), and exceptionHandler().