Skip to content

Commit 99a61a1

Browse files
Initial work on assertSameSize() and assertNotSameSize().
1 parent b235468 commit 99a61a1

File tree

9 files changed

+165
-4
lines changed

9 files changed

+165
-4
lines changed

ChangeLog.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ This is the list of changes for the PHPUnit 3.6 release series.
66
PHPUnit 3.6.0
77
-------------
88

9-
* Added `assertCount()` and `assertNotCount()` to assert the number of elements in an array as well as `Countable` or `Iterator` objects.
9+
* Added `assertCount()` and `assertNotCount()` to assert the number of elements in an array (or `Countable` or `Iterator` objects).
10+
* Added `assertSameSize()` and `assertNotSameSize()` to assert that the size of two arrays (or `Countable` or `Iterator` objects) is the same.
1011
* Added `returnSelf()` to ease the mocking and stubbing of fluent interfaces.
1112
* Added an option to disable the check for object identity in `assertContains()` and related methods.
1213
* Implemented GH-63: Invalid `@covers` annotations should produce a test error instead of aborting PHPUnit.

PHPUnit/Autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ function phpunit_autoload($class = NULL) {
100100
'phpunit_framework_constraint_objecthasattribute' => '/Framework/Constraint/ObjectHasAttribute.php',
101101
'phpunit_framework_constraint_or' => '/Framework/Constraint/Or.php',
102102
'phpunit_framework_constraint_pcrematch' => '/Framework/Constraint/PCREMatch.php',
103+
'phpunit_framework_constraint_samesize' => '/Framework/Constraint/SameSize.php',
103104
'phpunit_framework_constraint_stringcontains' => '/Framework/Constraint/StringContains.php',
104105
'phpunit_framework_constraint_stringendswith' => '/Framework/Constraint/StringEndsWith.php',
105106
'phpunit_framework_constraint_stringmatches' => '/Framework/Constraint/StringMatches.php',

PHPUnit/Framework/Assert.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,64 @@ public static function assertNotRegExp($pattern, $string, $message = '')
13011301
self::assertThat($string, $constraint, $message);
13021302
}
13031303

