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 236cd01

Browse filesBrowse files
dunglasnicolas-grekas
authored andcommitted
[DependencyInjection] Tests + refacto for "instanceof" definitions
1 parent 5184336 commit 236cd01
Copy full SHA for 236cd01

File tree

6 files changed

+82
-2
lines changed
Filter options

6 files changed

+82
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,21 @@ 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+
// @deprecated condition, to be removed in 4.0
297+
298+
return !isset($underscoreParam['alias']) && !isset($underscoreParam['class']) && !isset($underscoreParam['factory']);
299+
}
300+
286301
/**
287302
* @param array $service
288303
*

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/PassConfigTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/PassConfigTest.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class PassConfigTest extends \PHPUnit_Framework_TestCase
2222
public function testPassOrdering()
2323
{
2424
$config = new PassConfig();
25+
$config->setBeforeOptimizationPasses(array());
2526

2627
$pass1 = $this->getMockBuilder(CompilerPassInterface::class)->getMock();
2728
$config->addPass($pass1, PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
@@ -30,7 +31,7 @@ public function testPassOrdering()
3031
$config->addPass($pass2, PassConfig::TYPE_BEFORE_OPTIMIZATION, 30);
3132

3233
$passes = $config->getBeforeOptimizationPasses();
33-
$this->assertSame($pass2, $passes[1]);
34-
$this->assertSame($pass1, $passes[2]);
34+
$this->assertSame($pass2, $passes[0]);
35+
$this->assertSame($pass1, $passes[1]);
3536
}
3637
}
+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
+21Lines 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

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").
@@ -500,3 +513,11 @@ public function testUnderscoreServiceId()
500513
$loader->load('services_underscore.yml');
501514
}
502515
}
516+
517+
interface FooInterface
518+
{
519+
}
520+
521+
class Foo implements FooInterface
522+
{
523+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.