Skip to content

Commit f81c577

Browse files
Iltar van der Bergnicolas-grekas
authored andcommitted
[DI] Test references inside ServiceLocator are not inlined
1 parent 8783602 commit f81c577

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
2323
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2424
use Symfony\Component\DependencyInjection\Reference;
25+
use Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator;
2526
use Symfony\Component\DependencyInjection\TypedReference;
2627
use Symfony\Component\DependencyInjection\Definition;
2728
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
@@ -534,6 +535,40 @@ public function testServiceLocator()
534535
'nil' => $nil = new ServiceClosureArgument(new Reference('nil')),
535536
))
536537
;
538+
539+
// no method calls
540+
$container->register('translator.loader_1', 'stdClass');
541+
$container->register('translator.loader_1_locator', ServiceLocator::class)
542+
->setPublic(false)
543+
->addArgument(array(
544+
'translator.loader_1' => new ServiceClosureArgument(new Reference('translator.loader_1')),
545+
));
546+
$container->register('translator_1', StubbedTranslator::class)
547+
->addArgument(new Reference('translator.loader_1_locator'));
548+
549+
// one method calls
550+
$container->register('translator.loader_2', 'stdClass');
551+
$container->register('translator.loader_2_locator', ServiceLocator::class)
552+
->setPublic(false)
553+
->addArgument(array(
554+
'translator.loader_2' => new ServiceClosureArgument(new Reference('translator.loader_2')),
555+
));
556+
$container->register('translator_2', StubbedTranslator::class)
557+
->addArgument(new Reference('translator.loader_2_locator'))
558+
->addMethodCall('addResource', array('db', new Reference('translator.loader_2'), 'nl'));
559+
560+
// two method calls
561+
$container->register('translator.loader_3', 'stdClass');
562+
$container->register('translator.loader_3_locator', ServiceLocator::class)
563+
->setPublic(false)
564+
->addArgument(array(
565+
'translator.loader_3' => new ServiceClosureArgument(new Reference('translator.loader_3')),
566+
));
567+
$container->register('translator_3', StubbedTranslator::class)
568+
->addArgument(new Reference('translator.loader_3_locator'))
569+
->addMethodCall('addResource', array('db', new Reference('translator.loader_3'), 'nl'))
570+
->addMethodCall('addResource', array('db', new Reference('translator.loader_3'), 'en'));
571+
537572
$nil->setValues(array(null));
538573
$container->register('bar_service', 'stdClass')->setArguments(array(new Reference('baz_service')));
539574
$container->register('baz_service', 'stdClass')->setPublic(false);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
13+
14+
use Psr\Container\ContainerInterface;
15+
16+
/**
17+
* @author Iltar van der Berg <[email protected]>
18+
*/
19+
class StubbedTranslator
20+
{
21+
public function __construct(ContainerInterface $container)
22+
{
23+
24+
}
25+
26+
public function addResource($format, $resource, $locale, $domain = null)
27+
{
28+
}
29+
}

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_locator.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public function __construct()
3131
'bar_service' => 'getBarServiceService',
3232
'baz_service' => 'getBazServiceService',
3333
'foo_service' => 'getFooServiceService',
34+
'translator.loader_1' => 'getTranslator_Loader1Service',
35+
'translator.loader_2' => 'getTranslator_Loader2Service',
36+
'translator.loader_3' => 'getTranslator_Loader3Service',
37+
'translator_1' => 'getTranslator1Service',
38+
'translator_2' => 'getTranslator2Service',
39+
'translator_3' => 'getTranslator3Service',
3440
);
3541
$this->privates = array(
3642
'baz_service' => true,
@@ -97,6 +103,101 @@ protected function getFooServiceService()
97103
}));
98104
}
99105

106+
/**
107+
* Gets the 'translator.loader_1' service.
108+
*
109+
* This service is shared.
110+
* This method always returns the same instance of the service.
111+
*
112+
* @return \stdClass A stdClass instance
113+
*/
114+
protected function getTranslator_Loader1Service()
115+
{
116+
return $this->services['translator.loader_1'] = new \stdClass();
117+
}
118+
119+
/**
120+
* Gets the 'translator.loader_2' service.
121+
*
122+
* This service is shared.
123+
* This method always returns the same instance of the service.
124+
*
125+
* @return \stdClass A stdClass instance
126+
*/
127+
protected function getTranslator_Loader2Service()
128+
{
129+
return $this->services['translator.loader_2'] = new \stdClass();
130+
}
131+
132+
/**
133+
* Gets the 'translator.loader_3' service.
134+
*
135+
* This service is shared.
136+
* This method always returns the same instance of the service.
137+
*
138+
* @return \stdClass A stdClass instance
139+
*/
140+
protected function getTranslator_Loader3Service()
141+
{
142+
return $this->services['translator.loader_3'] = new \stdClass();
143+
}
144+
145+
/**
146+
* Gets the 'translator_1' service.
147+
*
148+
* This service is shared.
149+
* This method always returns the same instance of the service.
150+
*
151+
* @return \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator A Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator instance
152+
*/
153+
protected function getTranslator1Service()
154+
{
155+
return $this->services['translator_1'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(array('translator.loader_1' => function () {
156+
return ${($_ = isset($this->services['translator.loader_1']) ? $this->services['translator.loader_1'] : $this->get('translator.loader_1')) && false ?: '_'};
157+
})));
158+
}
159+
160+
/**
161+
* Gets the 'translator_2' service.
162+
*
163+
* This service is shared.
164+
* This method always returns the same instance of the service.
165+
*
166+
* @return \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator A Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator instance
167+
*/
168+
protected function getTranslator2Service()
169+
{
170+
$this->services['translator_2'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(array('translator.loader_2' => function () {
171+
return ${($_ = isset($this->services['translator.loader_2']) ? $this->services['translator.loader_2'] : $this->get('translator.loader_2')) && false ?: '_'};
172+
})));
173+
174+
$instance->addResource('db', ${($_ = isset($this->services['translator.loader_2']) ? $this->services['translator.loader_2'] : $this->get('translator.loader_2')) && false ?: '_'}, 'nl');
175+
176+
return $instance;
177+
}
178+
179+
/**
180+
* Gets the 'translator_3' service.
181+
*
182+
* This service is shared.
183+
* This method always returns the same instance of the service.
184+
*
185+
* @return \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator A Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator instance
186+
*/
187+
protected function getTranslator3Service()
188+
{
189+
$a = ${($_ = isset($this->services['translator.loader_3']) ? $this->services['translator.loader_3'] : $this->get('translator.loader_3')) && false ?: '_'};
190+
191+
$this->services['translator_3'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(array('translator.loader_3' => function () {
192+
return ${($_ = isset($this->services['translator.loader_3']) ? $this->services['translator.loader_3'] : $this->get('translator.loader_3')) && false ?: '_'};
193+
})));
194+
195+
$instance->addResource('db', $a, 'nl');
196+
$instance->addResource('db', $a, 'en');
197+
198+
return $instance;
199+
}
200+
100201
/**
101202
* Gets the 'baz_service' service.
102203
*

0 commit comments

Comments
 (0)