This commit is contained in:
tmont 2009-06-22 06:26:47 +00:00
parent 56a2360092
commit 10627f693c
3 changed files with 405 additions and 0 deletions

View File

@ -1,63 +1,233 @@
<?php <?php
/**
* Assert
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
/**
* Assertions
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
class Assert { class Assert {
/**
* Constructor
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @ignore
*/
private function __construct() {} private function __construct() {}
/**
* Evaluates the given constraint
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses Constraint::fail()
*
* @param Constraint $constraint
* @param string $message
*/
protected static function evaluate(Constraint $constraint, $message) { protected static function evaluate(Constraint $constraint, $message) {
if (!$constraint->evaluate()) { if (!$constraint->evaluate()) {
$constraint->fail($message); $constraint->fail($message);
} }
} }
/**
* Negates a constraint
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param Constraint $constraint
* @return NotConstraint
*/
protected static function negate(Constraint $constraint) { protected static function negate(Constraint $constraint) {
return new NotConstraint($constraint); return new NotConstraint($constraint);
} }
/**
* Asserts that two values are equal
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $expected
* @param mixed $actual
* @param string $message
*/
public static function equal($expected, $actual, $message = '') { public static function equal($expected, $actual, $message = '') {
self::evaluate(new EqualConstraint($expected, $actual), $message); self::evaluate(new EqualConstraint($expected, $actual), $message);
} }
/**
* Asserts that two values are not equal
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $expected
* @param mixed $actual
* @param string $message
*/
public static function notEqual($expected, $actual, $message = '') { public static function notEqual($expected, $actual, $message = '') {
self::evaluate(self::negate(new EqualConstraint($expected, $actual)), $message); self::evaluate(self::negate(new EqualConstraint($expected, $actual)), $message);
} }
/**
* Asserts that two values are identical
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $expected
* @param mixed $actual
* @param string $message
*/
public static function identical($expected, $actual, $message = '') { public static function identical($expected, $actual, $message = '') {
self::evaluate(new IdenticalConstraint($expected, $actual), $message); self::evaluate(new IdenticalConstraint($expected, $actual), $message);
} }
/**
* Asserts that two values are not identical
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $expected
* @param mixed $actual
* @param string $message
*/
public static function notIdentical($expected, $actual, $message = '') { public static function notIdentical($expected, $actual, $message = '') {
self::evaluate(self::negate(new IdenticalConstraint($expected, $actual)), $message); self::evaluate(self::negate(new IdenticalConstraint($expected, $actual)), $message);
} }
/**
* Asserts that a value is true
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $value
* @param string $message
*/
public static function isTrue($value, $message = '') { public static function isTrue($value, $message = '') {
self::evaluate(new TrueConstraint($value), $message); self::evaluate(new TrueConstraint($value), $message);
} }
/**
* Asserts that a value is false
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $value
* @param string $message
*/
public static function isFalse($value, $message = '') { public static function isFalse($value, $message = '') {
self::evaluate(new FalseConstraint($value), $message); self::evaluate(new FalseConstraint($value), $message);
} }
/**
* Asserts that a value is set
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $value
* @param string $message
*/
public static function set($value, $message = '') { public static function set($value, $message = '') {
self::evaluate(new IssetConstraint($value), $message); self::evaluate(new IssetConstraint($value), $message);
} }
/**
* Asserts that a value is not set
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $value
* @param string $message
*/
public static function notSet($value, $message = '') { public static function notSet($value, $message = '') {
self::evaluate(self::negate(new IssetConstraint($value)), $message); self::evaluate(self::negate(new IssetConstraint($value)), $message);
} }
/**
* Asserts that a value is empty
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $value
* @param string $message
*/
public static function isEmpty($value, $message = '') { public static function isEmpty($value, $message = '') {
self::evaluate(new EmptyConstraint($value), $message); self::evaluate(new EmptyConstraint($value), $message);
} }
/**
* Asserts that a value is not empty
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $value
* @param string $message
*/
public static function isNotEmpty($value, $message = '') { public static function isNotEmpty($value, $message = '') {
self::evaluate(self::negate(new EmptyConstraint($value)), $message); self::evaluate(self::negate(new EmptyConstraint($value)), $message);
} }
/**
* Asserts that a value is null
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $value
* @param string $message
*/
public static function isNull($value, $message = '') { public static function isNull($value, $message = '') {
self::evaluate(new NullConstraint($value), $message); self::evaluate(new NullConstraint($value), $message);
} }
/**
* Asserts that a value is not null
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $value
* @param string $message
*/
public static function isNotNull($value, $message = '') { public static function isNotNull($value, $message = '') {
self::evaluate(self::negate(new NullConstraint($value)), $message); self::evaluate(self::negate(new NullConstraint($value)), $message);
} }

View File

