forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SpecificAssertContainsWithoutIdentityRector: add phpunit 9 compatibil…
…ity rector As of sebastianbergmann/phpunit#3426 assertContains() and assertNotContains() will perform strict comparisons starting with PHPUnit 9 where non-strict comparisons were performed in PHPUnit 8 and earlier; assertContainsEqual() and assertNotContainsEqual() should be used instead if needed non-strict comparison
- Loading branch information
1 parent
3b68551
commit e2b5ff1
Showing
8 changed files
with
254 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
services: | ||
Rector\PHPUnit\Rector\Class_\TestListenerToHooksRector: null | ||
Rector\PHPUnit\Rector\MethodCall\ExplicitPhpErrorApiRector: null | ||
Rector\PHPUnit\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector: null |
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
110 changes: 110 additions & 0 deletions
110
rules/phpunit/src/Rector/MethodCall/SpecificAssertContainsWithoutIdentityRector.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,110 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Identifier; | ||
use PHPStan\Type\StringType; | ||
use Rector\Core\Rector\AbstractPHPUnitRector; | ||
use Rector\Core\RectorDefinition\CodeSample; | ||
use Rector\Core\RectorDefinition\RectorDefinition; | ||
|
||
/** | ||
* @see https://github.com/sebastianbergmann/phpunit/issues/3426 | ||
* @see \Rector\PHPUnit\Tests\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector\SpecificAssertContainsWithoutIdentityRectorTest | ||
*/ | ||
final class SpecificAssertContainsWithoutIdentityRector extends AbstractPHPUnitRector | ||
{ | ||
/** | ||
* @var string[][] | ||
*/ | ||
private const OLD_METHODS_NAMES_TO_NEW_NAMES = [ | ||
'string' => [ | ||
'assertContains' => 'assertContainsEquals', | ||
'assertNotContains' => 'assertNotContainsEquals', | ||
], | ||
]; | ||
|
||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition( | ||
'Change assertContains()/assertNotContains() with non-strict comparison to new specific alternatives', | ||
[ | ||
new CodeSample( | ||
<<<'PHP' | ||
<?php | ||
final class SomeTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function test() | ||
{ | ||
$objects = [ new \stdClass(), new \stdClass(), new \stdClass() ]; | ||
$this->assertContains(new \stdClass(), $objects, 'message', false, false); | ||
$this->assertNotContains(new \stdClass(), $objects, 'message', false, false); | ||
} | ||
} | ||
PHP | ||
, | ||
<<<'PHP' | ||
<?php | ||
final class SomeTest extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$objects = [ new \stdClass(), new \stdClass(), new \stdClass() ]; | ||
$this->assertContainsEquals(new \stdClass(), $objects, 'message'); | ||
$this->assertNotContainsEquals(new \stdClass(), $objects, 'message'); | ||
} | ||
} | ||
PHP | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class, StaticCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall|StaticCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isPHPUnitMethodNames($node, ['assertContains', 'assertNotContains'])) { | ||
return null; | ||
} | ||
|
||
//when second argument is string: do nothing | ||
if ($this->isStaticType($node->args[1]->value, StringType::class)) { | ||
return null; | ||
} | ||
|
||
//when less then 5 arguments given: do nothing | ||
if (! isset($node->args[4]) || $node->args[4]->value === null) { | ||
return null; | ||
} | ||
|
||
//when 5th argument check identity is true: do nothing | ||
if ($this->isValue($node->args[4]->value, true)) { | ||
return null; | ||
} | ||
|
||
/* here we search for element of array without identity check and we can replace functions */ | ||
$methodName = $this->getName($node->name); | ||
|
||
$node->name = new Identifier(self::OLD_METHODS_NAMES_TO_NEW_NAMES['string'][$methodName]); | ||
unset($node->args[3], $node->args[4]); | ||
|
||
return $node; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...tor/MethodCall/SpecificAssertContainsWithoutIdentityRector/Fixture/check_identity.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,18 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
final class CheckIdentity extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$objects = [ new stdClass(), new stdClass(), new stdClass() ]; | ||
$this->assertContains(new stdClass(), $objects, 'message', false, true); | ||
$this->assertNotContains(new stdClass(), $objects, 'message', false, true); | ||
} | ||
} | ||
|
||
?> |
37 changes: 37 additions & 0 deletions
37
...sts/Rector/MethodCall/SpecificAssertContainsWithoutIdentityRector/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,37 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
final class SomeTest extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$objects = [ new stdClass(), new stdClass(), new stdClass() ]; | ||
$this->assertContains(new stdClass(), $objects, 'message', false, false); | ||
$this->assertNotContains(new stdClass(), $objects, 'message', false, false); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
final class SomeTest extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$objects = [ new stdClass(), new stdClass(), new stdClass() ]; | ||
$this->assertContainsEquals(new stdClass(), $objects, 'message'); | ||
$this->assertNotContainsEquals(new stdClass(), $objects, 'message'); | ||
} | ||
} | ||
|
||
?> |
16 changes: 16 additions & 0 deletions
16
.../MethodCall/SpecificAssertContainsWithoutIdentityRector/Fixture/partial_arguments.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,16 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
final class PartialArguments extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$objects = [ new stdClass(), new stdClass(), new stdClass() ]; | ||
$this->assertContains(new stdClass(), $objects, 'message', false); | ||
$this->assertNotContains(new stdClass(), $objects, 'message', false); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...sts/Rector/MethodCall/SpecificAssertContainsWithoutIdentityRector/Fixture/strings.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,14 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class NoChangeOnStrings extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->assertContains('foo', 'foo bar', 'message', false, false); | ||
$this->assertNotContains('foo', 'foo bar', 'message', false, false); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...icAssertContainsWithoutIdentityRector/SpecificAssertContainsWithoutIdentityRectorTest.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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector; | ||
|
||
use Iterator; | ||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
use Rector\PHPUnit\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector; | ||
|
||
final class SpecificAssertContainsWithoutIdentityRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $file): void | ||
{ | ||
$this->doTestFile($file); | ||
} | ||
|
||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
protected function getRectorClass(): string | ||
{ | ||
return SpecificAssertContainsWithoutIdentityRector::class; | ||
} | ||
} |