Skip to content

Commit fe4c50f

Browse files
More work on PHPUnit_Framework_Constraint_*.
1 parent f0bf955 commit fe4c50f

18 files changed

Lines changed: 186 additions & 422 deletions

PHPUnit/Framework/Assert.php

Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static function assertArrayHasKey($key, array $array, $message = '')
9595
$constraint = new PHPUnit_Framework_Constraint_ArrayHasKey($key);
9696

9797
if (!$constraint->evaluate($array)) {
98-
self::failConstraint($constraint, $array, $message);
98+
$constraint->fail($array, $message);
9999
}
100100
}
101101

@@ -120,7 +120,7 @@ public static function assertArrayNotHasKey($key, array $array, $message = '')
120120
);
121121

122122
if (!$constraint->evaluate($array)) {
123-
self::failConstraint($constraint, $array, $message);
123+
$constraint->fail($array, $message);
124124
}
125125
}
126126

@@ -150,7 +150,7 @@ public static function assertContains($needle, $haystack, $message = '')
150150
}
151151

152152
if (!$constraint->evaluate($haystack)) {
153-
self::failConstraint($constraint, $haystack, $message);
153+
$constraint->fail($haystack, $message);
154154
}
155155
}
156156

@@ -209,7 +209,7 @@ public static function assertNotContains($needle, $haystack, $message = '')
209209
}
210210

211211
if (!$constraint->evaluate($haystack)) {
212-
self::failConstraint($constraint, $haystack, $message);
212+
$constraint->fail($haystack, $message);
213213
}
214214
}
215215

@@ -258,7 +258,7 @@ public static function assertEquals($expected, $actual, $message = '', $delta =
258258
);
259259

260260
if (!$constraint->evaluate($actual)) {
261-
self::failConstraint($constraint, $actual, $message);
261+
$constraint->fail($actual, $message);
262262
}
263263
}
264264

@@ -312,7 +312,7 @@ public static function assertNotEquals($expected, $actual, $message = '', $delta
312312
);
313313

314314
if (!$constraint->evaluate($actual)) {
315-
self::failConstraint($constraint, $actual, $message);
315+
$constraint->fail($actual, $message);
316316
}
317317
}
318318

@@ -361,7 +361,7 @@ public static function assertFileExists($filename, $message = '')
361361
$constraint = new PHPUnit_Framework_Constraint_FileExists;
362362

363363
if (!$constraint->evaluate($filename)) {
364-
self::failConstraint($constraint, $filename, $message);
364+
$constraint->fail($filename, $message);
365365
}
366366
}
367367

@@ -385,7 +385,7 @@ public static function assertFileNotExists($filename, $message = '')
385385
);
386386

387387
if (!$constraint->evaluate($filename)) {
388-
self::failConstraint($constraint, $filename, $message);
388+
$constraint->fail($filename, $message);
389389
}
390390
}
391391

@@ -408,7 +408,7 @@ public static function assertTrue($condition, $message = '')
408408
$constraint = new PHPUnit_Framework_Constraint_IsIdentical(TRUE);
409409

410410
if (!$constraint->evaluate($condition)) {
411-
self::failConstraint($constraint, $condition, $message);
411+
$constraint->fail($condition, $message);
412412
}
413413
}
414414

@@ -431,7 +431,7 @@ public static function assertFalse($condition, $message = '')
431431
$constraint = new PHPUnit_Framework_Constraint_IsIdentical(FALSE);
432432

433433
if (!$constraint->evaluate($condition)) {
434-
self::failConstraint($constraint, $condition, $message);
434+
$constraint->fail($condition, $message);
435435
}
436436
}
437437

@@ -450,7 +450,7 @@ public static function assertNotNull($actual, $message = '')
450450
);
451451

452452
if (!$constraint->evaluate($actual)) {
453-
self::failConstraint($constraint, $actual, $message);
453+
$constraint->fail($actual, $message);
454454
}
455455
}
456456

@@ -467,7 +467,7 @@ public static function assertNull($actual, $message = '')
467467
$constraint = new PHPUnit_Framework_Constraint_IsIdentical(NULL);
468468

469469
if (!$constraint->evaluate($actual)) {
470-
self::failConstraint($constraint, $actual, $message);
470+
$constraint->fail($actual, $message);
471471
}
472472
}
473473

