-
-
Notifications
You must be signed in to change notification settings - Fork 670
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PHPUnit] Add ReplaceAssertArraySubsetRector
- Loading branch information
1 parent
b12cf38
commit 8f9dd11
Showing
6 changed files
with
325 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
packages/PHPUnit/src/Rector/MethodCall/ReplaceAssertArraySubsetRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\Rector\MethodCall; | ||
|
||
use PhpParser\BuilderHelpers; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\BinaryOp\Identical; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Name; | ||
use Rector\Rector\AbstractPHPUnitRector; | ||
use Rector\RectorDefinition\CodeSample; | ||
use Rector\RectorDefinition\RectorDefinition; | ||
|
||
/** | ||
* @see https://github.com/sebastianbergmann/phpunit/issues/3494 | ||
* @see https://github.com/sebastianbergmann/phpunit/issues/3495 | ||
*/ | ||
final class ReplaceAssertArraySubsetRector extends AbstractPHPUnitRector | ||
{ | ||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition('Replace deprecated "assertArraySubset()" method with alternative methods', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function test() | ||
{ | ||
$checkedArray = []; | ||
$this->assertArraySubset([ | ||
'cache_directory' => 'new_value', | ||
], $checkedArray); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function test() | ||
{ | ||
$checkedArray = []; | ||
$this->assertArrayHasKey('cache_directory', $checkedArray); | ||
$this->assertSame('new_value', $checkedArray['cache_directory']); | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class, StaticCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall|StaticCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isInTestClass($node)) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node, 'assertArraySubset')) { | ||
return null; | ||
} | ||
|
||
$expectedValue = $node->args[0]->value; | ||
|
||
// nothing we can do | ||
if (! $expectedValue instanceof Array_) { | ||
return null; | ||
} | ||
|
||
$expectedKeys = []; | ||
$expectedValuesByKeys = []; | ||
|
||
foreach ($expectedValue->items as $arrayItem) { | ||
if ($arrayItem->key === null) { | ||
continue; | ||
} | ||
|
||
$expectedKeys[] = $arrayItem->key; | ||
|
||
$key = $this->getValue($arrayItem->key); | ||
$expectedValuesByKeys[$key] = $arrayItem->value; | ||
} | ||
|
||
$this->addKeyAsserts($node, $expectedKeys); | ||
|
||
if ($expectedKeys === []) { | ||
// no keys → intersect! | ||
$arrayIntersect = new FuncCall(new Name('array_intersect')); | ||
$arrayIntersect->args[] = new Arg($expectedValue); | ||
$arrayIntersect->args[] = $node->args[1]; | ||
|
||
$identical = new Identical($arrayIntersect, $expectedValue); | ||
|
||
$assertTrue = $this->createCallWithName($node, 'assertTrue'); | ||
$assertTrue->args[] = new Arg($identical); | ||
|
||
$this->addNodeAfterNode($assertTrue, $node); | ||
} else { | ||
$this->addValueAsserts($node, $expectedValuesByKeys); | ||
} | ||
|
||
$this->removeNode($node); | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param MethodCall|StaticCall $node | ||
* @param Expr[] $expectedKeys | ||
*/ | ||
private function addKeyAsserts(Node $node, array $expectedKeys): void | ||
{ | ||
foreach ($expectedKeys as $expectedKey) { | ||
$assertArrayHasKey = $this->createCallWithName($node, 'assertArrayHasKey'); | ||
$assertArrayHasKey->args[0] = new Arg($expectedKey); | ||
$assertArrayHasKey->args[1] = $node->args[1]; | ||
|
||
$this->addNodeAfterNode($assertArrayHasKey, $node); | ||
} | ||
} | ||
|
||
/** | ||
* @param MethodCall|StaticCall $node | ||
* @param Expr[] $expectedValuesByKeys | ||
*/ | ||
private function addValueAsserts(Node $node, array $expectedValuesByKeys): void | ||
{ | ||
foreach ($expectedValuesByKeys as $key => $expectedValue) { | ||
$assertSame = $this->createCallWithName($node, 'assertSame'); | ||
$assertSame->args[0] = new Arg($expectedValue); | ||
|
||
$arrayDimFetch = new ArrayDimFetch($node->args[1]->value, BuilderHelpers::normalizeValue($key)); | ||
$assertSame->args[1] = new Arg($arrayDimFetch); | ||
|
||
$this->addNodeAfterNode($assertSame, $node); | ||
} | ||
} | ||
|
||
/** | ||
* @param StaticCall|MethodCall $node | ||
* @return StaticCall|MethodCall | ||
*/ | ||
private function createCallWithName(Node $node, string $name): Node | ||
{ | ||
return $node instanceof MethodCall ? new MethodCall($node->var, $name) : new StaticCall($node->class, $name); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...es/PHPUnit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ReplaceAssertArraySubsetRector\Fixture; | ||
|
||
class SomeTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function test() | ||
{ | ||
$checkedArray = []; | ||
|
||
$this->assertArraySubset([ | ||
'cache_directory' => 'new_value', | ||
], $checkedArray); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ReplaceAssertArraySubsetRector\Fixture; | ||
|
||
class SomeTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function test() | ||
{ | ||
$checkedArray = []; | ||
$this->assertArrayHasKey('cache_directory', $checkedArray); | ||
$this->assertSame('new_value', $checkedArray['cache_directory']); | ||
} | ||
} | ||
|
||
?> |
46 changes: 46 additions & 0 deletions
46
...PHPUnit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/issue_2069.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ReplaceAssertArraySubsetRector\Fixture; | ||
|
||
class Issue2069 extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function test() | ||
{ | ||
$result = ['Hello' => 'a', 'World!' => 'b']; | ||
$this->assertArraySubset(['World!' => 'b', 'Hello' => 'a'], $result); | ||
} | ||
|
||
public function shouldWorkToo() | ||
{ | ||
$result = ['a', 'b', 'c', 'd']; | ||
$this->assertArraySubset(['b', 'c'], $result); | ||
$this->assertArraySubset(['a', 'c', 'b'], $result); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ReplaceAssertArraySubsetRector\Fixture; | ||
|
||
class Issue2069 extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function test() | ||
{ | ||
$result = ['Hello' => 'a', 'World!' => 'b']; | ||
$this->assertArrayHasKey('World!', $result); | ||
$this->assertArrayHasKey('Hello', $result); | ||
$this->assertSame('b', $result['World!']); | ||
$this->assertSame('a', $result['Hello']); | ||
} | ||
|
||
public function shouldWorkToo() | ||
{ | ||
$result = ['a', 'b', 'c', 'd']; | ||
$this->assertTrue(array_intersect(['b', 'c'], $result) === ['b', 'c']); | ||
$this->assertTrue(array_intersect(['a', 'c', 'b'], $result) === ['a', 'c', 'b']); | ||
} | ||
} | ||
|
||
?> |
57 changes: 57 additions & 0 deletions
57
...PHPUnit/tests/Rector/MethodCall/ReplaceAssertArraySubsetRector/Fixture/issue_2237.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ReplaceAssertArraySubsetRector\Fixture; | ||
|
||
class Issue2237 extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function test() | ||
{ | ||
$result = [ | ||
'a' => 'item a', | ||
'b' => 'item k', // wrong value | ||
'c' => [ | ||
// 'd' not present | ||
'g' => 'item g', | ||
], | ||
]; | ||
|
||
$this->assertArraySubset([ | ||
'a' => 'item a', | ||
'b' => 'item b', | ||
'c' => [ | ||
'd' => 'item d', | ||
], | ||
], $result); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ReplaceAssertArraySubsetRector\Fixture; | ||
|
||
class Issue2237 extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function test() | ||
{ | ||
$result = [ | ||
'a' => 'item a', | ||
'b' => 'item k', // wrong value | ||
'c' => [ | ||
// 'd' not present | ||
'g' => 'item g', | ||
], | ||
]; | ||
$this->assertArrayHasKey('a', $result); | ||
$this->assertArrayHasKey('b', $result); | ||
$this->assertArrayHasKey('c', $result); | ||
$this->assertSame('item a', $result['a']); | ||
$this->assertSame('item b', $result['b']); | ||
$this->assertSame([ | ||
'd' => 'item d', | ||
], $result['c']); | ||
} | ||
} | ||
|
||
?> |
23 changes: 23 additions & 0 deletions
23
...s/Rector/MethodCall/ReplaceAssertArraySubsetRector/ReplaceAssertArraySubsetRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ReplaceAssertArraySubsetRector; | ||
|
||
use Rector\PHPUnit\Rector\MethodCall\ReplaceAssertArraySubsetRector; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ReplaceAssertArraySubsetRectorTest extends AbstractRectorTestCase | ||
{ | ||
public function test(): void | ||
{ | ||
$this->doTestFiles([ | ||
__DIR__ . '/Fixture/fixture.php.inc', | ||
__DIR__ . '/Fixture/issue_2069.php.inc', | ||
__DIR__ . '/Fixture/issue_2237.php.inc', | ||
]); | ||
} | ||
|
||
protected function getRectorClass(): string | ||
{ | ||
return ReplaceAssertArraySubsetRector::class; | ||
} | ||
} |