1304+
/**
1305+
* Assert that the size of two arrays (or `Countable` or `Iterator` objects)
1306+
* is the same.
1307+
*
1308+
* @param integer $expected
1309+
* @param mixed $actual
1310+
* @param string $message
1311+
*/
1312+
public function assertSameSize($expected, $actual, $message = '')
1313+
{
1314+
if (!$expected instanceof Countable &&
1315+
!$expected instanceof Iterator &&
1316+
!is_array($expected)) {
1317+
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'countable');
1318+
}
1319+
1320+
if (!$actual instanceof Countable &&
1321+
!$actual instanceof Iterator &&
1322+
!is_array($actual)) {
1323+
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable');
1324+
}
1325+
1326+
self::assertThat(
1327+
$actual,
1328+
new PHPUnit_Framework_Constraint_SameSize($expected),
1329+
$message
1330+
);
1331+
}
1332+
1333+
/**
1334+
* Assert that the size of two arrays (or `Countable` or `Iterator` objects)
1335+
* is not the same.
1336+
*
1337+
* @param integer $expected
1338+
* @param mixed $actual
1339+
* @param string $message
1340+
*/
1341+
public function assertNotSameSize($expectedCount, $haystack, $message = '')
1342+
{
1343+
if (!$expected instanceof Countable &&
1344+
!$expected instanceof Iterator &&
1345+
!is_array($expected)) {
1346+
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'countable');
1347+
}
1348+
1349+
if (!$actual instanceof Countable &&
1350+
!$actual instanceof Iterator &&
1351+
!is_array($actual)) {
1352+
throw PHPUnit_Util_InvalidArgumentHelper::factory(2, 'countable');
1353+
}
1354+
1355+
$constraint = new PHPUnit_Framework_Constraint_Not(
1356+
new PHPUnit_Framework_Constraint_SameSize($expected)
1357+
);
1358+
1359+
self::assertThat($actual, $constraint, $message);
1360+
}
1361+
13041362
/**
13051363
* Asserts that a string matches a given format string.
13061364
*

PHPUnit/Framework/Assert/Functions.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,19 @@ function assertNotSame($expected, $actual, $message = '')
789789
return PHPUnit_Framework_Assert::assertNotSame($expected, $actual, $message);
790790
}
791791

792+
/**
793+
* Assert that the size of two arrays (or `Countable` or `Iterator` objects)
794+
* is not the same.
795+
*
796+
* @param integer $expected
797+
* @param mixed $actual
798+
* @param string $message
799+
*/
800+
function assertNotSameSize($expectedCount, $haystack, $message = '')
801+
{
802+
return PHPUnit_Framework_Assert::assertNotSameSize($expectedCount, $haystack, $message);
803+
}
804+
792805
/**
793806
* This assertion is the exact opposite of assertTag().
794807
*
@@ -871,6 +884,19 @@ function assertSame($expected, $actual, $message = '')
871884
return PHPUnit_Framework_Assert::assertSame($expected, $actual, $message);
872885
}
873886

887+
/**
888+
* Assert that the size of two arrays (or `Countable` or `Iterator` objects)
889+
* is the same.
890+
*
891+
* @param integer $expected
892+
* @param mixed $actual
893+
* @param string $message
894+
*/
895+
function assertSameSize($expected, $actual, $message = '')
896+
{
897+
return PHPUnit_Framework_Assert::assertSameSize($expected, $actual, $message);
898+
}
899+
874900
/**
875901
* Assert the presence, absence, or count of elements in a document matching
876902
* the CSS $selector, regardless of the contents of those elements.

PHPUnit/Framework/Constraint/Count.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected function getCountOf($other)
106106
protected function failureDescription($other, $description, $not)
107107
{
108108
return sprintf(
109-
'Count of %d does not match expected count of %d.',
109+
'Actual size %d does not match expected size %d.',
110110
$this->getCountOf($other),
111111
$this->expectedCount
112112
);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* PHPUnit
4+
*
5+
* Copyright (c) 2002-2011, Sebastian Bergmann <[email protected]>.
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
*
12+
* * Redistributions of source code must retain the above copyright
13+
* notice, this list of conditions and the following disclaimer.
14+
*
15+
* * Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in
17+
* the documentation and/or other materials provided with the
18+
* distribution.
19+
*
20+
* * Neither the name of Sebastian Bergmann nor the names of his
21+
* contributors may be used to endorse or promote products derived
22+
* from this software without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35+
* POSSIBILITY OF SUCH DAMAGE.
36+
*
37+
* @package PHPUnit
38+
* @subpackage Framework_Constraint
39+
* @author Sebastian Bergmann <[email protected]>
40+
* @copyright 2002-2011 Sebastian Bergmann <[email protected]>
41+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
42+
* @link http://www.phpunit.de/
43+
* @since File available since Release 3.6.0
44+
*/
45+
46+
/**
47+
*
48+
*
49+
* @package PHPUnit
50+
* @subpackage Framework_Constraint
51+
* @author Sebastian Bergmann <[email protected]>
52+
* @copyright 2002-2011 Sebastian Bergmann <[email protected]>
53+
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
54+
* @version Release: @package_version@
55+
* @link http://www.phpunit.de/
56+
* @since Class available since Release 3.6.0
57+
*/
58+
class PHPUnit_Framework_Constraint_SameSize extends PHPUnit_Framework_Constraint_Count
59+
{
60+
/**
61+
* @var integer
62+
*/
63+
protected $expectedCount;
64+
65+
/**
66+
* @param integer $expected
67+
*/
68+
public function __construct($expected)
69+
{
70+
$this->expectedCount = $this->getCountOf($expected);
71+
}
72+
}

Tests/Framework/AssertTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4207,7 +4207,7 @@ public function testAssertCount()
42074207
}
42084208

42094209
catch (PHPUnit_Framework_AssertionFailedError $e) {
4210-
$this->assertEquals('Count of 3 does not match expected count of 2.', $e->getMessage());
4210+
$this->assertEquals('Actual size 3 does not match expected size 2.', $e->getMessage());
42114211

42124212
return;
42134213
}

Tests/Framework/ConstraintTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2529,7 +2529,7 @@ public function testConstraintCountFailing()
25292529

25302530
catch (PHPUnit_Framework_ExpectationFailedException $e) {
25312531
$this->assertEquals(
2532-
'Count of 2 does not match expected count of 5.',
2532+
'Actual size 2 does not match expected size 5.',
25332533
$e->getDescription()
25342534
);
25352535

package.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@
163163
<file baseinstalldir="/" name="PCREMatch.php" role="php">
164164
<tasks:replace from="@package_version@" to="version" type="package-info" />
165165
</file>
166+
<file baseinstalldir="/" name="SameSize.php" role="php">
167+
<tasks:replace from="@package_version@" to="version" type="package-info" />
168+
</file>
166169
<file baseinstalldir="/" name="StringContains.php" role="php">
167170
<tasks:replace from="@package_version@" to="version" type="package-info" />
168171
</file>

0 commit comments

Comments
 (0)