-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TestWithAnnotationToAttributeRector
- Loading branch information
1 parent
4414d46
commit f8d95a7
Showing
7 changed files
with
209 additions
and
12 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
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
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
35 changes: 35 additions & 0 deletions
35
...butes/Rector/ClassMethod/TestWithAnnotationToAttributeRector/Fixture/some_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,35 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\ClassMethod\TestWithAnnotationToAttributeRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeFixture extends TestCase | ||
{ | ||
/** | ||
* @testWith ["foo"] | ||
* ["bar"] | ||
*/ | ||
public function testOne(): void | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\ClassMethod\TestWithAnnotationToAttributeRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SomeFixture extends TestCase | ||
{ | ||
#[\PHPUnit\Framework\Attributes\TestWith(['foo'])] | ||
#[\PHPUnit\Framework\Attributes\TestWith(['bar'])] | ||
public function testOne(): void | ||
{ | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
...assMethod/TestWithAnnotationToAttributeRector/TestWithAnnotationToAttributeRectorTest.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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\ClassMethod\TestWithAnnotationToAttributeRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class TestWithAnnotationToAttributeRectorTest 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'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ributes/Rector/ClassMethod/TestWithAnnotationToAttributeRector/config/configured_rule.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\TestWithAnnotationToAttributeRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../../config/config.php'); | ||
|
||
$rectorConfig->rule(TestWithAnnotationToAttributeRector::class); | ||
}; |
122 changes: 122 additions & 0 deletions
122
rules/AnnotationsToAttributes/Rector/ClassMethod/TestWithAnnotationToAttributeRector.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,122 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod; | ||
|
||
use Nette\Utils\Json; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; | ||
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory; | ||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @changelog https://docs.phpunit.de/en/10.0/annotations.html#testwith | ||
* @changelog https://docs.phpunit.de/en/10.2/attributes.html#testwith | ||
* | ||
* @see \Rector\PHPUnit\Tests\AnnotationsToAttributes\Rector\ClassMethod\TestWithAnnotationToAttributeRector\TestWithAnnotationToAttributeRectorTest | ||
*/ | ||
final class TestWithAnnotationToAttributeRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer, | ||
private readonly PhpAttributeGroupFactory $phpAttributeGroupFactory, | ||
private readonly PhpDocTagRemover $phpDocTagRemover, | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Change @testWith() annotation to #[TestWith] attribute', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
final class SomeFixture extends TestCase | ||
{ | ||
/** | ||
* @testWith ["foo"] | ||
* ["bar"] | ||
*/ | ||
public function test(): void | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit\Framework\Attributes\TestWith; | ||
final class SomeFixture extends TestCase | ||
{ | ||
#[TestWith(['foo'])] | ||
#[TestWith(['bar'])] | ||
public function test(): void | ||
{ | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ClassMethod::class]; | ||
} | ||
|
||
/** | ||
* @param ClassMethod $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->testsNodeAnalyzer->isTestClassMethod($node)) { | ||
return null; | ||
} | ||
|
||
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node); | ||
|
||
$testWithPhpDocTagNodes = $phpDocInfo->getTagsByName('testWith'); | ||
if ($testWithPhpDocTagNodes === []) { | ||
return null; | ||
} | ||
|
||
$attributeGroups = []; | ||
|
||
foreach ($testWithPhpDocTagNodes as $testWithPhpDocTagNode) { | ||
/** @var PhpDocTagNode $testWithPhpDocTagNode */ | ||
if (! $testWithPhpDocTagNode->value instanceof GenericTagValueNode) { | ||
// not supported yet | ||
continue; | ||
} | ||
|
||
// test from doc blocks | ||
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $testWithPhpDocTagNode); | ||
|
||
$genericTagValueNode = $testWithPhpDocTagNode->value; | ||
$testWithItems = explode("\n", trim($genericTagValueNode->value)); | ||
|
||
foreach ($testWithItems as $testWithItem) { | ||
$jsonArray = Json::decode(trim($testWithItem)); | ||
$attributeGroups[] = $this->phpAttributeGroupFactory->createFromClassWithItems('PHPUnit\Framework\Attributes\TestWith', [$jsonArray]); | ||
} | ||
} | ||
|
||
$node->attrGroups = array_merge($node->attrGroups, $attributeGroups); | ||
|
||
return $node; | ||
} | ||
} |