[YamlParser] move test NamedServiceToClassRector, allow config

This commit is contained in:
TomasVotruba 2017-12-02 18:51:41 +01:00
parent a69fcc020f
commit b10f48e328
7 changed files with 55 additions and 31 deletions

View File

@ -7,6 +7,17 @@ use Rector\YamlParser\Contract\Rector\YamlRectorInterface;
/**
* Turn custom names of services,
* to class based ones.
*
* Before:
*
* services:
* some_class:
* class: SomeClass
*
* After:
*
* services:
* SomeClass: ~
*/
final class NamedServiceToClassRector implements YamlRectorInterface
{

View File

@ -0,0 +1,34 @@
<?php declare(strict_types=1);
namespace Rector\YamlParser\Tests\Rector\Contrib\Symfony;
use PHPUnit\Framework\TestCase;
use Rector\DependencyInjection\ContainerFactory;
use Rector\YamlParser\YamlRectorCollector;
final class NamedServiceToClassRectorTest extends TestCase
{
/**
* @var YamlRectorCollector
*/
private $yamlRectorCollector;
protected function setUp(): void
{
$container = (new ContainerFactory())->createWithConfig($this->provideConfig());
$this->yamlRectorCollector = $container->get(YamlRectorCollector::class);
}
public function test(): void
{
$this->assertStringEqualsFile(
__DIR__ . '/Source/expected.some_services.yml',
$this->yamlRectorCollector->processFile(__DIR__ . '/Source/some_services.yml')
);
}
private function provideConfig(): string
{
return __DIR__ . '/Source/config.yml';
}
}

View File

@ -0,0 +1,2 @@
rectors:
Rector\YamlParser\Rector\Contrib\Symfony\NamedServiceToClassRector: ~

View File

@ -1,29 +0,0 @@
<?php declare(strict_types=1);
namespace Rector\YamlParser\Tests;
use Rector\Tests\AbstractContainerAwareTestCase;
use Rector\YamlParser\YamlRectorCollector;
final class YamlRectorCollectorTest extends AbstractContainerAwareTestCase
{
/**
* @var YamlRectorCollector
*/
private $yamlRectorCollector;
protected function setUp(): void
{
$this->yamlRectorCollector = $this->container->get(YamlRectorCollector::class);
}
public function testSymfonyServiceRename(): void
{
$file = __DIR__ . '/YamlRectorCollectorSource/some_services.yml';
$this->assertStringEqualsFile(
__DIR__ . '/YamlRectorCollectorSource/expected.some_services.yml',
$this->yamlRectorCollector->processFile($file)
);
}
}

View File

@ -4,6 +4,7 @@ namespace Rector\Configuration\Validator;
use Rector\Contract\Rector\RectorInterface;
use Rector\Exception\Validator\InvalidRectorClassException;
use Rector\YamlParser\Contract\Rector\YamlRectorInterface;
final class RectorClassValidator
{
@ -36,10 +37,15 @@ final class RectorClassValidator
return;
}
if (is_a($rector, YamlRectorInterface::class, true)) {
return;
}
throw new InvalidRectorClassException(sprintf(
'Rector "%s" is not supported. Use class that implements "%s".',
'Rector "%s" is not supported. Use class that implements "%s" or "%s".',
$rector,
RectorInterface::class
RectorInterface::class,
YamlRectorInterface::class
));
}
}