-
Notifications
You must be signed in to change notification settings - Fork 63
[PHPUnit10] Replace deleted PHPUnit methods #438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
samsonasik
merged 1 commit into
rectorphp:main
from
66Ton99:phpunit10-removed-assersions
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...nit100/Rector/MethodCall/PropertyExistsWithoutAssertRector/Fixture/fixture_static.php.inc
This file contains hidden or 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\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector\Fixture; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class MyPropertyExistsWithoutAssertRectorTest extends TestCase | ||
| { | ||
| public function test() | ||
| { | ||
| $this->assertClassHasStaticAttribute('property', 'stdClass'); | ||
| $this->assertClassHasStaticAttribute('property', stdClass::class); | ||
| $this->classHasStaticAttribute('property', stdClass::class, 'message'); | ||
| $this->assertClassNotHasStaticAttribute('property', 'Namespaced\stdClass', 'message'); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector\Fixture; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| final class MyPropertyExistsWithoutAssertRectorTest extends TestCase | ||
| { | ||
| public function test() | ||
| { | ||
| $this->assertTrue(property_exists('stdClass', 'property')); | ||
| $this->assertTrue(property_exists(stdClass::class, 'property')); | ||
| $this->assertTrue(property_exists(stdClass::class, 'property'), 'message'); | ||
| $this->assertFalse(property_exists('Namespaced\stdClass', 'property'), 'message'); | ||
| } | ||
| } | ||
|
|
||
| ?> |
28 changes: 28 additions & 0 deletions
28
...or/MethodCall/PropertyExistsWithoutAssertRector/PropertyExistsWithoutAssertRectorTest.php
This file contains hidden or 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,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\PHPUnit\Tests\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class PropertyExistsWithoutAssertRectorTest extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/configured_rule.php'; | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
...PHPUnit100/Rector/MethodCall/PropertyExistsWithoutAssertRector/config/configured_rule.php
This file contains hidden or 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,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\Config\RectorConfig; | ||
| use Rector\PHPUnit\PHPUnit100\Rector\MethodCall\PropertyExistsWithoutAssertRector; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->rule(PropertyExistsWithoutAssertRector::class); | ||
| }; |
104 changes: 104 additions & 0 deletions
104
rules/PHPUnit100/Rector/MethodCall/PropertyExistsWithoutAssertRector.php
This file contains hidden or 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,104 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\PHPUnit\PHPUnit100\Rector\MethodCall; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Arg; | ||
| use PhpParser\Node\Expr\ClassConstFetch; | ||
| use PhpParser\Node\Expr\FuncCall; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Name; | ||
| use Rector\PHPUnit\NodeAnalyzer\IdentifierManipulator; | ||
| use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||
| use Rector\Rector\AbstractRector; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @url https://github.com/sebastianbergmann/phpunit/issues/4601 | ||
| * | ||
| * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\PropertyExistsWithoutAssertRector\PropertyExistsWithoutAssertRectorTest | ||
| */ | ||
| final class PropertyExistsWithoutAssertRector extends AbstractRector | ||
| { | ||
| /** | ||
| * @var array<string, string> | ||
| */ | ||
| private const RENAME_METHODS_WITH_OBJECT_MAP = [ | ||
| 'assertClassHasStaticAttribute' => 'assertTrue', | ||
| 'classHasStaticAttribute' => 'assertTrue', | ||
| 'assertClassNotHasStaticAttribute' => 'assertFalse', | ||
| ]; | ||
|
|
||
| public function __construct( | ||
| private readonly IdentifierManipulator $identifierManipulator, | ||
| private readonly TestsNodeAnalyzer $testsNodeAnalyzer | ||
| ) { | ||
| } | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Replace delited PHPUnit methods: assertClassHasStaticAttribute, classHasStaticAttribute and assertClassNotHasStaticAttribute by property_exists()', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| $this->assertClassHasStaticAttribute("Class", "property"); | ||
| $this->classHasStaticAttribute("Class", "property"); | ||
| $this->assertClassNotHasStaticAttribute("Class", "property"); | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| $this->assertTrue(property_exists("Class", "property")); | ||
| $this->assertTrue(property_exists("Class", "property")); | ||
| $this->assertFalse(property_exists("Class", "property")); | ||
| CODE_SAMPLE | ||
| ), | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [MethodCall::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param MethodCall $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| $map = self::RENAME_METHODS_WITH_OBJECT_MAP; | ||
| if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames($node, array_keys($map))) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($node->isFirstClassCallable() || ! isset($node->getArgs()[0], $node->getArgs()[1])) { | ||
| return null; | ||
| } | ||
|
|
||
| $firstNode = new Arg($node->getArgs()[0]->value); | ||
|
|
||
| if ($node->getArgs()[1]->value instanceof ClassConstFetch) { | ||
| $secondNode = $node->getArgs()[1]; | ||
| } else { | ||
| $secondNode = new Arg($node->getArgs()[1]->value); | ||
| } | ||
|
|
||
| $funcCall = new FuncCall(new Name('property_exists'), [$secondNode, $firstNode]); | ||
|
|
||
| $newArgs = $this->nodeFactory->createArgs([$funcCall]); | ||
|
|
||
| unset($node->args[0], $node->args[1]); | ||
| $node->args = array_merge($newArgs, $node->getArgs()); | ||
|
|
||
| $this->identifierManipulator->renameNodeWithMap($node, $map); | ||
|
|
||
| return $node; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.