created test results, separated out test methods so they can be run independently of a test case
This commit is contained in:
parent
d92fb480c0
commit
801403b3f1
23
ErredTestResult.php
Normal file
23
ErredTestResult.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
class FailedTestResult implements TestResult {
|
||||
|
||||
protected $test;
|
||||
protected $failure;
|
||||
|
||||
public function __construct(Testable $test, TestFailure $failure) {
|
||||
$this->test = $test;
|
||||
$this->failure = $failure;
|
||||
}
|
||||
|
||||
public function passed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function failed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
23
FailedTestResult.php
Normal file
23
FailedTestResult.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
class FailedTestResult implements TestResult {
|
||||
|
||||
protected $test;
|
||||
protected $failure;
|
||||
|
||||
public function __construct(Testable $test, TestFailure $failure) {
|
||||
$this->test = $test;
|
||||
$this->failure = $failure;
|
||||
}
|
||||
|
||||
public function passed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function failed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
23
IgnoredTestResult.php
Normal file
23
IgnoredTestResult.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
class IgnoredTestResult implements TestResult {
|
||||
|
||||
protected $test;
|
||||
protected $failure;
|
||||
|
||||
public function __construct(Testable $test, TestFailure $failure) {
|
||||
$this->test = $test;
|
||||
$this->failure = $failure;
|
||||
}
|
||||
|
||||
public function passed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function failed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
21
PassedTestResult.php
Normal file
21
PassedTestResult.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
class PassedTestResult implements TestResult {
|
||||
|
||||
protected $test;
|
||||
|
||||
public function __construct(Testable $test) {
|
||||
$this->test = $test;
|
||||
}
|
||||
|
||||
public function passed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function failed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
64
TestCase.php
64
TestCase.php
@ -8,11 +8,11 @@
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
protected function setUp() {
|
||||
public function setUp() {
|
||||
|
||||
}
|
||||
|
||||
protected function tearDown() {
|
||||
public function tearDown() {
|
||||
|
||||
}
|
||||
|
||||
@ -21,69 +21,17 @@
|
||||
$listener->onBeforeTestCase($this);
|
||||
}
|
||||
|
||||
$result = new TestCaseResult();
|
||||
foreach ($this->getTestableMethods() as $method) {
|
||||
$this->runTestMethod($method);
|
||||
$testMethod = new TestMethod($this, $method);
|
||||
$result->addTestResult($testMethod->run($listeners));
|
||||
}
|
||||
|
||||
foreach ($listeners as $listener) {
|
||||
$listener->onAfterTestCase($this);
|
||||
}
|
||||
}
|
||||
|
||||
public function runTestMethod(ReflectionMethod $method, array $listeners) {
|
||||
foreach ($listeners as $listener) {
|
||||
$listener->beforeTestMethod($method);
|
||||
}
|
||||
|
||||
$testPassed = false;
|
||||
$result = null;
|
||||
$failure = null;
|
||||
|
||||
$this->setUp();
|
||||
try {
|
||||
$method->invoke($this);
|
||||
$testPassed = true;
|
||||
} catch (TestFailure $failure) {
|
||||
if ($failure instanceof FailedTest) {
|
||||
foreach ($listeners as $listener) {
|
||||
$listener->onTestMethodFailed($method);
|
||||
}
|
||||
} else if ($failure instanceof IgnoredTest) {
|
||||
foreach ($listeners as $listener) {
|
||||
$listener->onTestMethodIgnored($method);
|
||||
}
|
||||
} else if ($failure instanceof ErredTest) {
|
||||
foreach ($listeners as $listener) {
|
||||
$listener->onTestMethodErred($method);
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
//test for expected exception
|
||||
foreach ($listeners as $listener) {
|
||||
$listener->onTestMethodErred($method);
|
||||
}
|
||||
}
|
||||
|
||||
$result = $this->createTestResult($method, $failure);
|
||||
$this->tearDown();
|
||||
|
||||
foreach ($listeners as $listener) {
|
||||
$listener->afterTestMethod($method);
|
||||
}
|
||||
}
|
||||
|
||||
protected function createTestResult(ReflectionMethod $method, TestFailure $failure = null) {
|
||||
if ($failure === null) {
|
||||
return new PassedTestResult($method);
|
||||
} else if ($failure instanceof FailedTest) {
|
||||
return new FailedTestResult($method, $failure);
|
||||
} else if ($failure instanceof ErredTest) {
|
||||
return new ErredTestResult($method, $failure);
|
||||
} else if ($failure instanceof IgnoredTest) {
|
||||
return new IgnoredTestResult($method, $failure);
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('Unknown test failure type: ' . get_class($failure));
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected final function getTestableMethods() {
|
||||
|
55
TestCaseResult.php
Normal file
55
TestCaseResult.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
class TestCaseResult implements TestResult {
|
||||
|
||||
protected $testResults;
|
||||
|
||||
public function __construct() {
|
||||
$this->testResults = array();
|
||||
}
|
||||
|
||||
public function passed() {
|
||||
return count($this->getFailedTestResults()) === 0;
|
||||
}
|
||||
|
||||
public function failed() {
|
||||
return count($this->getFailedTestResults()) > 0;
|
||||
}
|
||||
|
||||
public function count() {
|
||||
return count($this->testResults);
|
||||
}
|
||||
|
||||
public function addTestResult(TestResult $result) {
|
||||
$this->testResults[] = $result;
|
||||
}
|
||||
|
||||
public function getTestResults() {
|
||||
return $this->testResults;
|
||||
}
|
||||
|
||||
public function getPassedTestResults() {
|
||||
$passedTests = array();
|
||||
foreach ($this->testResults as $testResult) {
|
||||
if ($testResult instanceof PassedTestResult) {
|
||||
$passedTests[] = $testResult;
|
||||
}
|
||||
}
|
||||
|
||||
return $passedTests;
|
||||
}
|
||||
|
||||
public function getFailedTestResults() {
|
||||
$failedTests = array();
|
||||
foreach ($this->testResults as $testResult) {
|
||||
if (!($testResult instanceof PassedTestResult)) {
|
||||
$failedTests[] = $testResult;
|
||||
}
|
||||
}
|
||||
|
||||
return $failedTests;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
77
TestMethod.php
Normal file
77
TestMethod.php
Normal file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
class TestMethod implements Testable {
|
||||
|
||||
protected $testCase;
|
||||
protected $method;
|
||||
|
||||
public function __construct(TestCase $testCase, ReflectionMethod $method) {
|
||||
$this->testCase = $testCase;
|
||||
$this->method = $method;
|
||||
}
|
||||
|
||||
public function run(array $listeners) {
|
||||
foreach ($listeners as $listener) {
|
||||
$listener->beforeTestMethod($this);
|
||||
}
|
||||
|
||||
$testPassed = false;
|
||||
$result = null;
|
||||
$failure = null;
|
||||
|
||||
$this->testCase->setUp();
|
||||
try {
|
||||
$this->method->invoke($this->testCase);
|
||||
$testPassed = true;
|
||||
} catch (TestFailure $failure) {
|
||||
$this->handleTestFailure($failure);
|
||||
} catch (Exception $e) {
|
||||
//test for expected exception
|
||||
foreach ($listeners as $listener) {
|
||||
$listener->onTestMethodErred($this);
|
||||
}
|
||||
}
|
||||
|
||||
$result = $this->createTestResult($this, $failure);
|
||||
$this->testCase->tearDown();
|
||||
|
||||
foreach ($listeners as $listener) {
|
||||
$listener->afterTestMethod($this);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function createTestResult(TestFailure $failure = null) {
|
||||
if ($failure === null) {
|
||||
return new PassedTestResult($this);
|
||||
} else if ($failure instanceof FailedTest) {
|
||||
return new FailedTestResult($this, $failure);
|
||||
} else if ($failure instanceof ErredTest) {
|
||||
return new ErredTestResult($this, $failure);
|
||||
} else if ($failure instanceof IgnoredTest) {
|
||||
return new IgnoredTestResult($this, $failure);
|
||||
}
|
||||
|
||||
throw new InvalidArgumentException('Unknown test failure type: ' . get_class($failure));
|
||||
}
|
||||
|
||||
protected function handleTestFailure(TestFailure $failure) {
|
||||
if ($failure instanceof FailedTest) {
|
||||
foreach ($listeners as $listener) {
|
||||
$listener->onTestMethodFailed($this);
|
||||
}
|
||||
} else if ($failure instanceof IgnoredTest) {
|
||||
foreach ($listeners as $listener) {
|
||||
$listener->onTestMethodIgnored($this);
|
||||
}
|
||||
} else if ($failure instanceof ErredTest) {
|
||||
foreach ($listeners as $listener) {
|
||||
$listener->onTestMethodErred($this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
abstract class TestResult {
|
||||
interface TestResult extends Countable {
|
||||
|
||||
public function __construct(TestCase $test) {
|
||||
|
||||
}
|
||||
public function passed();
|
||||
|
||||
public function failed();
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user