Merge pull request #562 from rectorphp/container-get-to-construcotr-rector-alias

[FrameworkBundle] Add test for services alias as well
This commit is contained in:
Tomáš Votruba 2018-08-09 09:41:40 +02:00 committed by GitHub
commit 337d069df7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 2 deletions

View File

@ -35,7 +35,7 @@ final class ContainerGetToConstructorInjectionRector extends AbstractToConstruct
[
new CodeSample(
<<<'CODE_SAMPLE'
class MyCommand extends ContainerAwareCommand
final class SomeCommand extends ContainerAwareCommand
{
public function someMethod()
{
@ -47,7 +47,7 @@ class MyCommand extends ContainerAwareCommand
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class MyCommand extends Command
final class SomeCommand extends Command
{
public function __construct(SomeService $someService)
{

View File

@ -2,6 +2,8 @@
namespace Rector\Symfony\Tests\FrameworkBundle\AbstractToConstructorInjectionRectorSource;
use Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeTranslator;
use Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeTranslatorInterface;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
@ -21,6 +23,11 @@ final class SomeKernelClass extends Kernel
protected function build(ContainerBuilder $containerBuilder): void
{
$containerBuilder->register('some_service', 'stdClass');
$containerBuilder->register('translator.data_collector', SomeTranslator::class);
$containerBuilder->setAlias('translator', 'translator.data_collector');
$containerBuilder->setAlias(SomeTranslatorInterface::class, 'translator.data_collector');
}
public function getCacheDir()

View File

@ -0,0 +1,8 @@
<?php declare(strict_types=1);
namespace Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource;
final class SomeTranslator
{
}

View File

@ -0,0 +1,8 @@
<?php declare(strict_types=1);
namespace Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource;
interface SomeTranslatorInterface
{
}

View File

@ -21,6 +21,7 @@ final class ContainerGetToConstructorInjectionRectorTest extends AbstractRectorT
public function provideWrongToFixedFiles(): Iterator
{
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc'];
yield [__DIR__ . '/Wrong/wrong2.php.inc', __DIR__ . '/Correct/correct2.php.inc'];
}
protected function provideConfig(): string

View File

@ -0,0 +1,21 @@
<?php declare(strict_types=1);
use Rector\Symfony\Tests\FrameworkBundle\ContainerGetToConstructorInjectionRector\Source\ContainerAwareParentClass;
final class AnotherCommand extends ContainerAwareParentClass
{
/**
* @var \Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeTranslator
*/
private $someTranslator;
public function __construct(\Rector\Symfony\Tests\Rector\FrameworkBundle\AbstractToConstructorInjectionRectorSource\SomeTranslator $someTranslator)
{
$this->someTranslator = $someTranslator;
}
protected function execute()
{
$someService = $this->someTranslator;
$someService = $this->someTranslator->translateSomething();
}
}

View File

@ -0,0 +1,13 @@
<?php declare(strict_types=1);
use Rector\Symfony\Tests\FrameworkBundle\ContainerGetToConstructorInjectionRector\Source\ContainerAwareParentClass;
final class AnotherCommand extends ContainerAwareParentClass
{
protected function execute()
{
$someService = $this->getContainer()->get('translator');
$someService = $this->getContainer()->get('translator')->translateSomething();
}
}