2007-05-30 00:09:25 +04:00
|
|
|
<?php
|
2007-07-16 22:31:52 +04:00
|
|
|
class GroupTest extends UnitTestCase
|
2007-05-30 00:09:25 +04:00
|
|
|
{
|
|
|
|
protected $_testCases = array();
|
|
|
|
|
2007-07-06 16:37:02 +04:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
if (extension_loaded('xdebug')) {
|
|
|
|
//xdebug_start_code_coverage(XDEBUG_CC_DEAD_CODE | XDEBUG_CC_UNUSED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-30 00:09:25 +04:00
|
|
|
public function addTestCase(UnitTestCase $testCase)
|
|
|
|
{
|
2007-09-02 21:55:42 +04:00
|
|
|
if($testCase instanceOf GroupTest) {
|
|
|
|
$this->_testCases = array_merge($this->_testCases, $testCase->getTestCases());
|
|
|
|
} else {
|
2007-09-03 00:14:45 +04:00
|
|
|
$this->_testCases[get_class($testCase)] = $testCase;
|
2007-09-02 21:55:42 +04:00
|
|
|
}
|
2007-05-30 00:09:25 +04:00
|
|
|
}
|
2007-09-02 21:55:42 +04:00
|
|
|
|
2007-09-03 01:51:04 +04:00
|
|
|
public function shouldBeRun($testCase, $filter){
|
2007-09-03 02:34:02 +04:00
|
|
|
if( ! is_array($filter)) {
|
2007-09-03 01:51:04 +04:00
|
|
|
return true;
|
|
|
|
}
|
2007-09-03 02:34:02 +04:00
|
|
|
foreach($filter as $subFilter) {
|
2007-09-03 01:51:04 +04:00
|
|
|
$name = strtolower(get_class($testCase));
|
|
|
|
$pos = strpos($name, strtolower($subFilter));
|
|
|
|
//it can be 0 so we have to use === to see if false
|
2007-09-03 02:34:02 +04:00
|
|
|
if ($pos === false) {
|
2007-09-03 01:51:04 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
public function run(HtmlReporter $reporter, $filter)
|
2007-05-30 00:09:25 +04:00
|
|
|
{
|
2007-07-23 23:22:13 +04:00
|
|
|
$reporter->paintHeader();
|
2007-09-02 20:19:32 +04:00
|
|
|
foreach ($this->_testCases as $k => $testCase) {
|
2007-09-03 01:51:04 +04:00
|
|
|
if ( ! $this->shouldBeRun($testCase, $filter)) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-09-02 20:19:32 +04:00
|
|
|
$testCase->run();
|
|
|
|
$this->_passed += $testCase->getPassCount();
|
|
|
|
$this->_failed += $testCase->getFailCount();
|
2007-07-23 23:22:13 +04:00
|
|
|
$this->_messages = array_merge($this->_messages, $testCase->getMessages());
|
2007-07-16 22:31:52 +04:00
|
|
|
|
2007-09-02 20:19:32 +04:00
|
|
|
$this->_testCases[$k] = null;
|
2007-07-29 00:28:20 +04:00
|
|
|
if(PHP_SAPI === "cli"){
|
|
|
|
echo ".";
|
|
|
|
}
|
|
|
|
set_time_limit(900);
|
2007-09-02 20:19:32 +04:00
|
|
|
}
|
2007-05-30 00:09:25 +04:00
|
|
|
$reporter->setTestCase($this);
|
|
|
|
|
|
|
|
$reporter->paintFooter();
|
|
|
|
}
|
2007-07-16 22:31:52 +04:00
|
|
|
|
|
|
|
|
2007-05-30 00:09:25 +04:00
|
|
|
public function getTestCaseCount()
|
|
|
|
{
|
|
|
|
return count($this->_testCases);
|
|
|
|
}
|
2007-07-16 22:31:52 +04:00
|
|
|
|
2007-09-02 21:55:42 +04:00
|
|
|
public function getTestCases(){
|
|
|
|
return $this->_testCases;
|
|
|
|
}
|
2007-05-30 00:09:25 +04:00
|
|
|
}
|
|
|
|
class HtmlReporter
|
|
|
|
{
|
|
|
|
protected $_test;
|
|
|
|
|
|
|
|
public function setTestCase(GroupTest $test)
|
|
|
|
{
|
|
|
|
$this->_test = $test;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class UnitTestCase
|
|
|
|
{
|
|
|
|
protected $_passed = 0;
|
|
|
|
|
|
|
|
protected $_failed = 0;
|
2007-05-31 01:26:19 +04:00
|
|
|
|
|
|
|
protected $_messages = array();
|
2007-05-30 00:09:25 +04:00
|
|
|
|
|
|
|
public function assertEqual($value, $value2)
|
|
|
|
{
|
|
|
|
if ($value == $value2) {
|
|
|
|
$this->_passed++;
|
|
|
|
} else {
|
2007-07-30 23:01:16 +04:00
|
|
|
$seperator = "<br>";
|
|
|
|
if(PHP_SAPI === "cli"){
|
|
|
|
$seperator = "\n";
|
|
|
|
}
|
|
|
|
$message = "$seperator Value1: $value $seperator != $seperator Value2: $value2 $seperator";
|
|
|
|
$this->_fail($message);
|
2007-05-31 01:26:19 +04:00
|
|
|
}
|
|
|
|
}
|
2007-06-12 21:05:16 +04:00
|
|
|
|
|
|
|
public function assertIdentical($value, $value2)
|
|
|
|
{
|
|
|
|
if ($value === $value2) {
|
|
|
|
$this->_passed++;
|
|
|
|
} else {
|
|
|
|
$this->_fail();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-31 01:26:19 +04:00
|
|
|
public function assertNotEqual($value, $value2)
|
|
|
|
{
|
|
|
|
if ($value != $value2) {
|
|
|
|
$this->_passed++;
|
|
|
|
} else {
|
2007-06-07 21:04:56 +04:00
|
|
|
$this->_fail();
|
2007-05-31 01:26:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public function assertTrue($expr)
|
|
|
|
{
|
|
|
|
if ($expr) {
|
|
|
|
$this->_passed++;
|
|
|
|
} else {
|
2007-06-07 21:04:56 +04:00
|
|
|
$this->_fail();
|
2007-05-30 00:09:25 +04:00
|
|
|
}
|
|
|
|
}
|
2007-05-31 01:26:19 +04:00
|
|
|
public function assertFalse($expr)
|
|
|
|
{
|
|
|
|
if ( ! $expr) {
|
|
|
|
$this->_passed++;
|
|
|
|
} else {
|
2007-06-07 21:04:56 +04:00
|
|
|
$this->_fail();
|
2007-05-31 01:26:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public function pass()
|
|
|
|
{
|
|
|
|
$this->_passed++;
|
|
|
|
}
|
2007-07-30 23:01:16 +04:00
|
|
|
public function fail($message = "")
|
2007-06-07 21:04:56 +04:00
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
$this->_fail($message);
|
2007-06-07 21:04:56 +04:00
|
|
|
}
|
2007-07-30 23:01:16 +04:00
|
|
|
public function _fail($message = "")
|
2007-05-31 01:26:19 +04:00
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
$trace = debug_backtrace();
|
|
|
|
array_shift($trace);
|
2007-05-31 01:26:19 +04:00
|
|
|
|
|
|
|
|
|
|
|
foreach ($trace as $stack) {
|
|
|
|
if (substr($stack['function'], 0, 4) === 'test') {
|
|
|
|
$class = new ReflectionClass($stack['class']);
|
|
|
|
|
2007-06-01 14:17:50 +04:00
|
|
|
if ( ! isset($line)) {
|
2007-06-07 21:04:56 +04:00
|
|
|
$line = $stack['line'];
|
2007-06-01 14:17:50 +04:00
|
|
|
}
|
|
|
|
|
2007-07-23 23:22:13 +04:00
|
|
|
$errorMessage = $class->getName() . ' : method ' . $stack['function'] . ' failed on line ' . $line;
|
2007-07-30 23:01:16 +04:00
|
|
|
$this->_messages[] = $errorMessage . " " . $message;
|
2007-05-31 01:26:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
$line = $stack['line'];
|
2007-09-03 18:57:18 +04:00
|
|
|
}
|
2007-05-31 01:26:19 +04:00
|
|
|
$this->_failed++;
|
|
|
|
}
|
2007-05-30 00:09:25 +04:00
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
foreach (get_class_methods($this) as $method) {
|
|
|
|
if (substr($method, 0, 4) === 'test') {
|
|
|
|
$this->setUp();
|
|
|
|
|
|
|
|
$this->$method();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-31 01:26:19 +04:00
|
|
|
public function getMessages()
|
|
|
|
{
|
|
|
|
return $this->_messages;
|
|
|
|
}
|
2007-05-30 00:09:25 +04:00
|
|
|
public function getFailCount()
|
|
|
|
{
|
|
|
|
return $this->_failed;
|
|
|
|
}
|
|
|
|
public function getPassCount()
|
|
|
|
{
|
|
|
|
return $this->_passed;
|
|
|
|
}
|
|
|
|
}
|