diff --git a/docs/AllRectorsOverview.md b/docs/AllRectorsOverview.md index 5a7f11d1740..95139bed5a0 100644 --- a/docs/AllRectorsOverview.md +++ b/docs/AllRectorsOverview.md @@ -6054,7 +6054,7 @@ Replace deprecated "assertArraySubset()" method with alternative methods - $this->assertArraySubset([ - 'cache_directory' => 'new_value', -- ], $checkedArray); +- ], $checkedArray, true); + $this->assertArrayHasKey('cache_directory', $checkedArray); + $this->assertSame('new_value', $checkedArray['cache_directory']); } diff --git a/rules/phpunit/src/Rector/MethodCall/ReplaceAssertArraySubsetRector.php b/rules/phpunit/src/Rector/MethodCall/ReplaceAssertArraySubsetRector.php index d43bb274d27..54c1f144309 100644 --- a/rules/phpunit/src/Rector/MethodCall/ReplaceAssertArraySubsetRector.php +++ b/rules/phpunit/src/Rector/MethodCall/ReplaceAssertArraySubsetRector.php @@ -49,7 +49,7 @@ public function test() $this->assertArraySubset([ 'cache_directory' => 'new_value', - ], $checkedArray); + ], $checkedArray, true); } } PHP @@ -176,11 +176,13 @@ private function addKeyAsserts(Node $node): void */ private function addValueAsserts(Node $node): void { + $assertMethodName = $this->resolveAssertMethodName($node); + foreach ($this->expectedValuesWithKeys as $expectedValueWithKey) { $expectedKey = $expectedValueWithKey['key']; $expectedValue = $expectedValueWithKey['value']; - $assertSame = $this->createPHPUnitCallWithName($node, 'assertSame'); + $assertSame = $this->createPHPUnitCallWithName($node, $assertMethodName); $assertSame->args[0] = new Arg($expectedValue); $arrayDimFetch = new ArrayDimFetch($node->args[1]->value, BuilderHelpers::normalizeValue($expectedKey)); @@ -189,4 +191,18 @@ private function addValueAsserts(Node $node): void $this->addNodeAfterNode($assertSame, $node); } } + + /** + * @param MethodCall|StaticCall $node + */ + private function resolveAssertMethodName(Node $node): string + { + if (! isset($node->args[2])) { + return 'assertEquals'; + } + + $isStrict = $this->getValue($node->args[2]->value); + + return $isStrict ? 'assertSame' : 'assertEquals'; + } } diff --git a/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/assert_same_assert_equal.php.inc b/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/assert_same_assert_equal.php.inc new file mode 100644 index 00000000000..67198c34c71 --- /dev/null +++ b/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/assert_same_assert_equal.php.inc @@ -0,0 +1,45 @@ + 'two']; + + $this->assertArraySubset($expectedSubset, $checkedArray); + + $this->assertArraySubset($expectedSubset, $checkedArray, true); + + $this->assertArraySubset($expectedSubset, $checkedArray, false); + } +} + +?> +----- + 'two']; + $this->assertArrayHasKey('cache_directory', $checkedArray); + $this->assertEquals('two', $checkedArray['cache_directory']); + $this->assertArrayHasKey('cache_directory', $checkedArray); + $this->assertSame('two', $checkedArray['cache_directory']); + $this->assertArrayHasKey('cache_directory', $checkedArray); + $this->assertEquals('two', $checkedArray['cache_directory']); + } +} + +?> diff --git a/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/fixture.php.inc b/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/fixture.php.inc index 482dc453ca5..3ebe5144f33 100644 --- a/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/fixture.php.inc +++ b/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/fixture.php.inc @@ -10,7 +10,7 @@ class SomeTest extends \PHPUnit\Framework\TestCase $this->assertArraySubset([ 'cache_directory' => 'new_value', - ], $checkedArray); + ], $checkedArray, true); } } diff --git a/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/issue_2069.php.inc b/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/issue_2069.php.inc index dbc478742d9..abc8324e031 100644 --- a/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/issue_2069.php.inc +++ b/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/issue_2069.php.inc @@ -31,8 +31,8 @@ class Issue2069 extends \PHPUnit\Framework\TestCase $result = ['Hello' => 'a', 'World!' => 'b']; $this->assertArrayHasKey('World!', $result); $this->assertArrayHasKey('Hello', $result); - $this->assertSame('b', $result['World!']); - $this->assertSame('a', $result['Hello']); + $this->assertEquals('b', $result['World!']); + $this->assertEquals('a', $result['Hello']); } public function shouldWorkToo() diff --git a/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/issue_2237.php.inc b/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/issue_2237.php.inc index 7d0bd86cf68..94c7ac79b01 100644 --- a/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/issue_2237.php.inc +++ b/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/issue_2237.php.inc @@ -21,7 +21,7 @@ class Issue2237 extends \PHPUnit\Framework\TestCase 'c' => [ 'd' => 'item d', ], - ], $result); + ], $result, true); } } diff --git a/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/issue_2279.php.inc b/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/issue_2279.php.inc index 66684c4db30..017168f1467 100644 --- a/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/issue_2279.php.inc +++ b/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/issue_2279.php.inc @@ -10,7 +10,7 @@ class Issue2279 extends TestCase { $this->assertArraySubset([ static::KEY_RESPONSE_BAD_REQUEST => static::VALUE_RESPONSE_BAD_REQUEST, - ], $parameters->getPost()->getResponses()); + ], $parameters->getPost()->getResponses(), true); } } diff --git a/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/multilevel_array.php.inc b/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/multilevel_array.php.inc index 8884a53b7e6..6361745a2a4 100644 --- a/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/multilevel_array.php.inc +++ b/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/multilevel_array.php.inc @@ -15,7 +15,7 @@ class MultiLevelArray extends \PHPUnit\Framework\TestCase ], ]; - $this->assertArraySubset($expectedSubset, $checkedArray); + $this->assertArraySubset($expectedSubset, $checkedArray, true); } public function otherTest() @@ -30,7 +30,7 @@ class MultiLevelArray extends \PHPUnit\Framework\TestCase ], ], ], - $checkedArray + $checkedArray, true ); } } diff --git a/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/variable.php.inc b/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/variable.php.inc index d4cf4f32440..85fcf760819 100644 --- a/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/variable.php.inc +++ b/rules/phpunit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/variable.php.inc @@ -11,7 +11,7 @@ class VariableTest extends \PHPUnit\Framework\TestCase 'cache_directory' => 'new_value', ]; - $this->assertArraySubset($expectedSubset, $checkedArray); + $this->assertArraySubset($expectedSubset, $checkedArray, true); } }