Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eqArraySubset, similar to what already exists in PHPUnit #283

Merged
merged 1 commit into from Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -200,6 +200,7 @@ Assertion::e164(string $value);
Assertion::email(mixed $value);
Assertion::endsWith(mixed $string, string $needle);
Assertion::eq(mixed $value, mixed $value2);
Assertion::eqArraySubset(mixed $value, mixed $value2);
Assertion::extensionLoaded(mixed $value);
Assertion::extensionVersion(string $extension, string $operator, mixed $version);
Assertion::false(mixed $value);
Expand Down
23 changes: 23 additions & 0 deletions lib/Assert/Assertion.php
Expand Up @@ -40,6 +40,7 @@
* @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.
* @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.
* @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.
* @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.
* @method static bool allExtensionLoaded(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that extension is loaded for all values.
* @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.
* @method static bool allFalse(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False for all values.
Expand Down Expand Up @@ -127,6 +128,7 @@
* @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.
* @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.
* @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.
* @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.
* @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.
* @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.
* @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.
Expand Down Expand Up @@ -309,6 +311,27 @@ public static function eq($value, $value2, $message = null, $propertyPath = null
return true;
}

/**
* Assert that the array contains the subset.
*
* @param mixed $value
* @param mixed $value2
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function eqArraySubset($value, $value2, $message = null, $propertyPath = null)
{
static::isArray($value, $message, $propertyPath);
static::isArray($value2, $message, $propertyPath);

$patched = \array_replace_recursive($value, $value2);
static::eq($patched, $value, $message, $propertyPath);

return true;
}

/**
* Assert that two values are the same (using ===).
*
Expand Down
1 change: 1 addition & 0 deletions lib/Assert/AssertionChain.php
Expand Up @@ -41,6 +41,7 @@
* @method AssertionChain email(string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL).
* @method AssertionChain endsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars.
* @method AssertionChain eq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using ==).
* @method AssertionChain eqArraySubset(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that the array contains the subset.
* @method AssertionChain extensionLoaded(string|callable $message = null, string $propertyPath = null) Assert that extension is loaded.
* @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.
* @method AssertionChain false(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False.
Expand Down
1 change: 1 addition & 0 deletions lib/Assert/LazyAssertion.php
Expand Up @@ -40,6 +40,7 @@
* @method $this email(string|callable $message = null, string $propertyPath = null) Assert that value is an email address (using input_filter/FILTER_VALIDATE_EMAIL).
* @method $this endsWith(string $needle, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string ends with a sequence of chars.
* @method $this eq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are equal (using ==).
* @method $this eqArraySubset(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that the array contains the subset.
* @method $this extensionLoaded(string|callable $message = null, string $propertyPath = null) Assert that extension is loaded.
* @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.
* @method $this false(string|callable $message = null, string $propertyPath = null) Assert that the value is boolean False.
Expand Down
51 changes: 51 additions & 0 deletions tests/Assert/Tests/AssertTest.php
Expand Up @@ -2209,4 +2209,55 @@ public function testNotBase64()
{
Assertion::base64('wrong-content');
}

public function invalidEqArraySubsetProvider()
{
return [
'firstArgumentNotArray' => ['notArray', []],
'secondArgumentNotArray' => [[], 'notArray'],
];
}

public function testEqArraySubsetValid()
{
$this->assertTrue(Assertion::eqArraySubset([
'a' => [
'a1' => 'a2',
'a3' => 'a4',
],
'b' => [
'b1' => 'b2',
],
], [
'a' => [
'a1' => 'a2',
],
]));
}

/**
* @dataProvider invalidEqArraySubsetProvider
*
* @expectedException \Assert\InvalidArgumentException
* @expectedExceptionCode \Assert\Assertion::INVALID_ARRAY
*/
public function testEqArraySubsetInvalid($value, $value2)
{
Assertion::eqArraySubset($value, $value2);
}

/**
* @dataProvider invalidEqArraySubsetProvider
*
* @expectedException \Assert\InvalidArgumentException
* @expectedExceptionCode \Assert\Assertion::INVALID_EQ
*/
public function testEqArraySubsetMismatchingSubset()
{
Assertion::eqArraySubset([
'a' => 'b',
], [
'c' => 'd',
]);
}
}