Merge pull request #15 from TomasVotruba/symfony-test-class

Symfony Constraint test class
This commit is contained in:
Tomáš Votruba 2017-09-01 10:42:49 +02:00 committed by GitHub
commit c5f5f795dc
5 changed files with 127 additions and 0 deletions

View 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();
}
}

View File

@ -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';
}
}

View File

@ -0,0 +1,8 @@
<?php declare (strict_types=1);
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
class MyCustomValidatorTest extends \Symfony\Component\Validator\Test\ConstraintValidatorTestCase
{
// ...
}

View File

@ -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];
}
}

View File

@ -0,0 +1,8 @@
<?php declare (strict_types=1);
use Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest;
class MyCustomValidatorTest extends AbstractConstraintValidatorTest
{
// ...
}