@ -1,14 +1,68 @@
<?php <?php
/**
* TestRunner
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
/**
* Base test runner
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
abstract class TestRunner implements RecursivelyCountable { abstract class TestRunner implements RecursivelyCountable {
/**
* The tests to run
*
* @var array
*/
protected $tests; protected $tests;
/**
* The subscribing listeners
*
* @var array
*/
protected $listeners; protected $listeners;
/**
* @var array
*/
protected $options; protected $options;
/**
* Time the test runner began
*
* @var float
*/
private $startTime; private $startTime;
/**
* Time the test runner ended
*
* @var float
*/
private $endTime; private $endTime;
/**
* Constructor
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param array $tests
* @param array $listeners
* @param array $options
*/
public function __construct(array $tests = array(), array $listeners = array(), array $options = array()) { public function __construct(array $tests = array(), array $listeners = array(), array $options = array()) {
$this->tests = $tests; $this->tests = $tests;
$this->listeners = $listeners; $this->listeners = $listeners;
@ -18,45 +72,135 @@
$this->endTime = -1; $this->endTime = -1;
} }
/**
* Gets the start time
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return float
*/
public final function getStartTime() { public final function getStartTime() {
return $this->startTime; return $this->startTime;
} }
/**
* Gets the end time
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return float
*/
public final function getEndTime() { public final function getEndTime() {
return $this->endTime; return $this->endTime;
} }
/**
* Gets the tests
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return array
*/
public final function getTests() { public final function getTests() {
return $this->tests; return $this->tests;
} }
/**
* Sends a warning out to all listeners
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses TestListener::onFrameworkWarning()
*
* @param string $message
*/
public final function warn($message) { public final function warn($message) {
foreach ($this->listeners as $listener) { foreach ($this->listeners as $listener) {
$listener->onFrameworkWarning($message); $listener->onFrameworkWarning($message);
} }
} }
/**
* Sends an error message out to all listeners
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses TestListener::onFrameworkError()
*
* @param string $message
*/
public final function error($message) { public final function error($message) {
foreach ($this->listeners as $listener) { foreach ($this->listeners as $listener) {
$listener->onFrameworkError($message); $listener->onFrameworkError($message);
} }
} }
/**
* Adds a test
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param Testable $test
* @return TestRunner
*/
public final function addTest(Testable $test) { public final function addTest(Testable $test) {
$this->tests[] = $test; $this->tests[] = $test;
return $this; return $this;
} }
/**
* Adds a listener
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param TestListener $listener
* @return TestRunner
*/
public final function addListener(TestListener $listener) { public final function addListener(TestListener $listener) {
$this->listeners[] = $listener; $this->listeners[] = $listener;
return $this; return $this;
} }
/**
* Sets the options
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses parseOptions()
*
* @param array $options
* @return TestRunner
*/
public final function setOptions(array $options) { public final function setOptions(array $options) {
$this->options = $this->parseOptions($options); $this->options = $this->parseOptions($options);
return $this; return $this;
} }
/**
* Parses options
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses getAllowableOptions()
*
* @param array $unparsed The unparsed options
* @throws {@link InvalidOptionException}
* @return array The parsed options
*/
protected final function parseOptions(array $unparsed) { protected final function parseOptions(array $unparsed) {
$allowedOptions = $this->getAllowableOptions(); $allowedOptions = $this->getAllowableOptions();
$options = array_fill_keys(array_key($allowedOptions), null); $options = array_fill_keys(array_key($allowedOptions), null);
@ -74,6 +218,18 @@
return $options; return $options;
} }
/**
* Runs the tests and returns the results
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses warn()
* @uses error()
* @uses Testable::run()
*
* @return array Array of {@link TestResult}s
*/
public function runTests() { public function runTests() {
$results = array(); $results = array();
foreach ($this->tests as $test) { foreach ($this->tests as $test) {
@ -91,12 +247,32 @@
return $results; return $results;
} }
/**
* Publishes test results
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses TestListener::publishTestResults()
*
* @param array $testResults Array of {@link TestResult}s
*/
public function publishResults(array $testResults) { public function publishResults(array $testResults) {
foreach ($this->listeners as $listener) { foreach ($this->listeners as $listener) {
$listener->publishTestResults($testResults); $listener->publishTestResults($testResults);
} }
} }
/**
* Runs all tests and publishes the results
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses TestListener::beforeTestRunner()
* @uses publishResults()
* @uses TestListener::afterTestRunner()
*/
public final function run() { public final function run() {
foreach ($this->listeners as $listener) { foreach ($this->listeners as $listener) {
$listener->beforeTestRunner($this); $listener->beforeTestRunner($this);
@ -111,14 +287,43 @@
} }
} }
/**
* Gets the number of tests
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return int
* @ignore
*/
public function count() { public function count() {
return count($this->tests); return count($this->tests);
} }
/**
* Gets a detailed count of all tests
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses Util::countTests()
*
* @return array The result of Util::countTests()
*/
public function getTestCount() { public function getTestCount() {
return Util::countTests($this->tests); return Util::countTests($this->tests);
} }
/**
* Gets all allowable options for this test runner
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return array
*/
protected abstract function getAllowableOptions(); protected abstract function getAllowableOptions();
} }

View File

@ -1,7 +1,37 @@
<?php <?php
/**
* Test runners
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
/**
* Test runner for the console
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
class ConsoleTestRunner extends TestRunner { class ConsoleTestRunner extends TestRunner {
/**
* Gets allowable options
*
* Currently supported options:
* - recursive (boolean)
* - bootstrap (string)
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return array
*/
protected function getAllowableOptions() { protected function getAllowableOptions() {
return array( return array(
'recursive' => 'boolean', 'recursive' => 'boolean',