Skip to content

Commit 24d8139

Browse files
dunglasnicolas-grekas
authored andcommitted
[DependencyInjection] Tests + refacto for "instanceof" definitions
1 parent 74112a0 commit 24d8139

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

+19
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,25 @@ private function parseDefaults(array &$content, $file)
256256
return $defaults;
257257
}
258258

259+
private function isUnderscoredParamValid($content, $name, $file)
260+
{
261+
if (!isset($content['services'][$name])) {
262+
return false;
263+
}
264+
265+
if (!is_array($underscoreParam = $content['services'][$name])) {
266+
throw new InvalidArgumentException(sprintf('Service "%s" key must be an array, "%s" given in "%s".', $name, gettype($underscoreParam), $file));
267+
}
268+
269+
if (isset($underscoreParam['alias']) || isset($underscoreParam['class']) || isset($underscoreParam['factory'])) {
270+
@trigger_error(sprintf('Giving a service the "%s" name is deprecated since Symfony 3.3 and will be forbidden in 4.0. Rename your service.', $name), E_USER_DEPRECATED);
271+
272+
return false;
273+
}
274+
275+
return true;
276+
}
277+
259278
/**
260279
* Parses a definition.
261280
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<instanceof id="Symfony\Component\DependencyInjection\Tests\Loader\BarInterface" lazy="true">
5+
<autowire>set*</autowire>
6+
<tag name="foo" />
7+
<tag name="bar" />
8+
</instanceof>
9+
10+
<service id="Symfony\Component\DependencyInjection\Tests\Loader\Bar" class="Symfony\Component\DependencyInjection\Tests\Loader\Bar" autowire="true" />
11+
</services>
12+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
services:
2+
_instanceof:
3+
Symfony\Component\DependencyInjection\Tests\Loader\FooInterface:
4+
autowire: true
5+
lazy: true
6+
tags:
7+
- { name: foo }
8+
- { name: bar }
9+
10+
Symfony\Component\DependencyInjection\Tests\Loader\Foo: ~

src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -670,4 +670,25 @@ public function testDefaultsWithAutowiredCalls()
670670
$this->assertSame(array('setFoo'), $container->getDefinition('no_defaults_child')->getAutowiredCalls());
671671
$this->assertSame(array(), $container->getDefinition('with_defaults_child')->getAutowiredCalls());
672672
}
673+
674+
public function testInstanceof()
675+
{
676+
$container = new ContainerBuilder();
677+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
678+
$loader->load('services_instanceof.xml');
679+
$container->compile();
680+
681+
$definition = $container->getDefinition(Bar::class);
682+
$this->assertSame(array('__construct', 'set*'), $definition->getAutowiredCalls());
683+
$this->assertTrue($definition->isLazy());
684+
$this->assertSame(array('foo' => array(array()), 'bar' => array(array())), $definition->getTags());
685+
}
686+
}
687+
688+
interface BarInterface
689+
{
690+
}
691+
692+
class Bar implements BarInterface
693+
{
673694
}

src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,19 @@ public function testGetter()
417417
$this->assertEquals(array('getbar' => array('bar' => new Reference('bar'))), $container->getDefinition('foo')->getOverriddenGetters());
418418
}
419419

420+
public function testInstanceof()
421+
{
422+
$container = new ContainerBuilder();
423+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
424+
$loader->load('services_instanceof.yml');
425+
$container->compile();
426+
427+
$definition = $container->getDefinition(Foo::class);
428+
$this->assertTrue($definition->isAutowired());
429+
$this->assertTrue($definition->isLazy());
430+
$this->assertSame(array('foo' => array(array()), 'bar' => array(array())), $definition->getTags());
431+
}
432+
420433
/**
421434
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
422435
* @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").
@@ -437,3 +450,11 @@ public function testInvalidTagsWithDefaults()
437450
$loader->load('services31_invalid_tags.yml');
438451
}
439452
}
453+
454+
interface FooInterface
455+
{
456+
}
457+
458+
class Foo implements FooInterface
459+
{
460+
}

0 commit comments

Comments
 (0)