*/ /* The MIT License Copyright (c) 2007 Nick Galbreath Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ //error_reporting(E_ALL | E_STRICT); /** This is just to separate out errors (bad code, won't compile) versus assertion failures ( 1 == 2) **/ class AssertException extends Exception { } /** the built-in 'assert' will call this.. you don't need it **/ function fail() { // bt[0] is right "here", bt[1] is the assert function, bt[2] is caller $bt = debug_backtrace(); $line = $bt[2]['line']; $class = $bt[2]['class']; $function = $bt[2]['function']; throw new AssertException("Assertion failed @ line $line in $class::$function"); } /* Make sure options are set correctly */ assert_options(ASSERT_ACTIVE, 1); assert_options(ASSERT_WARNING, 0); assert_options(ASSERT_BAIL, 0); assert_options(ASSERT_QUIET_EVAL, 0); assert_options(ASSERT_CALLBACK, 'fail'); /** Stand alone function to test for equality * Not really needed but makes a better error message than plain assert */ function assertEquals($val, $expected, $optional_msg = "") { if ($val === $expected) return; $bt = debug_backtrace(); $line = $bt[1]['line']; $class = $bt[1]['class']; $function = $bt[1]['function']; throw new AssertException("$optional_msg $val != $expected @ line $line in $class::$function"); } /* Guess what this does. * http://blog.modp.com/2007/10/php-string-startswith-let-me-count-ways.html */ function str_startswith($source, $prefix) { return strncmp($source, $prefix, strlen($prefix)) == 0; } class UnitTest { public function __construct() { $this->error = 0; $this->pass = 0; $this->fail = 0; // this returns the name of current class (the child) $mynameis = get_class($this); $class = new ReflectionClass($mynameis); $methods = $class->getMethods(); foreach ($methods as $i) { if (str_startswith($i->name, 'test')) { $name = $i->name; echo "$name"; try { $this->setUp(); $this->{$name}(); $this->tearDown(); $this->pass++; echo ": OK\n"; } catch (AssertException $e) { $this->fail++; $msg = $e->getMessage(); echo ": Fail : $msg\n"; } catch (Exception $e) { $this->error++; $msg = $e->getMessage(); $line = $e->getLine(); $file = $e->getFile(); echo ": Error : $msg @ line $line in $file\n"; } } } $this->total = $this->fail + $this->error + $this->pass; echo "Total Tests: $this->total\n"; } // test classes can over-ride these public function setup() { } public function teardown() { } } // Test Driver function unittest($name) { $ok = eval("\$ut = new $name();"); if ($ok === false) { echo "Test $name not loaded\n"; exit(2); } echo "total = $ut->total, pass = $ut->pass, fail = $ut->fail, error = $ut->error\n"; exit( ($ut->total == $ut->pass) ? 0 : 1); } /** CHANGE LOG * V1 20-Dec-07: inital release * */ ?>