1
0
mirror of synced 2024-12-13 14:56:01 +03:00
doctrine2/tests/Test.php

163 lines
3.6 KiB
PHP
Raw Normal View History

2007-05-30 00:09:25 +04:00
<?php
class GroupTest
{
protected $_testCases = array();
public function addTestCase(UnitTestCase $testCase)
{
$this->_testCases[] = $testCase;
}
public function run(HtmlReporter $reporter)
{
foreach ($this->_testCases as $testCase) {
$testCase->run();
}
$reporter->setTestCase($this);
$reporter->paintHeader();
$reporter->paintFooter();
}
2007-05-31 01:26:19 +04:00
public function getMessages()
{
$messages = array();
foreach($this->_testCases as $testCase) {
2007-06-04 22:20:32 +04:00
$messages = array_merge($messages, $testCase->getMessages());
2007-05-31 01:26:19 +04:00
}
return $messages;
}
2007-05-30 00:09:25 +04:00
public function getFailCount()
{
$fails = 0;
foreach ($this->_testCases as $testCase) {
$fails += $testCase->getFailCount();
}
return $fails;
}
public function getTestCaseCount()
{
return count($this->_testCases);
}
public function getPassCount()
{
$passes = 0;
foreach ($this->_testCases as $testCase) {
$passes += $testCase->getPassCount();
}
return $passes;
}
}
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-06-07 21:04:56 +04:00
$this->_fail();
2007-05-31 01:26:19 +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++;
}
public function fail()
2007-06-07 21:04:56 +04:00
{
$this->_fail();
}
public function _fail()
2007-05-31 01:26:19 +04:00
{
$trace = debug_backtrace();
array_shift($trace);
foreach ($trace as $stack) {
2007-06-01 14:17:50 +04:00
2007-05-31 01:26:19 +04:00
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-05-31 01:26:19 +04:00
$this->_messages[] = $class->getName() . ' : method ' . $stack['function'] . ' failed on line ' . $line;
break;
}
$line = $stack['line'];
2007-06-07 21:04:56 +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;
}
}