@@ -490,7 +490,7 @@ public static function assertObjectHasAttribute($attributeName, $object, $messag
490490
$constraint = new PHPUnit_Framework_Constraint_ObjectHasAttribute($attributeName);
491491

492492
if (!$constraint->evaluate($object)) {
493-
self::failConstraint($constraint, $object, $message);
493+
$constraint->fail($object, $message);
494494
}
495495
}
496496

@@ -515,7 +515,7 @@ public static function assertObjectNotHasAttribute($attributeName, $object, $mes
515515
);
516516

517517
if (!$constraint->evaluate($object)) {
518-
self::failConstraint($constraint, $object, $message);
518+
$constraint->fail($object, $message);
519519
}
520520
}
521521

@@ -535,7 +535,7 @@ public static function assertSame($expected, $actual, $message = '')
535535
$constraint = new PHPUnit_Framework_Constraint_IsIdentical($expected);
536536

537537
if (!$constraint->evaluate($actual)) {
538-
self::failConstraint($constraint, $actual, $message);
538+
$constraint->fail($actual, $message);
539539
}
540540
}
541541

@@ -581,7 +581,7 @@ public static function assertNotSame($expected, $actual, $message = '')
581581
);
582582

583583
if (!$constraint->evaluate($actual)) {
584-
self::failConstraint($constraint, $actual, $message);
584+
$constraint->fail($actual, $message);
585585
}
586586
}
587587

@@ -623,9 +623,8 @@ public static function assertType($expected, $actual, $message = '')
623623
if (is_string($expected)) {
624624
if (class_exists($expected, FALSE) ||
625625
interface_exists($expected, FALSE)) {
626-
$constraint = self::logicalAnd(
627-
new PHPUnit_Framework_Constraint_IsType('object'),
628-
new PHPUnit_Framework_Constraint_IsInstanceOf($expected)
626+
$constraint = new PHPUnit_Framework_Constraint_IsInstanceOf(
627+
$expected
629628
);
630629
} else {
631630
$constraint = new PHPUnit_Framework_Constraint_IsType($expected);
@@ -635,7 +634,7 @@ interface_exists($expected, FALSE)) {
635634
}
636635

637636
if (!$constraint->evaluate($actual)) {
638-
self::failConstraint($constraint, $actual, $message);
637+
$constraint->fail($actual, $message);
639638
}
640639
}
641640

@@ -655,10 +654,7 @@ public static function assertNotType($expected, $actual, $message = '')
655654
if (class_exists($expected, FALSE) ||
656655
interface_exists($expected, FALSE)) {
657656
$constraint = new PHPUnit_Framework_Constraint_Not(
658-
self::logicalAnd(
659-
new PHPUnit_Framework_Constraint_IsType('object'),
660-
new PHPUnit_Framework_Constraint_IsInstanceOf($expected)
661-
)
657+
new PHPUnit_Framework_Constraint_IsInstanceOf($expected)
662658
);
663659
} else {
664660
$constraint = new PHPUnit_Framework_Constraint_Not(
@@ -670,7 +666,7 @@ interface_exists($expected, FALSE)) {
670666
}
671667

672668
if (!$constraint->evaluate($actual)) {
673-
self::failConstraint($constraint, $actual, $message);
669+
$constraint->fail($actual, $message);
674670
}
675671
}
676672

@@ -692,7 +688,7 @@ public static function assertRegExp($pattern, $string, $message = '')
692688
$constraint = new PHPUnit_Framework_Constraint_PCREMatch($pattern);
693689

694690
if (!$constraint->evaluate($string)) {
695-
self::failConstraint($constraint, $string, $message);
691+
$constraint->fail($string, $message);
696692
}
697693
}
698694

@@ -717,7 +713,7 @@ public static function assertNotRegExp($pattern, $string, $message = '')
717713
);
718714

719715
if (!$constraint->evaluate($string)) {
720-
self::failConstraint($constraint, $string, $message);
716+
$constraint->fail($string, $message);
721717
}
722718
}
723719

