Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More work on PHPUnit_Framework_Constraint_*.

  • Loading branch information...
commit e4ddabeb7b4f357a8cb5b809d74f5059abbfa251 1 parent 1ce7fe4
@sebastianbergmann authored
View
81 PHPUnit/Framework/Assert.php
@@ -95,7 +95,7 @@ public static function assertArrayHasKey($key, array $array, $message = '')
$constraint = new PHPUnit_Framework_Constraint_ArrayHasKey($key);
if (!$constraint->evaluate($array)) {
- self::failConstraint($constraint, $array, $message);
+ $constraint->fail($array, $message);
}
}
@@ -120,7 +120,7 @@ public static function assertArrayNotHasKey($key, array $array, $message = '')
);
if (!$constraint->evaluate($array)) {
- self::failConstraint($constraint, $array, $message);
+ $constraint->fail($array, $message);
}
}
@@ -150,7 +150,7 @@ public static function assertContains($needle, $haystack, $message = '')
}
if (!$constraint->evaluate($haystack)) {
- self::failConstraint($constraint, $haystack, $message);
+ $constraint->fail($haystack, $message);
}
}
@@ -209,7 +209,7 @@ public static function assertNotContains($needle, $haystack, $message = '')
}
if (!$constraint->evaluate($haystack)) {
- self::failConstraint($constraint, $haystack, $message);
+ $constraint->fail($haystack, $message);
}
}
@@ -258,7 +258,7 @@ public static function assertEquals($expected, $actual, $message = '', $delta =
);
if (!$constraint->evaluate($actual)) {
- self::failConstraint($constraint, $actual, $message);
+ $constraint->fail($actual, $message);
}
}
@@ -312,7 +312,7 @@ public static function assertNotEquals($expected, $actual, $message = '', $delta
);
if (!$constraint->evaluate($actual)) {
- self::failConstraint($constraint, $actual, $message);
+ $constraint->fail($actual, $message);
}
}
@@ -361,7 +361,7 @@ public static function assertFileExists($filename, $message = '')
$constraint = new PHPUnit_Framework_Constraint_FileExists;
if (!$constraint->evaluate($filename)) {
- self::failConstraint($constraint, $filename, $message);
+ $constraint->fail($filename, $message);
}
}
@@ -385,7 +385,7 @@ public static function assertFileNotExists($filename, $message = '')
);
if (!$constraint->evaluate($filename)) {
- self::failConstraint($constraint, $filename, $message);
+ $constraint->fail($filename, $message);
}
}
@@ -408,7 +408,7 @@ public static function assertTrue($condition, $message = '')
$constraint = new PHPUnit_Framework_Constraint_IsIdentical(TRUE);
if (!$constraint->evaluate($condition)) {
- self::failConstraint($constraint, $condition, $message);
+ $constraint->fail($condition, $message);
}
}
@@ -431,7 +431,7 @@ public static function assertFalse($condition, $message = '')
$constraint = new PHPUnit_Framework_Constraint_IsIdentical(FALSE);
if (!$constraint->evaluate($condition)) {
- self::failConstraint($constraint, $condition, $message);
+ $constraint->fail($condition, $message);
}
}
@@ -450,7 +450,7 @@ public static function assertNotNull($actual, $message = '')
);
if (!$constraint->evaluate($actual)) {
- self::failConstraint($constraint, $actual, $message);
+ $constraint->fail($actual, $message);
}
}
@@ -467,7 +467,7 @@ public static function assertNull($actual, $message = '')
$constraint = new PHPUnit_Framework_Constraint_IsIdentical(NULL);
if (!$constraint->evaluate($actual)) {
- self::failConstraint($constraint, $actual, $message);
+ $constraint->fail($actual, $message);
}
}
@@ -490,7 +490,7 @@ public static function assertObjectHasAttribute($attributeName, $object, $messag
$constraint = new PHPUnit_Framework_Constraint_ObjectHasAttribute($attributeName);
if (!$constraint->evaluate($object)) {
- self::failConstraint($constraint, $object, $message);
+ $constraint->fail($object, $message);
}
}
@@ -515,7 +515,7 @@ public static function assertObjectNotHasAttribute($attributeName, $object, $mes
);
if (!$constraint->evaluate($object)) {
- self::failConstraint($constraint, $object, $message);
+ $constraint->fail($object, $message);
}
}
@@ -535,7 +535,7 @@ public static function assertSame($expected, $actual, $message = '')
$constraint = new PHPUnit_Framework_Constraint_IsIdentical($expected);
if (!$constraint->evaluate($actual)) {
- self::failConstraint($constraint, $actual, $message);
+ $constraint->fail($actual, $message);
}
}
@@ -581,7 +581,7 @@ public static function assertNotSame($expected, $actual, $message = '')
);
if (!$constraint->evaluate($actual)) {
- self::failConstraint($constraint, $actual, $message);
+ $constraint->fail($actual, $message);
}
}
@@ -623,9 +623,8 @@ public static function assertType($expected, $actual, $message = '')
if (is_string($expected)) {
if (class_exists($expected, FALSE) ||
interface_exists($expected, FALSE)) {
- $constraint = self::logicalAnd(
- new PHPUnit_Framework_Constraint_IsType('object'),
- new PHPUnit_Framework_Constraint_IsInstanceOf($expected)
+ $constraint = new PHPUnit_Framework_Constraint_IsInstanceOf(
+ $expected
);
} else {
$constraint = new PHPUnit_Framework_Constraint_IsType($expected);
@@ -635,7 +634,7 @@ public static function assertType($expected, $actual, $message = '')
}
if (!$constraint->evaluate($actual)) {
- self::failConstraint($constraint, $actual, $message);
+ $constraint->fail($actual, $message);
}
}
@@ -655,10 +654,7 @@ public static function assertNotType($expected, $actual, $message = '')
if (class_exists($expected, FALSE) ||
interface_exists($expected, FALSE)) {
$constraint = new PHPUnit_Framework_Constraint_Not(
- self::logicalAnd(
- new PHPUnit_Framework_Constraint_IsType('object'),
- new PHPUnit_Framework_Constraint_IsInstanceOf($expected)
- )
+ new PHPUnit_Framework_Constraint_IsInstanceOf($expected)
);
} else {
$constraint = new PHPUnit_Framework_Constraint_Not(
@@ -670,7 +666,7 @@ public static function assertNotType($expected, $actual, $message = '')
}
if (!$constraint->evaluate($actual)) {
- self::failConstraint($constraint, $actual, $message);
+ $constraint->fail($actual, $message);
}
}
@@ -692,7 +688,7 @@ public static function assertRegExp($pattern, $string, $message = '')
$constraint = new PHPUnit_Framework_Constraint_PCREMatch($pattern);
if (!$constraint->evaluate($string)) {
- self::failConstraint($constraint, $string, $message);
+ $constraint->fail($string, $message);
}
}
@@ -717,7 +713,7 @@ public static function assertNotRegExp($pattern, $string, $message = '')
);
if (!$constraint->evaluate($string)) {
- self::failConstraint($constraint, $string, $message);
+ $constraint->fail($string, $message);
}
}
@@ -734,7 +730,7 @@ public static function assertNotRegExp($pattern, $string, $message = '')
public static function assertThat($value, PHPUnit_Framework_Constraint $constraint, $message = '')
{
if (!$constraint->evaluate($value)) {
- self::failConstraint($constraint, $value, $message);
+ $constraint->fail($value, $message);
}
}
@@ -986,35 +982,6 @@ public static function fail($message = '')
}
/**
- * Fails a test based on a failed constraint.
- *
- * @param PHPUnit_Framework_Constraint $constraint
- * @param mixed $value
- * @param string $message
- * @throws PHPUnit_Framework_ExpectationFailedException
- * @access public
- * @static
- * @since Method available since Release 3.0.0
- */
- public static function failConstraint(PHPUnit_Framework_Constraint $constraint, $value, $message)
- {
- if (!empty($message)) {
- $message .= "\n";
- }
-
- $constraint->fail(
- $value,
- sprintf(
- '%sFailed asserting that %s %s.',
-
- $message,
- PHPUnit_Util_Type::toString($value),
- $constraint->toString()
- )
- );
- }
-
- /**
* Returns the value of an object's attribute.
* This also works for attributes that are declared protected or private.
*
View
58 PHPUnit/Framework/Constraint.php
@@ -53,15 +53,7 @@
if (!interface_exists('PHPUnit_Framework_Constraint')) {
/**
- * Interface for constraints which are placed upon any value.
- *
- * The constraint can be used in method name matching and parameter value matching
- * to perform more advanced checking than simply matching two values with ==.
- *
- * A constraint must provides the following methods:
- * - evaluate() check if a given object meets the constraint. If it does
- * not, fail() can be called to create an exception.
- * - toStrint() returns a description of the constraint.
+ * Abstract base class for constraints. which are placed upon any value.
*
* @category Testing
* @package PHPUnit
@@ -73,16 +65,17 @@
* @link http://www.phpunit.de/
* @since Interface available since Release 3.0.0
*/
-interface PHPUnit_Framework_Constraint extends PHPUnit_Framework_SelfDescribing
+abstract class PHPUnit_Framework_Constraint implements PHPUnit_Framework_SelfDescribing
{
/**
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
+ * @abstract
*/
- public function evaluate($other);
+ abstract public function evaluate($other);
/**
* Creates the appropriate exception for the constraint which can be caught
@@ -95,7 +88,46 @@ public function evaluate($other);
* @param boolean $not Flag to indicate negation.
* @throws PHPUnit_Framework_ExpectationFailedException
*/
- public function fail($other, $description, $not = FALSE);
+ public function fail($other, $description, $not = FALSE)
+ {
+ if (!empty($description)) {
+ $description .= "\n";
+ }
+
+ $failureDescription = sprintf(
+ '%sFailed asserting that %s %s.',
+
+ $description,
+ PHPUnit_Util_Type::toString($other),
+ $this->toString()
+ );
+
+ if ($not) {
+ $failureDescription = str_replace(
+ array(
+ ' anything ',
+ ' contains ',
+ ' exists ',
+ ' has ',
+ ' is ',
+ ' matches '
+ ),
+ array(
+ ' nothing ',
+ ' not contains ',
+ ' not exists ',
+ ' has not ',
+ ' is not ',
+ ' not matches '
+ ),
+ $failureDescription
+ );
+ }
+
+ throw new PHPUnit_Framework_ExpectationFailedException(
+ $failureDescription
+ );
+ }
}
}
View
37 PHPUnit/Framework/Constraint/And.php
@@ -67,11 +67,11 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_And implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_And extends PHPUnit_Framework_Constraint
{
private $constraints = array();
- private $failedConstraint = NULL;
+ private $lastConstraint = NULL;
public function setConstraints(array $constraints)
{
@@ -90,17 +90,17 @@ public function setConstraints(array $constraints)
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
{
- $this->failedConstraint = NULL;
+ $this->lastConstraint = NULL;
- foreach($this->constraints as $key => $constraint) {
- if (!$constraint->evaluate($other)) {
- $this->failedConstraint = $constraint;
+ foreach($this->constraints as $constraint) {
+ $this->lastConstraint = $constraint;
+ if (!$constraint->evaluate($other)) {
return FALSE;
}
}
@@ -118,28 +118,7 @@ public function evaluate($other)
*/
public function fail($other, $description, $not = FALSE)
{
- if (!empty($description)) {
- $description .= "\n";
- }
-
- $description = sprintf(
- "%sExpected that %s %s",
-
- $description,
- PHPUnit_Util_Type::toString($other),
- $this->toString()
- );
-
- if ($this->failedConstraint !== NULL) {
- $this->failedConstraint->fail(
- $other,
- $description
- );
- } else {
- throw new PHPUnit_Framework_ExpectationFailedException(
- $description
- );
- }
+ $this->lastConstraint->fail($other, $description, $not);
}
/**
View
35 PHPUnit/Framework/Constraint/ArrayHasKey.php
@@ -69,7 +69,7 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_ArrayHasKey implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_ArrayHasKey extends PHPUnit_Framework_Constraint
{
private $key;
@@ -82,7 +82,7 @@ public function __construct($key)
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
@@ -91,31 +91,6 @@ public function evaluate($other)
}
/**
- * @param mixed $other The value passed to evaluate() which failed the
- * constraint check.
- * @param string $description A string with extra description of what was
- * going on while the evaluation failed.
- * @param boolean $not Flag to indicate negation.
- * @throws PHPUnit_Framework_ExpectationFailedException
- */
- public function fail($other, $description, $not = FALSE)
- {
- if (!empty($description)) {
- $description .= "\n";
- }
-
- throw new PHPUnit_Framework_ExpectationFailedException(
- sprintf(
- '%sExpected array key %s not found in %s.',
-
- $description,
- PHPUnit_Util_Type::toString($this->key),
- PHPUnit_Util_Type::toString($other)
- )
- );
- }
-
- /**
* Returns a string representation of the constraint.
*
* @return string
@@ -123,11 +98,7 @@ public function fail($other, $description, $not = FALSE)
*/
public function toString()
{
- return sprintf(
- 'array has key %s',
-
- PHPUnit_Util_Type::toString($this->key)
- );
+ return 'has key ' . PHPUnit_Util_Type::toString($this->key);
}
}
?>
View
10 PHPUnit/Framework/Constraint/FileExists.php
@@ -65,13 +65,13 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_FileExists implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_FileExists extends PHPUnit_Framework_Constraint
{
/**
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
@@ -95,10 +95,10 @@ public function fail($other, $description, $not = FALSE)
throw new PHPUnit_Framework_ExpectationFailedException(
sprintf(
- '%sFile "%s" does not exist.',
+ '%sFailed asserting that file "%s" exists.',
- $description,
- $other
+ $description,
+ $other
)
);
}
View
39 PHPUnit/Framework/Constraint/GreaterThan.php
@@ -47,6 +47,7 @@
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/Util/Filter.php';
+require_once 'PHPUnit/Util/Type.php';
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
@@ -65,7 +66,7 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_GreaterThan implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_GreaterThan extends PHPUnit_Framework_Constraint
{
private $value;
@@ -78,7 +79,7 @@ public function __construct($value)
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
@@ -87,28 +88,6 @@ public function evaluate($other)
}
/**
- * @param mixed $other The value passed to evaluate() which failed the
- * constraint check.
- * @param string $description A string with extra description of what was
- * going on while the evaluation failed.
- * @param boolean $not Flag to indicate negation.
- * @throws PHPUnit_Framework_ExpectationFailedException
- */
- public function fail($other, $description, $not = FALSE)
- {
- if (!$not) {
- throw new PHPUnit_Framework_ExpectationFailedException(
- $description,
- PHPUnit_Framework_ComparisonFailure::diffIdentical($this->value, $other)
- );
- } else {
- throw new PHPUnit_Framework_ExpectationFailedException(
- $description
- );
- }
- }
-
- /**
* Returns a string representation of the constraint.
*
* @return string
@@ -116,17 +95,7 @@ public function fail($other, $description, $not = FALSE)
*/
public function toString()
{
- $type = '';
-
- if (!is_null($this->value)) {
- $type = gettype($this->value) . ':';
- }
-
- return sprintf(
- 'is greater than <%s>',
-
- $type . print_r($this->value, TRUE)
- );
+ return 'is greater than ' . PHPUnit_Util_Type::toString($this->value);
}
}
?>
View
4 PHPUnit/Framework/Constraint/IsAnything.php
@@ -63,13 +63,13 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_IsAnything implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_IsAnything extends PHPUnit_Framework_Constraint
{
/**
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
View
22 PHPUnit/Framework/Constraint/IsEqual.php
@@ -71,7 +71,7 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_IsEqual implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_IsEqual extends PHPUnit_Framework_Constraint
{
private $value;
private $delta = 0;
@@ -88,7 +88,7 @@ public function __construct($value, $delta = 0, $maxDepth = 10)
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
@@ -106,14 +106,26 @@ public function evaluate($other)
*/
public function fail($other, $description, $not = FALSE)
{
+ if (!empty($description)) {
+ $description .= "\n";
+ }
+
+ $failDescription = sprintf(
+ '%sFailed asserting that %s %s.',
+
+ $description,
+ PHPUnit_Util_Type::toString($other),
+ $this->toString()
+ );
+
if (!$not) {
throw new PHPUnit_Framework_ExpectationFailedException(
- $description,
- PHPUnit_Framework_ComparisonFailure::diffIdentical($this->value, $other)
+ $failDescription,
+ PHPUnit_Framework_ComparisonFailure::diffEqual($this->value, $other)
);
} else {
throw new PHPUnit_Framework_ExpectationFailedException(
- $description
+ $failDescription
);
}
}
View
26 PHPUnit/Framework/Constraint/IsIdentical.php
@@ -70,7 +70,7 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_IsIdentical implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_IsIdentical extends PHPUnit_Framework_Constraint
{
private $value;
@@ -83,7 +83,7 @@ public function __construct($value)
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
@@ -101,14 +101,26 @@ public function evaluate($other)
*/
public function fail($other, $description, $not = FALSE)
{
+ if (!empty($description)) {
+ $description .= "\n";
+ }
+
+ $failDescription = sprintf(
+ '%sFailed asserting that %s %s.',
+
+ $description,
+ PHPUnit_Util_Type::toString($other),
+ $this->toString()
+ );
+
if (!$not) {
throw new PHPUnit_Framework_ExpectationFailedException(
- $description,
+ $failDescription,
PHPUnit_Framework_ComparisonFailure::diffIdentical($this->value, $other)
);
} else {
throw new PHPUnit_Framework_ExpectationFailedException(
- $description
+ $failDescription
);
}
}
@@ -121,11 +133,7 @@ public function fail($other, $description, $not = FALSE)
*/
public function toString()
{
- return sprintf(
- 'is identical to %s',
-
- PHPUnit_Util_Type::toString($this->value)
- );
+ return 'is identical to ' . PHPUnit_Util_Type::toString($this->value);
}
}
?>
View
43 PHPUnit/Framework/Constraint/IsInstanceOf.php
@@ -66,7 +66,7 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_IsInstanceOf implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_IsInstanceOf extends PHPUnit_Framework_Constraint
{
private $className;
@@ -79,7 +79,7 @@ public function __construct($className)
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
@@ -88,43 +88,6 @@ public function evaluate($other)
}
/**
- * @param mixed $other The value passed to evaluate() which failed the
- * constraint check.
- * @param string $description A string with extra description of what was
- * going on while the evaluation failed.
- * @param boolean $not Flag to indicate negation.
- * @throws PHPUnit_Framework_ExpectationFailedException
- */
- public function fail($other, $description, $not = FALSE)
- {
- if (!empty($description)) {
- $description .= "\n";
- }
-
- if (!is_object($other)) {
- throw new PHPUnit_Framework_ExpectationFailedException(
- sprintf(
- '%sExpected object of class "%s", got %s.',
-
- $description,
- $this->className,
- PHPUnit_Util_Type::toString($other)
- )
- );
- } else {
- throw new PHPUnit_Framework_ExpectationFailedException(
- sprintf(
- '%sExpected object of class "%s", got object of class "%s".',
-
- $description,
- $this->className,
- get_class($other)
- )
- );
- }
- }
-
- /**
* Returns a string representation of the constraint.
*
* @return string
@@ -133,7 +96,7 @@ public function fail($other, $description, $not = FALSE)
public function toString()
{
return sprintf(
- 'is instance of <%s>',
+ 'is instance of class "%s"',
$this->className
);
View
21 PHPUnit/Framework/Constraint/IsType.php
@@ -65,7 +65,7 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_IsType implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_IsType extends PHPUnit_Framework_Constraint
{
private $type;
@@ -102,7 +102,7 @@ public function __construct($type)
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
@@ -141,21 +141,6 @@ public function evaluate($other)
}
/**
- * @param mixed $other The value passed to evaluate() which failed the
- * constraint check.
- * @param string $description A string with extra description of what was
- * going on while the evaluation failed.
- * @param boolean $not Flag to indicate negation.
- * @throws PHPUnit_Framework_ExpectationFailedException
- */
- public function fail($other, $description, $not = FALSE)
- {
- throw new PHPUnit_Framework_ExpectationFailedException(
- $description
- );
- }
-
- /**
* Returns a string representation of the constraint.
*
* @return string
@@ -164,7 +149,7 @@ public function fail($other, $description, $not = FALSE)
public function toString()
{
return sprintf(
- 'is type <%s>',
+ 'is of type "%s"',
$this->type
);
View
39 PHPUnit/Framework/Constraint/LessThan.php
@@ -47,6 +47,7 @@
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/Util/Filter.php';
+require_once 'PHPUnit/Util/Type.php';
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
@@ -65,7 +66,7 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_LessThan implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_LessThan extends PHPUnit_Framework_Constraint
{
private $value;
@@ -78,7 +79,7 @@ public function __construct($value)
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
@@ -87,28 +88,6 @@ public function evaluate($other)
}
/**
- * @param mixed $other The value passed to evaluate() which failed the
- * constraint check.
- * @param string $description A string with extra description of what was
- * going on while the evaluation failed.
- * @param boolean $not Flag to indicate negation.
- * @throws PHPUnit_Framework_ExpectationFailedException
- */
- public function fail($other, $description, $not = FALSE)
- {
- if (!$not) {
- throw new PHPUnit_Framework_ExpectationFailedException(
- $description,
- PHPUnit_Framework_ComparisonFailure::diffIdentical($this->value, $other)
- );
- } else {
- throw new PHPUnit_Framework_ExpectationFailedException(
- $description
- );
- }
- }
-
- /**
* Returns a string representation of the constraint.
*
* @return string
@@ -116,17 +95,7 @@ public function fail($other, $description, $not = FALSE)
*/
public function toString()
{
- $type = '';
-
- if (!is_null($this->value)) {
- $type = gettype($this->value) . ':';
- }
-
- return sprintf(
- 'is less than <%s>',
-
- $type . print_r($this->value, TRUE)
- );
+ return 'is less than ' . PHPUnit_Util_Type::toString($this->value);
}
}
?>
View
39 PHPUnit/Framework/Constraint/Not.php
@@ -69,7 +69,7 @@
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_Not implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_Not extends PHPUnit_Framework_Constraint
{
private $constraint;
@@ -86,7 +86,7 @@ public function __construct($constraint)
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
@@ -115,11 +115,36 @@ public function fail($other, $description, $not = FALSE)
*/
public function toString()
{
- return str_replace(
- 'not is',
- 'is not',
- 'not ' . $this->constraint->toString()
- );
+ switch (get_class($this->constraint)) {
+ case 'PHPUnit_Framework_Constraint_And':
+ case 'PHPUnit_Framework_Constraint_Not':
+ case 'PHPUnit_Framework_Constraint_Or': {
+ return 'not( ' . $this->constraint->toString() . ' )';
+ }
+ break;
+
+ default: {
+ return str_replace(
+ array(
+ ' anything ',
+ ' contains ',
+ ' exists ',
+ ' has ',
+ ' is ',
+ ' matches '
+ ),
+ array(
+ ' nothing ',
+ ' not contains ',
+ ' not exists ',
+ ' has not ',
+ ' is not ',
+ ' not matches '
+ ),
+ $this->constraint->toString()
+ );
+ }
+ }
}
}
?>
View
31 PHPUnit/Framework/Constraint/ObjectHasAttribute.php
@@ -65,7 +65,7 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_ObjectHasAttribute implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_ObjectHasAttribute extends PHPUnit_Framework_Constraint
{
private $attributeName;
@@ -78,7 +78,7 @@ public function __construct($attributeName)
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
@@ -89,31 +89,6 @@ public function evaluate($other)
}
/**
- * @param mixed $other The value passed to evaluate() which failed the
- * constraint check.
- * @param string $description A string with extra description of what was
- * going on while the evaluation failed.
- * @param boolean $not Flag to indicate negation.
- * @throws PHPUnit_Framework_ExpectationFailedException
- */
- public function fail($other, $description, $not = FALSE)
- {
- if (!empty($description)) {
- $description .= "\n";
- }
-
- throw new PHPUnit_Framework_ExpectationFailedException(
- sprintf(
- '%sExpected attribute "%s" not found in %s.',
-
- $description,
- $this->attributeName,
- PHPUnit_Util_Type::toString($other)
- )
- );
- }
-
- /**
* Returns a string representation of the constraint.
*
* @return string
@@ -122,7 +97,7 @@ public function fail($other, $description, $not = FALSE)
public function toString()
{
return sprintf(
- 'object has attribute "%s"',
+ 'has attribute "%s"',
$this->attributeName
);
View
13 PHPUnit/Framework/Constraint/Or.php
@@ -67,7 +67,7 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_Or implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_Or extends PHPUnit_Framework_Constraint
{
private $constraints = array();
@@ -88,12 +88,12 @@ public function setConstraints(array $constraints)
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
{
- foreach($this->constraints as $key => $constraint) {
+ foreach($this->constraints as $constraint) {
if ($constraint->evaluate($other)) {
return TRUE;
}
@@ -118,11 +118,10 @@ public function fail($other, $description, $not = FALSE)
throw new PHPUnit_Framework_ExpectationFailedException(
sprintf(
- "%sExpected that %s %s",
+ '%sFailed asserting that %s.',
- $description,
- PHPUnit_Util_Type::toString($other),
- $this->toString()
+ $description,
+ $this->toString()
)
);
}
View
33 PHPUnit/Framework/Constraint/PCREMatch.php
@@ -69,7 +69,7 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_PCREMatch implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_PCREMatch extends PHPUnit_Framework_Constraint
{
private $pattern;
@@ -82,7 +82,7 @@ public function __construct($pattern)
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
@@ -91,33 +91,6 @@ public function evaluate($other)
}
/**
- * @param mixed $other The value passed to evaluate() which failed the
- * constraint check.
- * @param string $description A string with extra description of what was
- * going on while the evaluation failed.
- * @param boolean $not Flag to indicate negation.
- * @throws PHPUnit_Framework_ExpectationFailedException
- */
- public function fail($other, $description, $not = FALSE)
- {
- if (empty($description)) {
- throw new PHPUnit_Framework_ExpectationFailedException(
- sprintf(
- '%sPCRE pattern "%s" did not find a match in %s',
-
- $description,
- $this->pattern,
- PHPUnit_Util_Type::toString($other)
- )
- );
- } else {
- throw new PHPUnit_Framework_ExpectationFailedException(
- $description
- );
- }
- }
-
- /**
* Returns a string representation of the constraint.
*
* @return string
@@ -126,7 +99,7 @@ public function fail($other, $description, $not = FALSE)
public function toString()
{
return sprintf(
- 'string matches PCRE pattern "%s"',
+ 'matches PCRE pattern "%s"',
$this->pattern
);
View
43 PHPUnit/Framework/Constraint/StringContains.php
@@ -69,7 +69,7 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_StringContains implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_StringContains extends PHPUnit_Framework_Constraint
{
private $string;
@@ -85,7 +85,7 @@ public function __construct($string, $case = TRUE)
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
@@ -98,39 +98,6 @@ public function evaluate($other)
}
/**
- * @param mixed $other The value passed to evaluate() which failed the
- * constraint check.
- * @param string $description A string with extra description of what was
- * going on while the evaluation failed.
- * @param boolean $not Flag to indicate negation.
- * @throws PHPUnit_Framework_ExpectationFailedException
- */
- public function fail($other, $description, $not = FALSE)
- {
- if ($this->case) {
- $string = $this->string;
- $otherString = $other;
- } else {
- $string = strtolower($this->string);
- $otherString = strtolower($other);
- }
-
- if (!empty($description)) {
- $description .= "\n";
- }
-
- throw new PHPUnit_Framework_ExpectationFailedException(
- sprintf(
- '%sExpected %s not found in %s.',
-
- $description,
- PHPUnit_Util_Type::toString($string),
- PHPUnit_Util_Type::toString($otherString)
- )
- );
- }
-
- /**
* Returns a string representation of the constraint.
*
* @return string
@@ -144,11 +111,7 @@ public function toString()
$string = strtolower($this->string);
}
- return sprintf(
- 'contains %s',
-
- PHPUnit_Util_Type::toString($string)
- );
+ return 'contains ' . PHPUnit_Util_Type::toString($string);
}
}
?>
View
34 PHPUnit/Framework/Constraint/TraversableContains.php
@@ -46,6 +46,7 @@
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/Util/Filter.php';
+require_once 'PHPUnit/Util/Type.php';
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
@@ -61,7 +62,7 @@
* @link http://www.phpunit.de/
* @since Class available since Release 3.0.0
*/
-class PHPUnit_Framework_Constraint_TraversableContains implements PHPUnit_Framework_Constraint
+class PHPUnit_Framework_Constraint_TraversableContains extends PHPUnit_Framework_Constraint
{
private $value;
@@ -74,7 +75,7 @@ public function __construct($value)
* Evaluates the constraint for parameter $other. Returns TRUE if the
* constraint is met, FALSE otherwise.
*
- * @parameter mixed $other Value or object to evaluate.
+ * @param mixed $other Value or object to evaluate.
* @return bool
*/
public function evaluate($other)
@@ -92,28 +93,6 @@ public function evaluate($other)
}
/**
- * @param mixed $other The value passed to evaluate() which failed the
- * constraint check.
- * @param string $description A string with extra description of what was
- * going on while the evaluation failed.
- * @param boolean $not Flag to indicate negation.
- * @throws PHPUnit_Framework_ExpectationFailedException
- */
- public function fail($other, $description, $not = FALSE)
- {
- throw new PHPUnit_Framework_ExpectationFailedException(
- sprintf(
- "%s\nexpected value <%s:%s> not found in object <%s>",
-
- $description,
- gettype($this->value),
- print_r($this->value, TRUE),
- print_r($other, TRUE)
- )
- );
- }
-
- /**
* Returns a string representation of the constraint.
*
* @return string
@@ -121,12 +100,7 @@ public function fail($other, $description, $not = FALSE)
*/
public function toString()
{
- return sprintf(
- 'contains value <%s:%s>',
-
- gettype($this->value),
- print_r($this->value, TRUE)
- );
+ return 'contains ' . PHPUnit_Util_Type::toString($this->value);
}
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.