Skip to content

Commit

Permalink
[Transform] Add CommunityTestCaseRector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Feb 10, 2021
1 parent 4647213 commit 267321e
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
RenameMethodRector::METHOD_CALL_RENAMES => ValueObjectInliner::inline([
new MethodCallRename(AbstractType::class, 'setDefaultOptions', 'configureOptions'),
new MethodCallRename(Html::class, 'add', 'addHtml'),
new MethodCallRename('Rector\Renaming\Tests\Rector\MethodCall\RenameMethodRector\Fixture\DemoFile', 'notify', '__invoke'),
new MethodCallRename(
'Rector\Renaming\Tests\Rector\MethodCall\RenameMethodRector\Fixture\DemoFile',
'notify',
'__invoke'
),
new MethodCallRename('*Presenter', 'run', '__invoke'),
new MethodCallRename(
\Rector\Renaming\Tests\Rector\MethodCall\RenameMethodRector\Fixture\SkipSelfMethodRename::class,
Expand Down
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 rules/transform/src/Rector/Class_/CommunityTestCaseRector.php
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);
}
}
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;
}
}
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);
};
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';
}
}

?>

0 comments on commit 267321e

Please sign in to comment.