Permalink
Browse files

More work on PHPUnit_Framework_Constraint_*.

  • Loading branch information...
1 parent 1ce7fe4 commit e4ddabeb7b4f357a8cb5b809d74f5059abbfa251 @sebastianbergmann committed Nov 10, 2006
@@ -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);
}
}
@@ -985,35 +981,6 @@ public static function fail($message = '')
throw new PHPUnit_Framework_AssertionFailedError($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.
@@ -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
+ );
+ }
}
}
Oops, something went wrong.

0 comments on commit e4ddabe

Please sign in to comment.