From ad2c8a7750209fbd49a9189fb01dae557e61dfc6 Mon Sep 17 00:00:00 2001 From: Anna Filina Date: Thu, 25 Jul 2019 05:19:56 -0400 Subject: [PATCH] Add eqArraySubset, similar to what already exists in PHPUnit. (#283) Thank you. Nice addition. --- README.md | 1 + lib/Assert/Assertion.php | 23 ++++++++++++++ lib/Assert/AssertionChain.php | 1 + lib/Assert/LazyAssertion.php | 1 + tests/Assert/Tests/AssertTest.php | 51 +++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+) diff --git a/README.md b/README.md index 43e9e464..92f65d07 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index 247fa62f..0b06b065 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -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. @@ -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. @@ -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 ===). * diff --git a/lib/Assert/AssertionChain.php b/lib/Assert/AssertionChain.php index 5d48377a..4d3ca1c0 100644 --- a/lib/Assert/AssertionChain.php +++ b/lib/Assert/AssertionChain.php @@ -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. diff --git a/lib/Assert/LazyAssertion.php b/lib/Assert/LazyAssertion.php index 1153a09a..abbaf4bc 100644 --- a/lib/Assert/LazyAssertion.php +++ b/lib/Assert/LazyAssertion.php @@ -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. diff --git a/tests/Assert/Tests/AssertTest.php b/tests/Assert/Tests/AssertTest.php index c210834e..cfaf8494 100644 --- a/tests/Assert/Tests/AssertTest.php +++ b/tests/Assert/Tests/AssertTest.php @@ -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', + ]); + } }