Skip to content

Commit 17b3d3c

Browse files
committed
Merge branch 'master' into v3
* master: Add eqArraySubset, similar to what already exists in PHPUnit. (#283)
2 parents ce139b6 + ad2c8a7 commit 17b3d3c

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ Assertion::e164(string $value);
200200
Assertion::email(mixed $value);
201201
Assertion::endsWith(mixed $string, string $needle);
202202
Assertion::eq(mixed $value, mixed $value2);
203+
Assertion::eqArraySubset(mixed $value, mixed $value2);
203204
Assertion::extensionLoaded(mixed $value);
204205
Assertion::extensionVersion(string $extension, string $operator, mixed $version);
205206
Assertion::false(mixed $value);

lib/Assert/Assertion.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @method static bool allEmail(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL) for all values.
4141
* @method static bool allEndsWith(mixed $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars for all values.
4242
* @method static bool allEq(mixed $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using ==) for all values.
43+
* @method static bool allEqArraySubset(mixed $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that the array contains the subset for all values.
4344
* @method static bool allExtensionLoaded(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded for all values.
4445
* @method static bool allExtensionVersion(string $extension, string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded and a specific version is installed for all values.
4546
* @method static bool allFalse(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False for all values.
@@ -127,6 +128,7 @@
127128
* @method static bool nullOrEmail(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL) or that the value is null.
128129
* @method static bool nullOrEndsWith(mixed|null $string, string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars or that the value is null.
129130
* @method static bool nullOrEq(mixed|null $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using ==) or that the value is null.
131+
* @method static bool nullOrEqArraySubset(mixed|null $value, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that the array contains the subset or that the value is null.
130132
* @method static bool nullOrExtensionLoaded(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded or that the value is null.
131133
* @method static bool nullOrExtensionVersion(string|null $extension, string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded and a specific version is installed or that the value is null.
132134
* @method static bool nullOrFalse(mixed|null $value, string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False or that the value is null.
@@ -309,6 +311,27 @@ public static function eq($value, $value2, $message = null, $propertyPath = null
309311
return true;
310312
}
311313

314+
/**
315+
* Assert that the array contains the subset.
316+
*
317+
* @param mixed $value
318+
* @param mixed $value2
319+
* @param string|callable|null $message
320+
* @param string|null $propertyPath
321+
*
322+
* @return bool
323+
*/
324+
public static function eqArraySubset($value, $value2, $message = null, $propertyPath = null)
325+
{
326+
static::isArray($value, $message, $propertyPath);
327+
static::isArray($value2, $message, $propertyPath);
328+
329+
$patched = \array_replace_recursive($value, $value2);
330+
static::eq($patched, $value, $message, $propertyPath);
331+
332+
return true;
333+
}
334+
312335
/**
313336
* Assert that two values are the same (using ===).
314337
*

lib/Assert/AssertionChain.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* @method AssertionChain email(string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL).
4242
* @method AssertionChain endsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars.
4343
* @method AssertionChain eq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using ==).
44+
* @method AssertionChain eqArraySubset(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that the array contains the subset.
4445
* @method AssertionChain extensionLoaded(string|callable $message = null, string $propertyPath = null) Assert that extension is loaded.
4546
* @method AssertionChain extensionVersion(string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded and a specific version is installed.
4647
* @method AssertionChain false(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False.

lib/Assert/LazyAssertion.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @method $this email(string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL).
4141
* @method $this endsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars.
4242
* @method $this eq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using ==).
43+
* @method $this eqArraySubset(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that the array contains the subset.
4344
* @method $this extensionLoaded(string|callable $message = null, string $propertyPath = null) Assert that extension is loaded.
4445
* @method $this extensionVersion(string $operator, mixed $version, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded and a specific version is installed.
4546
* @method $this false(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False.

tests/Assert/Tests/AssertTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,4 +2209,55 @@ public function testNotBase64()
22092209
{
22102210
Assertion::base64('wrong-content');
22112211
}
2212+
2213+
public function invalidEqArraySubsetProvider()
2214+
{
2215+
return [
2216+
'firstArgumentNotArray' => ['notArray', []],
2217+
'secondArgumentNotArray' => [[], 'notArray'],
2218+
];
2219+
}
2220+
2221+
public function testEqArraySubsetValid()
2222+
{
2223+
$this->assertTrue(Assertion::eqArraySubset([
2224+
'a' => [
2225+
'a1' => 'a2',
2226+
'a3' => 'a4',
2227+
],
2228+
'b' => [
2229+
'b1' => 'b2',
2230+
],
2231+
], [
2232+
'a' => [
2233+
'a1' => 'a2',
2234+
],
2235+
]));
2236+
}
2237+
2238+
/**
2239+
* @dataProvider invalidEqArraySubsetProvider
2240+
*
2241+
* @expectedException \Assert\InvalidArgumentException
2242+
* @expectedExceptionCode \Assert\Assertion::INVALID_ARRAY
2243+
*/
2244+
public function testEqArraySubsetInvalid($value, $value2)
2245+
{
2246+
Assertion::eqArraySubset($value, $value2);
2247+
}
2248+
2249+
/**
2250+
* @dataProvider invalidEqArraySubsetProvider
2251+
*
2252+
* @expectedException \Assert\InvalidArgumentException
2253+
* @expectedExceptionCode \Assert\Assertion::INVALID_EQ
2254+
*/
2255+
public function testEqArraySubsetMismatchingSubset()
2256+
{
2257+
Assertion::eqArraySubset([
2258+
'a' => 'b',
2259+
], [
2260+
'c' => 'd',
2261+
]);
2262+
}
22122263
}

0 commit comments

Comments
 (0)