mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-01 00:40:52 +00:00
Merge pull request #15 from TomasVotruba/symfony-test-class
Symfony Constraint test class
This commit is contained in:
commit
c5f5f795dc
48
src/Rector/AbstractChangeParentClassRector.php
Normal file
48
src/Rector/AbstractChangeParentClassRector.php
Normal file
|
@ -0,0 +1,48 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Name;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
|
||||
abstract class AbstractChangeParentClassRector extends AbstractRector
|
||||
{
|
||||
public function isCandidate(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof Class_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->getParentClassName($node) === $this->getOldClassName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Class_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
$node->extends = new Name('\\' . $this->getNewClassName());
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
abstract protected function getOldClassName(): string;
|
||||
|
||||
abstract protected function getNewClassName(): string;
|
||||
|
||||
private function getParentClassName(Class_ $classNode): string
|
||||
{
|
||||
if (! $classNode->extends) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/** @var Name $parentClassName */
|
||||
$parentClassNameNode = $classNode->extends;
|
||||
|
||||
/** @var Node\Name\FullyQualified $fsqName */
|
||||
$fsqName = $parentClassNameNode->getAttribute('resolvedName');
|
||||
|
||||
return $fsqName->toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Contrib\Symfony;
|
||||
|
||||
use Rector\Deprecation\SetNames;
|
||||
use Rector\Rector\AbstractChangeParentClassRector;
|
||||
|
||||
/**
|
||||
* Ref: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#validator
|
||||
*
|
||||
* Converts all:
|
||||
* Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest
|
||||
*
|
||||
* into:
|
||||
* Symfony\Component\Validator\Test\ConstraintValidatorTestCase
|
||||
*/
|
||||
final class ConstraintValidatorTestClassRenameRector extends AbstractChangeParentClassRector
|
||||
{
|
||||
public function getSetName(): string
|
||||
{
|
||||
return SetNames::SYMFONY;
|
||||
}
|
||||
|
||||
public function sinceVersion(): float
|
||||
{
|
||||
return 4.0;
|
||||
}
|
||||
|
||||
protected function getOldClassName(): string
|
||||
{
|
||||
return 'Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest';
|
||||
}
|
||||
|
||||
protected function getNewClassName(): string
|
||||
{
|
||||
return 'Symfony\Component\Validator\Test\ConstraintValidatorTestCase';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
|
||||
|
||||
class MyCustomValidatorTest extends \Symfony\Component\Validator\Test\ConstraintValidatorTestCase
|
||||
{
|
||||
// ...
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Contrib\Symfony\ConstraintValidatorTestClassRenameRector;
|
||||
|
||||
use Rector\Rector\Contrib\Symfony\ConstraintValidatorTestClassRenameRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class Test extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFileMatchesExpectedContent(
|
||||
__DIR__ . '/Wrong/wrong.php.inc',
|
||||
__DIR__ . '/Correct/correct.php.inc'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
protected function getRectorClasses(): array
|
||||
{
|
||||
return [ConstraintValidatorTestClassRenameRector::class];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
<?php declare (strict_types=1);
|
||||
|
||||
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
|
||||
|
||||
class MyCustomValidatorTest extends AbstractConstraintValidatorTest
|
||||
{
|
||||
// ...
|
||||
}
|
Loading…
Reference in New Issue
Block a user