From 00b3aa01450f387c531ebd14982f096a077c4229 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Mon, 25 Nov 2019 16:23:49 +0100 Subject: [PATCH] Closes #3495 --- ChangeLog-9.0.md | 1 + src/Framework/Assert.php | 38 ------ src/Framework/Assert/Functions.php | 20 --- src/Framework/Constraint/ArraySubset.php | 129 ------------------ .../Framework/Constraint/ArraySubsetTest.php | 89 ------------ 5 files changed, 1 insertion(+), 276 deletions(-) delete mode 100644 src/Framework/Constraint/ArraySubset.php delete mode 100644 tests/unit/Framework/Constraint/ArraySubsetTest.php diff --git a/ChangeLog-9.0.md b/ChangeLog-9.0.md index c4356727fec..394cb4cd720 100644 --- a/ChangeLog-9.0.md +++ b/ChangeLog-9.0.md @@ -15,6 +15,7 @@ All notable changes of the PHPUnit 9.0 release series are documented in this fil * Implemented [#3339](https://github.com/sebastianbergmann/phpunit/issues/3339): Remove assertions (and helper methods) that operate on (non-public) attributes * Implemented [#3342](https://github.com/sebastianbergmann/phpunit/issues/3342): Remove optional parameters of `assertEquals()` and `assertNotEquals()` * Implemented [#3426](https://github.com/sebastianbergmann/phpunit/issues/3426): Clean up `assertContains()` and `assertNotContains()` +* Implemented [#3495](https://github.com/sebastianbergmann/phpunit/issues/3495): Remove `assertArraySubset()` [9.0.0]: https://github.com/sebastianbergmann/phpunit/compare/8.5...master diff --git a/src/Framework/Assert.php b/src/Framework/Assert.php index a6ba72176bd..7d380e3d60f 100644 --- a/src/Framework/Assert.php +++ b/src/Framework/Assert.php @@ -14,7 +14,6 @@ use DOMDocument; use DOMElement; use PHPUnit\Framework\Constraint\ArrayHasKey; -use PHPUnit\Framework\Constraint\ArraySubset; use PHPUnit\Framework\Constraint\Callback; use PHPUnit\Framework\Constraint\ClassHasAttribute; use PHPUnit\Framework\Constraint\ClassHasStaticAttribute; @@ -102,43 +101,6 @@ public static function assertArrayHasKey($key, $array, string $message = ''): vo static::assertThat($array, $constraint, $message); } - /** - * Asserts that an array has a specified subset. - * - * @param array|ArrayAccess $subset - * @param array|ArrayAccess $array - * - * @throws ExpectationFailedException - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException - * @throws Exception - * - * @codeCoverageIgnore - * - * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3494 - */ - public static function assertArraySubset($subset, $array, bool $checkForObjectIdentity = false, string $message = ''): void - { - self::createWarning('assertArraySubset() is deprecated and will be removed in PHPUnit 9.'); - - if (!(\is_array($subset) || $subset instanceof ArrayAccess)) { - throw InvalidArgumentException::create( - 1, - 'array or ArrayAccess' - ); - } - - if (!(\is_array($array) || $array instanceof ArrayAccess)) { - throw InvalidArgumentException::create( - 2, - 'array or ArrayAccess' - ); - } - - $constraint = new ArraySubset($subset, $checkForObjectIdentity); - - static::assertThat($array, $constraint, $message); - } - /** * Asserts that an array does not have a specified key. * diff --git a/src/Framework/Assert/Functions.php b/src/Framework/Assert/Functions.php index b7edfbcb52f..db10120dbad 100644 --- a/src/Framework/Assert/Functions.php +++ b/src/Framework/Assert/Functions.php @@ -81,26 +81,6 @@ function assertArrayHasKey($key, $array, string $message = ''): void Assert::assertArrayHasKey(...\func_get_args()); } -/** - * Asserts that an array has a specified subset. - * - * @param array|ArrayAccess $subset - * @param array|ArrayAccess $array - * - * @throws ExpectationFailedException - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException - * @throws Exception - * - * @codeCoverageIgnore - * - * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3494 - * @see Assert::assertArraySubset - */ -function assertArraySubset($subset, $array, bool $checkForObjectIdentity = false, string $message = ''): void -{ - Assert::assertArraySubset(...\func_get_args()); -} - /** * Asserts that an array does not have a specified key. * diff --git a/src/Framework/Constraint/ArraySubset.php b/src/Framework/Constraint/ArraySubset.php deleted file mode 100644 index a60c26150ed..00000000000 --- a/src/Framework/Constraint/ArraySubset.php +++ /dev/null @@ -1,129 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace PHPUnit\Framework\Constraint; - -use PHPUnit\Framework\ExpectationFailedException; -use SebastianBergmann\Comparator\ComparisonFailure; - -/** - * Constraint that asserts that the array it is evaluated for has a specified subset. - * - * Uses array_replace_recursive() to check if a key value subset is part of the - * subject array. - * - * @codeCoverageIgnore - * - * @deprecated https://github.com/sebastianbergmann/phpunit/issues/3494 - */ -final class ArraySubset extends Constraint -{ - /** - * @var iterable - */ - private $subset; - - /** - * @var bool - */ - private $strict; - - public function __construct(iterable $subset, bool $strict = false) - { - $this->strict = $strict; - $this->subset = $subset; - } - - /** - * Evaluates the constraint for parameter $other - * - * If $returnResult is set to false (the default), an exception is thrown - * in case of a failure. null is returned otherwise. - * - * If $returnResult is true, the result of the evaluation is returned as - * a boolean value instead: true in case of success, false in case of a - * failure. - * - * @throws ExpectationFailedException - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException - */ - public function evaluate($other, string $description = '', bool $returnResult = false) - { - //type cast $other & $this->subset as an array to allow - //support in standard array functions. - $other = $this->toArray($other); - $this->subset = $this->toArray($this->subset); - - $patched = \array_replace_recursive($other, $this->subset); - - if ($this->strict) { - $result = $other === $patched; - } else { - $result = $other == $patched; - } - - if ($returnResult) { - return $result; - } - - if (!$result) { - $f = new ComparisonFailure( - $patched, - $other, - \var_export($patched, true), - \var_export($other, true) - ); - - $this->fail($other, $description, $f); - } - } - - /** - * Returns a string representation of the constraint. - * - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException - */ - public function toString(): string - { - return 'has the subset ' . $this->exporter()->export($this->subset); - } - - /** - * Returns the description of the failure - * - * The beginning of failure messages is "Failed asserting that" in most - * cases. This method should return the second part of that sentence. - * - * @param mixed $other evaluated value or object - * - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException - */ - protected function failureDescription($other): string - { - return 'an array ' . $this->toString(); - } - - private function toArray(iterable $other): array - { - if (\is_array($other)) { - return $other; - } - - if ($other instanceof \ArrayObject) { - return $other->getArrayCopy(); - } - - if ($other instanceof \Traversable) { - return \iterator_to_array($other); - } - - // Keep BC even if we know that array would not be the expected one - return (array) $other; - } -} diff --git a/tests/unit/Framework/Constraint/ArraySubsetTest.php b/tests/unit/Framework/Constraint/ArraySubsetTest.php deleted file mode 100644 index 2e074fc37e3..00000000000 --- a/tests/unit/Framework/Constraint/ArraySubsetTest.php +++ /dev/null @@ -1,89 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -namespace PHPUnit\Framework\Constraint; - -use PHPUnit\Framework\ExpectationFailedException; - -/** - * @small - */ -final class ArraySubsetTest extends ConstraintTestCase -{ - public static function evaluateDataProvider(): array - { - return [ - 'loose array subset and array other' => [ - 'expected' => true, - 'subset' => ['bar' => 0], - 'other' => ['foo' => '', 'bar' => '0'], - 'strict' => false, - ], - 'strict array subset and array other' => [ - 'expected' => false, - 'subset' => ['bar' => 0], - 'other' => ['foo' => '', 'bar' => '0'], - 'strict' => true, - ], - 'loose array subset and ArrayObject other' => [ - 'expected' => true, - 'subset' => ['bar' => 0], - 'other' => new \ArrayObject(['foo' => '', 'bar' => '0']), - 'strict' => false, - ], - 'strict ArrayObject subset and array other' => [ - 'expected' => true, - 'subset' => new \ArrayObject(['bar' => 0]), - 'other' => ['foo' => '', 'bar' => 0], - 'strict' => true, - ], - ]; - } - - /** - * @param bool $expected - * @param array|\Traversable $subset - * @param array|\Traversable $other - * @param bool $strict - * - * @throws ExpectationFailedException - * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException - * @dataProvider evaluateDataProvider - */ - public function testEvaluate($expected, $subset, $other, $strict): void - { - $constraint = new ArraySubset($subset, $strict); - - $this->assertSame($expected, $constraint->evaluate($other, '', true)); - } - - public function testEvaluateWithArrayAccess(): void - { - $arrayAccess = new \ArrayAccessible(['foo' => 'bar']); - - $constraint = new ArraySubset(['foo' => 'bar']); - - $this->assertTrue($constraint->evaluate($arrayAccess, '', true)); - } - - public function testEvaluateFailMessage(): void - { - $constraint = new ArraySubset(['foo' => 'bar']); - - try { - $constraint->evaluate(['baz' => 'bar'], '', false); - $this->fail(\sprintf('Expected %s to be thrown.', ExpectationFailedException::class)); - } catch (ExpectationFailedException $expectedException) { - $comparisonFailure = $expectedException->getComparisonFailure(); - $this->assertNotNull($comparisonFailure); - $this->assertStringContainsString("'foo' => 'bar'", $comparisonFailure->getExpectedAsString()); - $this->assertStringContainsString("'baz' => 'bar'", $comparisonFailure->getActualAsString()); - } - } -}