8000 Adding an integration test for the hirarchy of defaults, instanceof, … · symfony/symfony@6d6116b · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d6116b

Browse files
weaverryannicolas-grekas
authored andcommitted
Adding an integration test for the hirarchy of defaults, instanceof, child, parent definitions
1 parent ab86457 commit 6d6116b

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

src/Symfony/Component/DependencyInjection/Compiler/ResolveInstanceofConditionalsPass.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function process(ContainerBuilder $container)
3030
$didProcess = false;
3131
foreach ($container->getDefinitions() as $id => $definition) {
3232
if ($definition instanceof ChildDefinition) {
33+
// don't apply "instanceof" to children: it will be applied to their parent
3334
continue;
3435
}
3536
if ($definition !== $processedDefinition = $this->processDefinition($container, $id, $definition)) {

src/Symfony/Component/DependencyInjection/Compiler/ResolveTagsInheritancePass.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

1414
use Symfony\Component\DependencyInjection\ChildDefinition;
15+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1516

1617
/**
1718
* Applies tags inheritance to definitions.
@@ -31,7 +32,7 @@ protected function processValue($value, $isRoot = false)
3132
$value->setInheritTags(false);
3233

3334
if (!$this->container->has($parent = $value->getParent())) {
34-
return parent::processValue($value, $isRoot);
35+
throw new RuntimeException(sprintf('Parent definition "%s" does not exist.', $parent));
3536
}
3637

3738
$parentDef = $this->container->findDefinition($parent);

src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Config\FileLocator;
1516
use Symfony\Component\DependencyInjection\Alias;
17+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
1618
use Symfony\Component\DependencyInjection\Reference;
1719
use Symfony\Component\DependencyInjection\ContainerBuilder;
1820

@@ -114,4 +116,79 @@ public function testProcessInlinesWhenThereAreMultipleReferencesButFromTheSameDe
114116
$this->assertFalse($container->hasDefinition('b'));
115117
$this->assertFalse($container->hasDefinition('c'), 'Service C was not inlined.');
116118
}
119+
120+
public function testInstanceofDefaultsAndParentDefinitionResolution()
121+
{
122+
$container = new ContainerBuilder();
123+
$container->setResourceTracking(false);
124+
125+
// loading YAML with an expressive test-case in that file
126+
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Fixtures/yaml'));
127+
$loader->load('services_defaults_instanceof_parent.yml');
128+
$container->compile();
129+
130+
// instanceof overrides defaults
131+
$simpleService = $container->getDefinition('service_simple');
132+
$this->assertFalse($simpleService->isAutowired());
133+
$this->assertFalse($simpleService->isShared());
134+
135+
// all tags are kept
136+
$this->assertEquals(
137+
array(
138+
'foo_tag' => array(array('priority' => 100), array()),
139+
'bar_tag' => array(array()),
140+
),
141+
$simpleService->getTags()
142+
);
143+
144+
// calls are all kept, but service-level calls are last
145+
$this->assertEquals(
146+
array(
147+
// from instanceof
148+
array('setSunshine', array('bright')),
149+
// from service
150+
array('enableSummer', array(true)),
151+
array('setSunshine', array('warm')),
152+
),
153+
$simpleService->getMethodCalls()
154+
);
155+
156+
// service override instanceof
157+
$overrideService = $container->getDefinition('service_override_instanceof');
158+
$this->assertTrue($overrideService->isAutowired());
159+
160+
// children definitions get no instanceof
161+
$childDef = $container->getDefinition('child_service');
162+
$this->assertEmpty($childDef->getTags());
163+
164+
$childDef2 = $container->getDefinition('child_service_with_parent_instanceof');
165+
// taken from instanceof applied to parent
166+
$this->assertFalse($childDef2->isAutowired());
167+
// override the instanceof
168+
$this->assertTrue($childDef2->isShared());
169+
// tags inherit like normal
170+
$this->assertEquals(
171+
array(
172+
'foo_tag' => array(array('priority' => 100), array()),
173+
'bar_tag' => array(array()),
174+
),
175+
$simpleService->getTags()
176+
);
177+
}
178+
}
179+
180+
class IntegrationTestStub extends IntegrationTestStubParent
181+
{
182+
}
183+
184+
class IntegrationTestStubParent
185+
{
186+
public function enableSummer($enable)
187+
{
188+
// methods used in calls - added here to prevent errors for not existing
189+
}
190+
191+
public function setSunshine($type)
192+
{
193+
}
117194
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
services:
2+
_defaults:
3+
autowire: true
4+
5+
_instanceof:
6+
Symfony\Component\DependencyInjection\Tests\Compiler\IntegrationTestStubParent:
7+
# should override _defaults
8+
autowire: false
9+
shared: false
10+
tags:
11+
- { name: foo_tag }
12+
calls:
13+
- [setSunshine, [bright]]
14+
15+
# a second instanceof that will be applied
16+
Symfony\Component\DependencyInjection\Tests\Compiler\IntegrationTestStub:
17+
tags:
18+
- { name: bar_tag }
19+
20+
service_simple:
21+
class: Symfony\Component\DependencyInjection\Tests\Compiler\IntegrationTestStub
22+
tags:
23+
- { name: foo_tag, priority: 100 }
24+
# calls from instanceof are kept, but this comes later
25+
calls:
26+
- [enableSummer, [true]]
27+
- [setSunshine, [warm]]
28+
29+
service_override_instanceof:
30+
class: Symfony\Component\DependencyInjection\Tests\Compiler\IntegrationTestStub
31+
# override instanceof
32+
autowire: true
33+
34+
parent_service:
35+
abstract: true
36+
lazy: true
37+
38+
# instanceof will not be applied to this
39+
child_service:
40+
class: Symfony\Component\DependencyInjection\Tests\Compiler\IntegrationTestStub
41+
parent: parent_service
42+
43+
parent_service_with_class:
44+
abstract: true
45+
class: Symfony\Component\DependencyInjection\Tests\Compiler\IntegrationTestStub
46+
47+
child_service_with_parent_instanceof:
48+
parent: parent_service_with_class
49+
shared: true

0 commit comments

Comments
 (0)
0