Skip to content

Commit

Permalink
More PHPUnit_Framework_Constraint* refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Nov 10, 2006
1 parent 12b557b commit e243abc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 42 deletions.
28 changes: 16 additions & 12 deletions PHPUnit/Framework/Constraint.php
Expand Up @@ -90,14 +90,16 @@ abstract public function evaluate($other);
*/
public function fail($other, $description, $not = FALSE)
{
if (!empty($description)) {
$description .= "\n";
}
throw new PHPUnit_Framework_ExpectationFailedException(
$this->failureDescription($other, $description, $not)
);
}

protected function failureDescription($other, $description, $not)
{
$failureDescription = sprintf(
'%sFailed asserting that %s %s.',
'Failed asserting that %s %s.',

$description,
PHPUnit_Util_Type::toString($other),
$this->toString()
);
Expand All @@ -106,9 +108,11 @@ public function fail($other, $description, $not = FALSE)
$failureDescription = self::negate($failureDescription);
}

throw new PHPUnit_Framework_ExpectationFailedException(
$failureDescription
);
if (!empty($description)) {
$failureDescription = $description . "\n" . $failureDescription;
}

return $failureDescription;
}

public static function negate($string)
Expand All @@ -122,11 +126,11 @@ public static function negate($string)
'matches '
),
array(
'not contains ',
'not exists ',
'has not ',
'does not contain ',
'does not exist ',
'does not have ',
'is not ',
'not matches '
'does not match '
),
$string
);
Expand Down
19 changes: 12 additions & 7 deletions PHPUnit/Framework/Constraint/FileExists.php
Expand Up @@ -89,17 +89,22 @@ public function evaluate($other)
*/
public function fail($other, $description, $not = FALSE)
{
$failureDescription = sprintf(
'Failed asserting that file "%s" exists.',

$other
);

if ($not) {
$failureDescription = self::negate($failureDescription);
}

if (!empty($description)) {
$description .= "\n";
$failureDescription = $description . "\n" . $failureDescription;
}

throw new PHPUnit_Framework_ExpectationFailedException(
sprintf(
'%sFailed asserting that file "%s" exists.',

$description,
$other
)
$failureDescription
);
}

Expand Down
16 changes: 4 additions & 12 deletions PHPUnit/Framework/Constraint/IsEqual.php
Expand Up @@ -106,16 +106,10 @@ public function evaluate($other)
*/
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()
$failureDescription = $this->failureDescription(
$other,
$description,
$not
);

if (!$not) {
Expand All @@ -124,8 +118,6 @@ public function fail($other, $description, $not = FALSE)
PHPUnit_Framework_ComparisonFailure::diffEqual($this->value, $other)
);
} else {
$failureDescription = self::negate($failureDescription);

throw new PHPUnit_Framework_ExpectationFailedException(
$failureDescription
);
Expand Down
16 changes: 5 additions & 11 deletions PHPUnit/Framework/Constraint/IsIdentical.php
Expand Up @@ -101,16 +101,10 @@ public function evaluate($other)
*/
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()
$failureDescription = $this->failureDescription(
$other,
$description,
$not
);

if (!$not) {
Expand All @@ -120,7 +114,7 @@ public function fail($other, $description, $not = FALSE)
);
} else {
throw new PHPUnit_Framework_ExpectationFailedException(
$failureDescription
self::negate($failureDescription)
);
}
}
Expand Down

0 comments on commit e243abc

Please sign in to comment.