-
-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Transform] Add CommunityTestCaseRector
- Loading branch information
1 parent
4647213
commit 267321e
Showing
6 changed files
with
275 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
39 changes: 39 additions & 0 deletions
39
rules/transform/src/NodeFactory/ProvideConfigFilePathClassMethodFactory.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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Transform\NodeFactory; | ||
|
||
use PhpParser\Node\Expr\BinaryOp\Concat; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Scalar\MagicConst\Dir; | ||
use PhpParser\Node\Scalar\String_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Return_; | ||
use Rector\Core\PhpParser\Node\NodeFactory; | ||
|
||
final class ProvideConfigFilePathClassMethodFactory | ||
{ | ||
/** | ||
* @var NodeFactory | ||
*/ | ||
private $nodeFactory; | ||
|
||
public function __construct(NodeFactory $nodeFactory) | ||
{ | ||
$this->nodeFactory = $nodeFactory; | ||
} | ||
|
||
public function create(): ClassMethod | ||
{ | ||
$classMethod = $this->nodeFactory->createPublicMethod('provideConfigFilePath'); | ||
$classMethod->returnType = new Identifier('string'); | ||
|
||
$concat = new Concat(new Dir(), new String_('/config/configured_rule.php')); | ||
$return = new Return_($concat); | ||
|
||
$classMethod->stmts[] = $return; | ||
|
||
return $classMethod; | ||
} | ||
} |
146 changes: 146 additions & 0 deletions
146
rules/transform/src/Rector/Class_/CommunityTestCaseRector.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,146 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Transform\Rector\Class_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ClassConstFetch; | ||
use PhpParser\Node\Name\FullyQualified; | ||
use PhpParser\Node\Stmt\Class_; | ||
use PhpParser\Node\Stmt\ClassMethod; | ||
use PhpParser\Node\Stmt\Return_; | ||
use Rector\Core\Exception\ShouldNotHappenException; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\FileSystemRector\ValueObject\AddedFileWithContent; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\Testing\PhpConfigPrinter\PhpConfigPrinterFactory; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Rector\Transform\NodeFactory\ProvideConfigFilePathClassMethodFactory; | ||
use Symplify\PhpConfigPrinter\Printer\SmartPhpConfigPrinter; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
/** | ||
* @see \Rector\Transform\Tests\Rector\Class_\CommunityTestCaseRector\CommunityTestCaseRectorTest | ||
*/ | ||
final class CommunityTestCaseRector extends AbstractRector | ||
{ | ||
/** | ||
* @var ProvideConfigFilePathClassMethodFactory | ||
*/ | ||
private $provideConfigFilePathClassMethodFactory; | ||
|
||
/** | ||
* @var SmartPhpConfigPrinter | ||
*/ | ||
private $smartPhpConfigPrinter; | ||
|
||
public function __construct( | ||
ProvideConfigFilePathClassMethodFactory $provideConfigFilePathClassMethodFactory, | ||
PhpConfigPrinterFactory $phpConfigPrinterFactory | ||
) { | ||
$this->provideConfigFilePathClassMethodFactory = $provideConfigFilePathClassMethodFactory; | ||
$this->smartPhpConfigPrinter = $phpConfigPrinterFactory->create(); | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change Rector test case to Community version', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
final class SomeClassTest extends AbstractRectorTestCase | ||
{ | ||
public function getRectorClass(): string | ||
{ | ||
return SomeRector::class; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
use Rector\Testing\PHPUnit\AbstractCommunityRectorTestCase; | ||
final class SomeClassTest extends AbstractCommunityRectorTestCase | ||
{ | ||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Class_::class]; | ||
} | ||
|
||
/** | ||
* @param Class_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node->extends === null) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->extends, AbstractRectorTestCase::class)) { | ||
return null; | ||
} | ||
|
||
$getRectorClassMethod = $node->getMethod('getRectorClass'); | ||
if (! $getRectorClassMethod instanceof ClassMethod) { | ||
return null; | ||
} | ||
|
||
$node->extends = new FullyQualified('Rector\Testing\PHPUnit\AbstractCommunityRectorTestCase'); | ||
|
||
$this->removeNode($getRectorClassMethod); | ||
$node->stmts[] = $this->provideConfigFilePathClassMethodFactory->create(); | ||
|
||
$this->createConfigFile($getRectorClassMethod); | ||
|
||
return $node; | ||
} | ||
|
||
private function createConfigFile(ClassMethod $getRectorClassMethod): void | ||
{ | ||
$onlyStmt = $getRectorClassMethod->stmts[0] ?? null; | ||
if (! $onlyStmt instanceof Return_) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
if (! $onlyStmt->expr instanceof ClassConstFetch) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
$rectorClass = $this->valueResolver->getValue($onlyStmt->expr); | ||
if (! is_string($rectorClass)) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
$phpConfigFileContent = $this->smartPhpConfigPrinter->printConfiguredServices([ | ||
$rectorClass => null, | ||
]); | ||
|
||
$fileInfo = $getRectorClassMethod->getAttribute(AttributeKey::FILE_INFO); | ||
if (! $fileInfo instanceof SmartFileInfo) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
|
||
$configFilePath = dirname($fileInfo->getRealPath()) . '/config/configured_rule.php'; | ||
$addedFileWithContent = new AddedFileWithContent($configFilePath, $phpConfigFileContent); | ||
$this->removedAndAddedFilesCollector->addAddedFile($addedFileWithContent); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
rules/transform/tests/Rector/Class_/CommunityTestCaseRector/CommunityTestCaseRectorTest.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,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Transform\Tests\Rector\Class_\CommunityTestCaseRector; | ||
|
||
use Iterator; | ||
use Rector\FileSystemRector\ValueObject\AddedFileWithContent; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Rector\Transform\Rector\Class_\CommunityTestCaseRector; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
use Symplify\SmartFileSystem\SmartFileSystem; | ||
|
||
final class CommunityTestCaseRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo, AddedFileWithContent $addedFileWithContent): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
$this->assertFileWithContentWasAdded($addedFileWithContent); | ||
} | ||
|
||
public function provideData(): Iterator | ||
{ | ||
$smartFileSystem = new SmartFileSystem(); | ||
|
||
yield [ | ||
new SmartFileInfo(__DIR__ . '/Fixture/some_class.php.inc'), | ||
new AddedFileWithContent( | ||
$this->getFixtureTempDirectory() . '/config/configured_rule.php', | ||
$smartFileSystem->readFile(__DIR__ . '/Expected/config/configured_rule.php') | ||
), | ||
]; | ||
} | ||
|
||
protected function getRectorClass(): string | ||
{ | ||
return CommunityTestCaseRector::class; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...transform/tests/Rector/Class_/CommunityTestCaseRector/Expected/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\Transform\Tests\Rector\Class_\CommunityTestCaseRector\Fixture\SomeRector; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$services = $containerConfigurator->services(); | ||
|
||
$services->set(SomeRector::class); | ||
}; |
31 changes: 31 additions & 0 deletions
31
rules/transform/tests/Rector/Class_/CommunityTestCaseRector/Fixture/some_class.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,31 @@ | ||
<?php | ||
|
||
namespace Rector\Transform\Tests\Rector\Class_\CommunityTestCaseRector\Fixture; | ||
|
||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class SomeClassTest extends AbstractRectorTestCase | ||
{ | ||
public function getRectorClass(): string | ||
{ | ||
return SomeRector::class; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Transform\Tests\Rector\Class_\CommunityTestCaseRector\Fixture; | ||
|
||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class SomeClassTest extends \Rector\Testing\PHPUnit\AbstractCommunityRectorTestCase | ||
{ | ||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} | ||
|
||
?> |