This commit is contained in:
tmont 2009-06-22 09:03:08 +00:00
parent 01a02e892f
commit e8f4f8cd7e
6 changed files with 406 additions and 0 deletions

View File

@ -1,19 +1,75 @@
<?php <?php
/**
* Constraint
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
/**
* Base constraint
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
abstract class Constraint { abstract class Constraint {
/**
* Fails the constraint
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param string $message
* @throws {@link FailedTest}
*/
public function fail($message = '') { public function fail($message = '') {
throw new FailedTest($this->toString($message)); throw new FailedTest($this->toString($message));
} }
/**
* Gets a string representation of the constraint
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses getFailureMessage()
*
* @param string $message
* @return string
*/
public function toString($message) { public function toString($message) {
$message = !empty($message) ? $message . "\n" : ''; $message = !empty($message) ? $message . "\n" : '';
$message .= 'Failed asserting that ' . $this->getFailureMessage(); $message .= 'Failed asserting that ' . $this->getFailureMessage();
return $message; return $message;
} }
/**
* Evaluates the constraint
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public abstract function evaluate(); public abstract function evaluate();
/**
* Gets the failure message
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected abstract function getFailureMessage(); protected abstract function getFailureMessage();
} }

View File

@ -1,10 +1,48 @@
<?php <?php
/**
* DefaultConstraint
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
/**
* Default constraint
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
abstract class DefaultConstraint extends Constraint { abstract class DefaultConstraint extends Constraint {
/**
* The expected value
*
* @var mixed
*/
protected $expected; protected $expected;
/**
* The actual value
*
* @var mixed
*/
protected $actual; protected $actual;
/**
* Constructor
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $expected
* @param mixed $actual
*/
public function __construct($expected, $actual) { public function __construct($expected, $actual) {
$this->expected = $expected; $this->expected = $expected;
$this->actual = $actual; $this->actual = $actual;

View File

@ -1,11 +1,46 @@
<?php <?php
/**
* EqualConstraint
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
/**
* Constraint for asserting that two objects are equal
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
class EqualConstraint extends DefaultConstraint { class EqualConstraint extends DefaultConstraint {
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public function evaluate() { public function evaluate() {
return $this->expected == $this->actual; return $this->expected == $this->actual;
} }
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected function getFailureMessage() { protected function getFailureMessage() {
return Util::export($this->actual) . ' is equal to ' . Util::export($this->expected); return Util::export($this->actual) . ' is equal to ' . Util::export($this->expected);
} }

View File

@ -1,11 +1,46 @@
<?php <?php
/**
* IdenticalConstraint
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
/**
* Constraint for asserting that two objects are identical
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
class IdenticalConstraint extends DefaultConstraint { class IdenticalConstraint extends DefaultConstraint {
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return mixed
*/
public function evaluate() { public function evaluate() {
return $this->expected === $this->actual; return $this->expected === $this->actual;
} }
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected function getFailureMessage() { protected function getFailureMessage() {
return Util::export($this->actual) . ' is identical to ' . Util::export($this->expected); return Util::export($this->actual) . ' is identical to ' . Util::export($this->expected);
} }

View File

@ -1,29 +1,110 @@
<?php <?php
/**
* NotConstraint
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
/**
* Decorator for negating a constraint
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
class NotConstraint extends Constraint { class NotConstraint extends Constraint {
/**
* The constraint to negate
*
* @var mixed
*/
protected $constraint; protected $constraint;
/**
* Constructor
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param Constraint $constraint The constraint to negate
*/
public function __construct(Constraint $constraint) { public function __construct(Constraint $constraint) {
$this->constraint = $constraint; $this->constraint = $constraint;
} }
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param string $message
* @throws {@link FailedTest}
*/
public function fail($message = '') { public function fail($message = '') {
throw new FailedTest($this->toString($message)); throw new FailedTest($this->toString($message));
} }
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public function evaluate() { public function evaluate() {
return !$this->constraint->evaluate(); return !$this->constraint->evaluate();
} }
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
* @uses negateString()
* @uses Constraint::toString()
*
* @param string $message
* @return string
*/
public function toString($message) { public function toString($message) {
return $this->negateString($this->constraint->toString($message)); return $this->negateString($this->constraint->toString($message));
} }
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @throws BadMethodCallException
*/
protected function getFailureMessage() { protected function getFailureMessage() {
throw new BadMethodCallException(); throw new BadMethodCallException();
} }
/**
* Negates a string
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param string $string
* @return string
*/
protected function negateString($string) { protected function negateString($string) {
return str_replace( return str_replace(
array( array(

View File

@ -1,69 +1,230 @@
<?php <?php
/**
* Constraints that only take one value
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
/**
* Base class for simple constraints that only require one value
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
abstract class SimpleConstraint extends Constraint { abstract class SimpleConstraint extends Constraint {
/**
* The value to evaluate
*
* @var mixed
*/
protected $value; protected $value;
/**
* Constructor
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $value
*/
public function __construct($value) { public function __construct($value) {
$this->value = $value; $this->value = $value;
} }
} }
/**
* Constraint for asserting that a value is set
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
class IssetConstraint extends SimpleConstraint { class IssetConstraint extends SimpleConstraint {
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public function evaluate() { public function evaluate() {
return isset($this->value); return isset($this->value);
} }
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected function getFailureMessage() { protected function getFailureMessage() {
return Util::export($this->value) . ' is set'; return Util::export($this->value) . ' is set';
} }
} }
/**
* Constraint for asserting that a value is empty
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
class EmptyConstraint extends SimpleConstraint { class EmptyConstraint extends SimpleConstraint {
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public function evaluate() { public function evaluate() {
return empty($this->value); return empty($this->value);
} }
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected function getFailureMessage() { protected function getFailureMessage() {
return Util::export($this->value) . ' is empty'; return Util::export($this->value) . ' is empty';
} }
} }
/**
* Constraint for asserting that a value is null
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
class NullConstraint extends SimpleConstraint { class NullConstraint extends SimpleConstraint {
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public function evaluate() { public function evaluate() {
return $this->value === null; return $this->value === null;
} }
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected function getFailureMessage() { protected function getFailureMessage() {
return Util::export($this->value) . ' is null'; return Util::export($this->value) . ' is null';
} }
} }
/**
* Constraint for asserting that a value is true
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
class TrueConstraint extends SimpleConstraint { class TrueConstraint extends SimpleConstraint {
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public function evaluate() { public function evaluate() {
return $this->value === true; return $this->value === true;
} }
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected function getFailureMessage() { protected function getFailureMessage() {
return Util::export($this->value) . ' is true'; return Util::export($this->value) . ' is true';
} }
} }
/**
* Constraint for asserting that a value is true
*
* @package TUnit
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*/
class FalseConstraint extends SimpleConstraint { class FalseConstraint extends SimpleConstraint {
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public function evaluate() { public function evaluate() {
return $this->value === false; return $this->value === false;
} }
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected function getFailureMessage() { protected function getFailureMessage() {
return Util::export($this->value) . ' is false'; return Util::export($this->value) . ' is false';
} }