Skip to content

Commit 89b9c5d

Browse files
committed
Improved support for asserting on traversable objects.
* Assertions using `PHPUnit_Framework_Constraint_Count` now support traversable objects. * Assertions using `PHPUnit_Framework_Constraint_TraversableContains` now give messages that mention the `Traversable` interface.
1 parent a192c68 commit 89b9c5d

File tree

6 files changed

+52
-51
lines changed

6 files changed

+52
-51
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ PHPUnit 3.8.0
2222
* Implemented #871: Add Comparator for DateTime objects.
2323
* Implemented #877: Added new HTML5 tags to `PHPUnit_Util_XML::findNodes()`.
2424
* Added `--coverage-crap4j` switch to generate code coverage report in Crap4J XML format.
25+
* `assertCount()`, `assertNotCount()`, `assertSameSize()`, and `assertNotSameSize()` now support all objects that implement the `Traversable` interface.
2526
* A test will now fail in strict mode when it uses the `@covers` annotation and code that is not expected to be covered is executed.
2627
* All relative paths in a configuration file are now resolved relative to that configuration file. When upgrading, you may need to update relative paths for the following configurations: `testSuiteLoaderFile`, `printerFile`, `testsuites/file`, `testsuites/exclude`.
2728
* Fixed #240: XML strings are escaped by removing invalid characters.

