* @license PHP License * @package WB * @subpackage unittest */ /** * Helper class for UnitTests * * @version 0.2.1 * @package WB * @subpackage unittest */ class WBUnitTest { /** * base folder of unit-tests * @var string */ protected $dir; /** * test group * @var string */ protected $group; /** * unit test files to run * @var array */ protected $files = array(); /** * * @var */ protected $groupTest; static protected $incPath; /** * static constructor * * load simple test framework */ static public function staticConstruct() { $incPath = WBParam::get('wb/class/phpincludepath'); $simpleTestDir = WBParam::get('wb/dir/system') . '/include/simpletest'; $incPath[] = $simpleTestDir; WBParam::set('wb/class/phpincludepath', $incPath); WBParam::set('wb/error/ignoreincludepath', true); WBParam::set('wb/dir/config', 'test/unit/etc'); WBParam::set('wb/error/reporting', E_ALL | E_STRICT | E_DEPRECATED); WBParam::set('wb/error/php4', 'log'); // load simpletest basics require_once $simpleTestDir . '/unit_tester.php'; require_once $simpleTestDir . '/scorer.php'; self::$incPath = WBParam::get('wb/class/includepath'); $locals = WBParam::get('wb/class/local'); $locals['WBUT'] = 'Wombat::WBUT/'; WBParam::set('wb/class/local', $locals); // ignore warning about session storage driver Null WBClass::load('patErrorManager'); patErrorManager::addIgnore('patSession:Null:1'); } /** * constructor * */ public function __construct() { // avoid ugly warning when sending header $this->dir = '.'; $this->group = basename($_SERVER['PHP_SELF'], '.php'); } public function setDir($dir) { $this->dir = $dir; } /** * add test file to group test * * @param string $file withoup extension and path */ public function addTestFile($file) { $this->files[] = $file; } /** * run test * * run group test and use mathing reporter to generate nice output */ public function run() { WBClass::load('WBLog'); $runner = new TestSuite($this->group); foreach ($this->files as $f) { $runner->addFile($this->dir . '/' . $this->group . '/' . $f . '.php'); } if (isset($_SERVER['HTTP_HOST'])) { $reporter = WBClass::create('WBUnitTest_Reporter'); } else { require_once 'reporter.php'; $reporter = new TextReporter(); } // add group's include path $incPath = self::$incPath; $incPath[] = $this->dir . '/' . $this->group . '/include'; WBParam::set('wb/class/includepath', $incPath); $runner->run($reporter); } /** * run all groups * */ public function runAll() { $di = new DirectoryIterator($this->dir); foreach ($di as $i) { $group = $i->getFilename(); if ($i->isDot() || $i->isFile() || $group[0] == '.' || $group == 'etc-default') { continue; } // start over $this->group = $group; $this->files = array(); $dj = new DirectoryIterator($this->dir . '/' . $this->group); $run = false; foreach ($dj as $j) { $file = $j->getFilename(); if ($j->isDot() || substr( $file, -4 ) != '.php') { continue; } $run = true; $this->addTestFile(basename($file, '.php')); } if ($run) { $this->run(); } continue; // create group runner $test = new GroupTest($group); $dj = new DirectoryIterator($dir . '/' . $group); $run = false; foreach ($dj as $j) { $file = $j->getFilename(); if ($j->isDot() || $name[0] == '.' || substr( $file, -4 ) != '.php') { continue; } $run = true; // collect additional test files $test->addTestFile($j->getPath() . '/' . $file); } // run grouped tests if ($run) { $test->run(getReporter()); } unset($test); } } } ?>