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
/**
* 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 {
/**
* Constructor
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @ignore
*/
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) {
if (!$constraint->evaluate()) {
$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) {
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 = '') {
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 = '') {
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 = '') {
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 = '') {
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 = '') {
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 = '') {
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 = '') {
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 = '') {
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 = '') {
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 = '') {
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 = '') {
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 = '') {
self::evaluate(self::negate(new NullConstraint($value)), $message);
}

View File

@ -1,14 +1,68 @@
<?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 {
/**
* The tests to run
*
* @var array
*/
protected $tests;
/**
* The subscribing listeners
*
* @var array
*/
protected $listeners;
/**
* @var array
*/
protected $options;
/**
* Time the test runner began
*
* @var float
*/
private $startTime;
/**
* Time the test runner ended
*
* @var float
*/
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()) {
$this->tests = $tests;
$this->listeners = $listeners;
@ -18,45 +72,135 @@
$this->endTime = -1;
}
/**
* Gets the start time
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return float
*/
public final function getStartTime() {
return $this->startTime;
}
/**
* Gets the end time
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return float
*/
public final function getEndTime() {
return $this->endTime;
}
/**
* Gets the tests
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return array
*/
public final function getTests() {
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) {
foreach ($this->listeners as $listener) {
$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) {
foreach ($this->listeners as $listener) {
$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) {
$this->tests[] = $test;
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) {
$this->listeners[] = $listener;
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) {
$this->options = $this->parseOptions($options);
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) {
$allowedOptions = $this->getAllowableOptions();
$options = array_fill_keys(array_key($allowedOptions), null);
@ -74,6 +218,18 @@
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() {
$results = array();
foreach ($this->tests as $test) {
@ -91,12 +247,32 @@
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) {
foreach ($this->listeners as $listener) {
$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() {
foreach ($this->listeners as $listener) {
$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() {
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() {
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();
}

View File

@ -1,7 +1,37 @@
<?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 {
/**
* Gets allowable options
*
* Currently supported options:
* - recursive (boolean)
* - bootstrap (string)
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return array
*/
protected function getAllowableOptions() {
return array(
'recursive' => 'boolean',