Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9f89907

Browse filesBrowse files
dunglasnicolas-grekas
authored andcommitted
[DependencyInjection] Tests + refacto for "instanceof" definitions
1 parent 3c29476 commit 9f89907
Copy full SHA for 9f89907

File tree

5 files changed

+84
-0
lines changed
Filter options

5 files changed

+84
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
+19Lines 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
*
+12Lines 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>
+10Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,4 +719,26 @@ 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+
{
743+
>>>>>>> [DependencyInjection] Tests + refacto for "instanceof" definitions
722744
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php
+21Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.