8000 [DependencyInjection] Tests + refacto for "instanceof" definitions · symfony/symfony@a06d87c · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit a06d87c

Browse files
dunglasnicolas-grekas
authored andcommitted
[DependencyInjection] Tests + refacto for "instanceof" definitions
1 parent 0f8a551 commit a06d87c

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,25 @@ private function parseDefaults(array &$content, $file)
283283
return $defaults;
284284
}
285285

286+
private function isUnderscoredParamValid($content, $name, $file)
287+
{
288+
if (!isset($content['services'][$name])) {
289+
return false;
290+
}
291+
292+
if (!is_array($underscoreParam = $content['services'][$name])) {
293+
throw new InvalidArgumentException(sprintf('Service "%s" key must be an array, "%s" given in "%s".', $name, gettype($underscoreParam), $file));
294+
}
295+
296+
if (isset($underscoreParam['alias']) || isset($underscoreParam['class']) || isset($underscoreParam['factory'])) {
297+
@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);
298+
299+
return false;
300+
}
301+
302+
return true;
303+
}
304+
286305
/**
287306
* @param array $service
288307
*
Lines changed: 12 additions & 0 deletions
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>
Lines changed: 10 additions & 0 deletions
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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,4 +719,25 @@ public function testNamedArguments()
719719
$this->assertEquals(array(null, 'ABCD'), $container->getDefinition(NamedArgumentsDummy::class)->getArguments());
720720
$this->assertEquals(array(array('setApiKey', array('123'))), $container->getDefinition(NamedArgumentsDummy::class)->getMethodCalls());
721721
}
722+
723+
public function testInstanceof()
724+
{
725+
$container = new ContainerBuilder();
726+
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
727+
$loader->load('services_instanceof.xml');
728+
$container->compile();
729+
730+
$definition = $container->getDefinition(Bar::class);
731+
$this->assertSame(array('__construct', 'set*'), $definition->getAutowiredCalls());
732+
$this->assertTrue($definition->isLazy());
733+
$this->assertSame(array('foo' => array(array()), 'bar' => array(array())), $definition->getTags());
734+
}
735+
}
736+
737+
interface BarInterface
738+
{
739+
}
740+
741+
class Bar implements BarInterface
742+
{
722743
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,19 @@ public function testNamedArguments()
469469
$this->assertEquals(array(array('setApiKey', array('123'))), $container->getDefinition('another_one')->getMethodCalls());
470470
}
471471

472+
public function testInstanceof()
473+
{
474+
$container = new ContainerBuilder();
475+
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
476+
$loader->load('services_instanceof.yml');
477+
$container->compile();
478+
479+
$definition = $container->getDefinition(Foo::class);
480+
$this->assertTrue($definition->isAutowired());
481+
$this->assertTrue($definition->isLazy());
482+
$this->assertSame(array('foo' => array(array()), 'bar' => array(array())), $definition->getTags());
483+
}
484+
472485
/**
473486
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
474487
* @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").
@@ -489,3 +502,11 @@ public function testInvalidTagsWithDefaults()
489502
$loader->load('services31_invalid_tags.yml');
490503
}
491504
}
505+
506+
interface FooInterface
507+
{
508+
}
509+
510+
class Foo implements FooInterface
511+
{
512+
}

0 commit comments

Comments
 (0)
0