From 773eca7794facb9bbd3055db8da1055e37c2b103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Sat, 4 Feb 2017 14:46:50 +0100 Subject: [PATCH] [DependencyInjection] Tests + refacto for "instanceof" definitions --- .../Loader/YamlFileLoader.php | 15 +++++++++++++ .../Tests/Compiler/PassConfigTest.php | 5 +++-- .../Fixtures/xml/services_instanceof.xml | 12 +++++++++++ .../Fixtures/yaml/services_instanceof.yml | 10 +++++++++ .../Tests/Loader/XmlFileLoaderTest.php | 21 +++++++++++++++++++ .../Tests/Loader/YamlFileLoaderTest.php | 21 +++++++++++++++++++ 6 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_instanceof.xml create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_instanceof.yml diff --git a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php index f5e97f3cb9..6ad27499e7 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php @@ -283,6 +283,21 @@ class YamlFileLoader extends FileLoader return $defaults; } + private function isUnderscoredParamValid($content, $name, $file) + { + if (!isset($content['services'][$name])) { + return false; + } + + if (!is_array($underscoreParam = $content['services'][$name])) { + throw new InvalidArgumentException(sprintf('Service "%s" key must be an array, "%s" given in "%s".', $name, gettype($underscoreParam), $file)); + } + + // @deprecated condition, to be removed in 4.0 + + return !isset($underscoreParam['alias']) && !isset($underscoreParam['class']) && !isset($underscoreParam['factory']); + } + /** * @param array $service * diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/PassConfigTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/PassConfigTest.php index 98ae2745e7..23f225f7d0 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/PassConfigTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/PassConfigTest.php @@ -22,6 +22,7 @@ class PassConfigTest extends \PHPUnit_Framework_TestCase public function testPassOrdering() { $config = new PassConfig(); + $config->setBeforeOptimizationPasses(array()); $pass1 = $this->getMockBuilder(CompilerPassInterface::class)->getMock(); $config->addPass($pass1, PassConfig::TYPE_BEFORE_OPTIMIZATION, 10); @@ -30,7 +31,7 @@ class PassConfigTest extends \PHPUnit_Framework_TestCase $config->addPass($pass2, PassConfig::TYPE_BEFORE_OPTIMIZATION, 30); $passes = $config->getBeforeOptimizationPasses(); - $this->assertSame($pass2, $passes[1]); - $this->assertSame($pass1, $passes[2]); + $this->assertSame($pass2, $passes[0]); + $this->assertSame($pass1, $passes[1]); } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_instanceof.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_instanceof.xml new file mode 100644 index 0000000000..8ba3ab7c64 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_instanceof.xml @@ -0,0 +1,12 @@ + + + + + set* + + + + + + + diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_instanceof.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_instanceof.yml new file mode 100644 index 0000000000..f0612c6d0a --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_instanceof.yml @@ -0,0 +1,10 @@ +services: + _instanceof: + Symfony\Component\DependencyInjection\Tests\Loader\FooInterface: + autowire: true + lazy: true + tags: + - { name: foo } + - { name: bar } + + Symfony\Component\DependencyInjection\Tests\Loader\Foo: ~ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php index f8f02c0eeb..01cc9d4ce7 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php @@ -719,4 +719,25 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array(null, 'ABCD'), $container->getDefinition(NamedArgumentsDummy::class)->getArguments()); $this->assertEquals(array(array('setApiKey', array('123'))), $container->getDefinition(NamedArgumentsDummy::class)->getMethodCalls()); } + + public function testInstanceof() + { + $container = new ContainerBuilder(); + $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')); + $loader->load('services_instanceof.xml'); + $container->compile(); + + $definition = $container->getDefinition(Bar::class); + $this->assertSame(array('__construct', 'set*'), $definition->getAutowiredCalls()); + $this->assertTrue($definition->isLazy()); + $this->assertSame(array('foo' => array(array()), 'bar' => array(array())), $definition->getTags()); + } +} + +interface BarInterface +{ +} + +class Bar implements BarInterface +{ } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php index ae56e9c223..da86c481b8 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php @@ -469,6 +469,19 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array(array('setApiKey', array('123'))), $container->getDefinition('another_one')->getMethodCalls()); } + public function testInstanceof() + { + $container = new ContainerBuilder(); + $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')); + $loader->load('services_instanceof.yml'); + $container->compile(); + + $definition = $container->getDefinition(Foo::class); + $this->assertTrue($definition->isAutowired()); + $this->assertTrue($definition->isLazy()); + $this->assertSame(array('foo' => array(array()), 'bar' => array(array())), $definition->getTags()); + } + /** * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException * @expectedExceptionMessage The value of the "decorates" option for the "bar" service must be the id of the service without the "@" prefix (replace "@foo" with "foo"). @@ -500,3 +513,11 @@ class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase $loader->load('services_underscore.yml'); } } + +interface FooInterface +{ +} + +class Foo implements FooInterface +{ +}