PHPUnit/Framework/Assert.php

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static function assertContains($needle, $haystack, $message = '', $ignore
143143

144144
else {
145145
throw PHPUnit_Util_InvalidArgumentHelper::factory(
146-
2, 'array, iterator or string'
146+
2, 'array, traversable or string'
147147
);
148148
}
149149

@@ -207,7 +207,7 @@ public static function assertNotContains($needle, $haystack, $message = '', $ign
207207

208208
else {
209209
throw PHPUnit_Util_InvalidArgumentHelper::factory(
210-
2, 'array, iterator or string'
210+
2, 'array, traversable or string'
211211
);
212212
}
213213

@@ -253,7 +253,7 @@ public static function assertContainsOnly($type, $haystack, $isNativeType = NULL
253253
if (!(is_array($haystack) ||
254254
is_object($haystack) && $haystack instanceof Traversable)) {
255255
throw PHPUnit_Util_InvalidArgumentHelper::factory(
256-
2, 'array or iterator'
256+
2, 'array or traversable'
257257
);
258258
}
259259

@@ -282,7 +282,7 @@ public static function assertContainsOnlyInstancesOf($classname, $haystack, $mes
282282
if (!(is_array($haystack) ||
283283
is_object($haystack) && $haystack instanceof Traversable)) {
284284
throw PHPUnit_Util_InvalidArgumentHelper::factory(
285-
2, 'array or iterator'
285+
2, 'array or traversable'
286286
);
287287
}
288288

@@ -330,7 +330,7 @@ public static function assertNotContainsOnly($type, $haystack, $isNativeType = N
330330
if (!(is_array($haystack) ||
331331
is_object($haystack) && $haystack instanceof Traversable)) {
332332
throw PHPUnit_Util_InvalidArgumentHelper::factory(
333-
2, 'array or iterator'
333+
2, 'array or traversable'
334334
);
335335
}
336336

@@ -372,7 +372,7 @@ public static function assertAttributeNotContainsOnly($type, $haystackAttributeN
372372
}
373373

374374
/**
375-
* Asserts the number of elements of an array, Countable or Iterator.
375+
* Asserts the number of elements of an array, Countable or Traversable.
376376
*
377377
* @param integer $expectedCount
378378
* @param mixed $haystack
@@ -385,9 +385,9 @@ public static function assertCount($expectedCount, $haystack, $message = '')
385385
}
386386

387387
if (!$haystack instanceof Countable &&
388-
!$haystack instanceof Iterator &&
388+
!$haystack instanceof Traversable &&
389389
!is_array($haystack)) {
390-
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable');
390+
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable');
391391
}
392392

393393
self::assertThat(
@@ -398,7 +398,7 @@ public static function assertCount($expectedCount, $haystack, $message = '')
398398
}
399399

400400
/**
401-
* Asserts the number of elements of an array, Countable or Iterator
401+
* Asserts the number of elements of an array, Countable or Traversable
402402
* that is stored in an attribute.
403403
*
404404
* @param integer $expectedCount
@@ -417,7 +417,7 @@ public static function assertAttributeCount($expectedCount, $haystackAttributeNa
417417
}
418418

419419
/**
420-
* Asserts the number of elements of an array, Countable or Iterator.
420+
* Asserts the number of elements of an array, Countable or Traversable.
421421
*
422422
* @param integer $expectedCount
423423
* @param mixed $haystack
@@ -430,9 +430,9 @@ public static function assertNotCount($expectedCount, $haystack, $message = '')
430430
}
431431

432432
if (!$haystack instanceof Countable &&
433-
!$haystack instanceof Iterator &&
433+
!$haystack instanceof Traversable &&
434434
!is_array($haystack)) {
435-
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable');
435+
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable');
436436
}
437437

438438
$constraint = new PHPUnit_Framework_Constraint_Not(
@@ -443,7 +443,7 @@ public static function assertNotCount($expectedCount, $haystack, $message = '')
443443
}
444444

445445
/**
446-
* Asserts the number of elements of an array, Countable or Iterator
446+
* Asserts the number of elements of an array, Countable or Traversable
447447
* that is stored in an attribute.
448448
*
449449
* @param integer $expectedCount
@@ -941,7 +941,7 @@ public static function assertClassHasAttribute($attributeName, $className, $mess
941941
if (!is_string($attributeName)) {
942942
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
943943
}
944-
944+
945945
if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
946946
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
947947
}
@@ -970,7 +970,7 @@ public static function assertClassNotHasAttribute($attributeName, $className, $m
970970
if (!is_string($attributeName)) {
971971
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
972972
}
973-
973+
974974
if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
975975
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
976976
}
@@ -999,7 +999,7 @@ public static function assertClassHasStaticAttribute($attributeName, $className,
999999
if (!is_string($attributeName)) {
10001000
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
10011001
}
1002-
1002+
10031003
if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
10041004
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
10051005
}
@@ -1028,7 +1028,7 @@ public static function assertClassNotHasStaticAttribute($attributeName, $classNa
10281028
if (!is_string($attributeName)) {
10291029
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
10301030
}
1031-
1031+
10321032
if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
10331033
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
10341034
}
@@ -1059,7 +1059,7 @@ public static function assertObjectHasAttribute($attributeName, $object, $messag
10591059
if (!is_string($attributeName)) {
10601060
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
10611061
}
1062-
1062+
10631063
if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
10641064
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
10651065
}
@@ -1088,7 +1088,7 @@ public static function assertObjectNotHasAttribute($attributeName, $object, $mes
10881088
if (!is_string($attributeName)) {
10891089
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
10901090
}
1091-
1091+
10921092
if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
10931093
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'valid attribute name');
10941094
}
@@ -1404,25 +1404,25 @@ public static function assertNotRegExp($pattern, $string, $message = '')
14041404
}
14051405

14061406
/**
1407-
* Assert that the size of two arrays (or `Countable` or `Iterator` objects)
1407+
* Assert that the size of two arrays (or `Countable` or `Traversable` objects)
14081408
* is the same.
14091409
*
1410-
* @param array|Countable|Iterator $expected
1411-
* @param array|Countable|Iterator $actual
1410+
* @param array|Countable|Traversable $expected
1411+
* @param array|Countable|Traversable $actual
14121412
* @param string $message
14131413
*/
14141414
public static function assertSameSize($expected, $actual, $message = '')
14151415
{
14161416
if (!$expected instanceof Countable &&
1417-
!$expected instanceof Iterator &&
1417+
!$expected instanceof Traversable &&
14181418
!is_array($expected)) {
1419-
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'countable');
1419+
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'countable or traversable');
14201420
}
14211421

14221422
if (!$actual instanceof Countable &&
1423-
!$actual instanceof Iterator &&
1423+
!$actual instanceof Traversable &&
14241424
!is_array($actual)) {
1425-
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable');
1425+
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable');
14261426
}
14271427

14281428
self::assertThat(
@@ -1433,25 +1433,25 @@ public static function assertSameSize($expected, $actual, $message = '')
14331433
}
14341434

14351435
/**
1436-
* Assert that the size of two arrays (or `Countable` or `Iterator` objects)
1436+
* Assert that the size of two arrays (or `Countable` or `Traversable` objects)
14371437
* is not the same.
14381438
*
1439-
* @param array|Countable|Iterator $expected
1440-
* @param array|Countable|Iterator $actual
1439+
* @param array|Countable|Traversable $expected
1440+
* @param array|Countable|Traversable $actual
14411441
* @param string $message
14421442
*/
14431443
public static function assertNotSameSize($expected, $actual, $message = '')
14441444
{
14451445
if (!$expected instanceof Countable &&
1446-
!$expected instanceof Iterator &&
1446+
!$expected instanceof Traversable &&
14471447
!is_array($expected)) {
1448-
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'countable');
1448+
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'countable or traversable');
14491449
}
14501450

14511451
if (!$actual instanceof Countable &&
1452-
!$actual instanceof Iterator &&
1452+
!$actual instanceof Traversable &&
14531453
!is_array($actual)) {
1454-
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable');
1454+
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable or traversable');
14551455
}
14561456

14571457
$constraint = new PHPUnit_Framework_Constraint_Not(
@@ -2810,7 +2810,7 @@ public static function readAttribute($classOrObject, $attributeName)
28102810
if (!is_string($attributeName)) {
28112811
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
28122812
}
2813-
2813+
28142814
if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
28152815
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'valid attribute name');
28162816
}
@@ -2910,7 +2910,7 @@ protected static function getStaticAttribute($className, $attributeName)
29102910
if (!is_string($attributeName)) {
29112911
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
29122912
}
2913-
2913+
29142914
if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
29152915
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'valid attribute name');
29162916
}
@@ -2955,7 +2955,7 @@ protected static function getObjectAttribute($object, $attributeName)
29552955
if (!is_string($attributeName)) {
29562956
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'string');
29572957
}
2958-
2958+
29592959
if(!preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName)) {
29602960
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'valid attribute name');
29612961
}

