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
/**
* 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 {
/**
* Fails the constraint
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param string $message
* @throws {@link FailedTest}
*/
public function fail($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) {
$message = !empty($message) ? $message . "\n" : '';
$message .= 'Failed asserting that ' . $this->getFailureMessage();
return $message;
}
/**
* Evaluates the constraint
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public abstract function evaluate();
/**
* Gets the failure message
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected abstract function getFailureMessage();
}

View File

@ -1,10 +1,48 @@
<?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 {
/**
* The expected value
*
* @var mixed
*/
protected $expected;
/**
* The actual value
*
* @var mixed
*/
protected $actual;
/**
* Constructor
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $expected
* @param mixed $actual
*/
public function __construct($expected, $actual) {
$this->expected = $expected;
$this->actual = $actual;

View File

@ -1,11 +1,46 @@
<?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 {
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public function evaluate() {
return $this->expected == $this->actual;
}
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected function getFailureMessage() {
return Util::export($this->actual) . ' is equal to ' . Util::export($this->expected);
}

View File

@ -1,11 +1,46 @@
<?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 {
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return mixed
*/
public function evaluate() {
return $this->expected === $this->actual;
}
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected function getFailureMessage() {
return Util::export($this->actual) . ' is identical to ' . Util::export($this->expected);
}

View File

@ -1,29 +1,110 @@
<?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 {
/**
* The constraint to negate
*
* @var mixed
*/
protected $constraint;
/**
* Constructor
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param Constraint $constraint The constraint to negate
*/
public function __construct(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 = '') {
throw new FailedTest($this->toString($message));
}
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public function 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) {
return $this->negateString($this->constraint->toString($message));
}
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @throws BadMethodCallException
*/
protected function getFailureMessage() {
throw new BadMethodCallException();
}
/**
* Negates a string
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param string $string
* @return string
*/
protected function negateString($string) {
return str_replace(
array(

View File

@ -1,69 +1,230 @@
<?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 {
/**
* The value to evaluate
*
* @var mixed
*/
protected $value;
/**
* Constructor
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @param mixed $value
*/
public function __construct($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 {
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public function evaluate() {
return isset($this->value);
}
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected function getFailureMessage() {
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 {
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public function evaluate() {
return empty($this->value);
}
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected function getFailureMessage() {
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 {
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public function evaluate() {
return $this->value === null;
}
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected function getFailureMessage() {
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 {
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public function evaluate() {
return $this->value === true;
}
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected function getFailureMessage() {
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 {
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return bool
*/
public function evaluate() {
return $this->value === false;
}
/**
* {@inheritdoc}
*
* @author Tommy Montgomery
* @version 1.0
* @since 1.0
*
* @return string
*/
protected function getFailureMessage() {
return Util::export($this->value) . ' is false';
}