|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\PHPUnit70\Rector\ClassMethod; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Stmt\ClassMethod; |
| 9 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; |
| 10 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 11 | +use Rector\Comments\NodeDocBlock\DocBlockUpdater; |
| 12 | +use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockTagReplacer; |
| 13 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 14 | +use Rector\Rector\AbstractRector; |
| 15 | +use Rector\RuleDoc\CodeSample\CodeSample; |
| 16 | +use Rector\RuleDoc\RuleDefinition; |
| 17 | +use Rector\VersionBonding\Contract\ComposerPackageConstraintInterface; |
| 18 | +use Rector\VersionBonding\ValueObject\ComposerPackageConstraint; |
| 19 | + |
| 20 | +/** |
| 21 | + * @see \Rector\PHPUnit\Tests\PHPUnit70\Rector\ClassMethod\ScenarioToTestAnnotationRector\ScenarioToTestAnnotationRectorTest |
| 22 | + */ |
| 23 | +final class ScenarioToTestAnnotationRector extends AbstractRector implements ComposerPackageConstraintInterface |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + private readonly DocBlockTagReplacer $docBlockTagReplacer, |
| 27 | + private readonly DocBlockUpdater $docBlockUpdater, |
| 28 | + private readonly PhpDocInfoFactory $phpDocInfoFactory, |
| 29 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 30 | + ) { |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * the "@scenario" annotation was dropped in PHPUnit 7.0 |
| 35 | + */ |
| 36 | + public function provideComposerPackageConstraint(): ComposerPackageConstraint |
| 37 | + { |
| 38 | + return new ComposerPackageConstraint('phpunit/phpunit', '>=7.0'); |
| 39 | + } |
| 40 | + |
| 41 | + public function getRuleDefinition(): RuleDefinition |
| 42 | + { |
| 43 | + return new RuleDefinition('Change `@scenario` annotation to `@test` in a test class', [ |
| 44 | + new CodeSample( |
| 45 | + <<<'CODE_SAMPLE' |
| 46 | +/** |
| 47 | + * @scenario |
| 48 | + */ |
| 49 | +public function someMethod() |
| 50 | +{ |
| 51 | +} |
| 52 | +CODE_SAMPLE |
| 53 | + , |
| 54 | + <<<'CODE_SAMPLE' |
| 55 | +/** |
| 56 | + * @test |
| 57 | + */ |
| 58 | +public function someMethod() |
| 59 | +{ |
| 60 | +} |
| 61 | +CODE_SAMPLE |
| 62 | + ), |
| 63 | + ]); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @return array<class-string<Node>> |
| 68 | + */ |
| 69 | + public function getNodeTypes(): array |
| 70 | + { |
| 71 | + return [ClassMethod::class]; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * @param ClassMethod $node |
| 76 | + */ |
| 77 | + public function refactor(Node $node): ?Node |
| 78 | + { |
| 79 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + $phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); |
| 84 | + if (! $phpDocInfo instanceof PhpDocInfo) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + if (! $this->docBlockTagReplacer->replaceTagByAnother($phpDocInfo, 'scenario', 'test')) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($node); |
| 93 | + |
| 94 | + return $node; |
| 95 | + } |
| 96 | +} |
0 commit comments