@@ -734,7 +730,7 @@ public static function assertNotRegExp($pattern, $string, $message = '')
734730
public static function assertThat($value, PHPUnit_Framework_Constraint $constraint, $message = '')
735731
{
736732
if (!$constraint->evaluate($value)) {
737-
self::failConstraint($constraint, $value, $message);
733+
$constraint->fail($value, $message);
738734
}
739735
}
740736

@@ -985,35 +981,6 @@ public static function fail($message = '')
985981
throw new PHPUnit_Framework_AssertionFailedError($message);
986982
}
987983

988-
/**
989-
* Fails a test based on a failed constraint.
990-
*
991-
* @param PHPUnit_Framework_Constraint $constraint
992-
* @param mixed $value
993-
* @param string $message
994-
* @throws PHPUnit_Framework_ExpectationFailedException
995-
* @access public
996-
* @static
997-
* @since Method available since Release 3.0.0
998-
*/
999-
public static function failConstraint(PHPUnit_Framework_Constraint $constraint, $value, $message)
1000-
{
1001-
if (!empty($message)) {
1002-
$message .= "\n";
1003-
}
1004-
1005-
$constraint->fail(
1006-
$value,
1007-
sprintf(
1008-
'%sFailed asserting that %s %s.',
1009-
1010-
$message,
1011-
PHPUnit_Util_Type::toString($value),
1012-
$constraint->toString()
1013-
)
1014-
);
1015-
}
1016-
1017984
/**
1018985
* Returns the value of an object's attribute.
1019986
* This also works for attributes that are declared protected or private.

PHPUnit/Framework/Constraint.php

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,7 @@
5353
if (!interface_exists('PHPUnit_Framework_Constraint')) {
5454

5555
/**
56-
* Interface for constraints which are placed upon any value.
57-
*
58-
* The constraint can be used in method name matching and parameter value matching
59-
* to perform more advanced checking than simply matching two values with ==.
60-
*
61-
* A constraint must provides the following methods:
62-
* - evaluate() check if a given object meets the constraint. If it does
63-
* not, fail() can be called to create an exception.
64-
* - toStrint() returns a description of the constraint.
56+
* Abstract base class for constraints. which are placed upon any value.
6557
*
6658
* @category Testing
6759
* @package PHPUnit
@@ -73,16 +65,17 @@
7365
* @link http://www.phpunit.de/
7466
* @since Interface available since Release 3.0.0
7567
*/
76-
interface PHPUnit_Framework_Constraint extends PHPUnit_Framework_SelfDescribing
68+
abstract class PHPUnit_Framework_Constraint implements PHPUnit_Framework_SelfDescribing
7769
{
7870
/**
7971
* Evaluates the constraint for parameter $other. Returns TRUE if the
8072
* constraint is met, FALSE otherwise.
8173
*
82-
* @parameter mixed $other Value or object to evaluate.
74+
* @param mixed $other Value or object to evaluate.
8375
* @return bool
76+
* @abstract
8477
*/
85-
public function evaluate($other);
78+
abstract public function evaluate($other);
8679

8780
/**
8881
* Creates the appropriate exception for the constraint which can be caught
@@ -95,7 +88,46 @@ public function evaluate($other);
9588
* @param boolean $not Flag to indicate negation.
9689
* @throws PHPUnit_Framework_ExpectationFailedException
9790
*/
98-
public function fail($other, $description, $not = FALSE);
91+
public function fail($other, $description, $not = FALSE)
92+
{
93+
if (!empty($description)) {
94+
$description .= "\n";
95+
}
96+
97+
$failureDescription = sprintf(
98+
'%sFailed asserting that %s %s.',
99+
100+
$description,
101+
PHPUnit_Util_Type::toString($other),
102+
$this->toString()
103+
);
104+
105+
if ($not) {
106+
$failureDescription = str_replace(
107+
array(
108+
' anything ',
109+
' contains ',
110+
' exists ',
111+
' has ',
112+
' is ',
113+
' matches '
114+
),
115+
array(
116+
' nothing ',
117+
' not contains ',
118+
' not exists ',
119+
' has not ',
120+
' is not ',
121+
' not matches '
122+
),
123+
$failureDescription
124+
);
125+
}
126+
127+
throw new PHPUnit_Framework_ExpectationFailedException(
128+
$failureDescription
129+
);
130+
}
99131
}
100132

101133
}

0 commit comments

Comments
 (0)