PHPUnit/Framework/Assert/Functions.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function assertAttributeContainsOnly($type, $haystackAttributeName, $haystackCla
160160
}
161161

162162
/**
163-
* Asserts the number of elements of an array, Countable or Iterator
163+
* Asserts the number of elements of an array, Countable or Traversable
164164
* that is stored in an attribute.
165165
*
166166
* @param integer $expectedCount
@@ -358,7 +358,7 @@ function assertAttributeNotContainsOnly($type, $haystackAttributeName, $haystack
358358
}
359359

360360
/**
361-
* Asserts the number of elements of an array, Countable or Iterator
361+
* Asserts the number of elements of an array, Countable or Traversable
362362
* that is stored in an attribute.
363363
*
364364
* @param integer $expectedCount
@@ -596,7 +596,7 @@ function assertContainsOnlyInstancesOf($classname, $haystack, $message = '')
596596
}
597597

598598
/**
599-
* Asserts the number of elements of an array, Countable or Iterator.
599+
* Asserts the number of elements of an array, Countable or Traversable.
600600
*
601601
* @param integer $expectedCount
602602
* @param mixed $haystack
@@ -983,7 +983,7 @@ function assertNotContainsOnly($type, $haystack, $isNativeType = NULL, $message
983983
}
984984

985985
/**
986-
* Asserts the number of elements of an array, Countable or Iterator.
986+
* Asserts the number of elements of an array, Countable or Traversable.
987987
*
988988
* @param integer $expectedCount
989989
* @param mixed $haystack
@@ -1112,11 +1112,11 @@ function assertNotSame($expected, $actual, $message = '')
11121112
}
11131113

11141114
/**
1115-
* Assert that the size of two arrays (or `Countable` or `Iterator` objects)
1115+
* Assert that the size of two arrays (or `Countable` or `Traversable` objects)
11161116
* is not the same.
11171117
*
1118-
* @param array|Countable|Iterator $expected
1119-
* @param array|Countable|Iterator $actual
1118+
* @param array|Countable|Traversable $expected
1119+
* @param array|Countable|Traversable $actual
11201120
* @param string $message
11211121
*/
11221122
function assertNotSameSize($expected, $actual, $message = '')
@@ -1228,11 +1228,11 @@ function assertSame($expected, $actual, $message = '')
12281228
}
12291229

12301230
/**
1231-
* Assert that the size of two arrays (or `Countable` or `Iterator` objects)
1231+
* Assert that the size of two arrays (or `Countable` or `Traversable` objects)
12321232
* is the same.
12331233
*
1234-
* @param array|Countable|Iterator $expected
1235-
* @param array|Countable|Iterator $actual
1234+
* @param array|Countable|Traversable $expected
1235+
* @param array|Countable|Traversable $actual
12361236
* @param string $message
12371237
*/
12381238
function assertSameSize($expected, $actual, $message = '')

PHPUnit/Framework/Constraint/Count.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected function getCountOf($other)
9494
return count($other);
9595
}
9696

97-
else if ($other instanceof Iterator) {
97+
else if ($other instanceof Traversable) {
9898
return iterator_count($other);
9999
}
100100
}

PHPUnit/Framework/Constraint/TraversableContains.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ public function toString()
159159
protected function failureDescription($other)
160160
{
161161
return sprintf(
162-
'an %s %s',
162+
'%s %s',
163163

164-
is_array($other) ? 'array' : 'iterator',
164+
is_array($other) ? 'an array' : 'a traversable',
165165
$this->toString()
166166
);
167167
}

Tests/Framework/ConstraintTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3196,7 +3196,7 @@ public function testConstraintSplObjectStorageContains()
31963196
catch (PHPUnit_Framework_ExpectationFailedException $e) {
31973197
$this->assertStringMatchesFormat(
31983198
<<<EOF
3199-
Failed asserting that an iterator contains stdClass Object &%x ().
3199+
Failed asserting that a traversable contains stdClass Object &%x ().
32003200
32013201
EOF
32023202
,
@@ -3226,7 +3226,7 @@ public function testConstraintSplObjectStorageContains2()
32263226
$this->assertStringMatchesFormat(
32273227
<<<EOF
32283228
custom message
3229-
Failed asserting that an iterator contains stdClass Object &%x ().
3229+
Failed asserting that a traversable contains stdClass Object &%x ().
32303230
32313231
EOF
32323232
,

0 commit comments

Comments
 (0)