PhpDumperTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Tests\Dumper;
  11. use DummyProxyDumper;
  12. use PHPUnit\Framework\TestCase;
  13. use Psr\Container\ContainerInterface;
  14. use Symfony\Component\Config\FileLocator;
  15. use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
  16. use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
  17. use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
  18. use Symfony\Component\DependencyInjection\ContainerBuilder;
  19. use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
  20. use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
  21. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  22. use Symfony\Component\DependencyInjection\Reference;
  23. use Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator;
  24. use Symfony\Component\DependencyInjection\TypedReference;
  25. use Symfony\Component\DependencyInjection\Definition;
  26. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  27. use Symfony\Component\DependencyInjection\ServiceLocator;
  28. use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber;
  29. use Symfony\Component\DependencyInjection\Variable;
  30. use Symfony\Component\ExpressionLanguage\Expression;
  31. require_once __DIR__.'/../Fixtures/includes/classes.php';
  32. class PhpDumperTest extends TestCase
  33. {
  34. protected static $fixturesPath;
  35. public static function setUpBeforeClass()
  36. {
  37. self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
  38. }
  39. public function testDump()
  40. {
  41. $container = new ContainerBuilder();
  42. $container->compile();
  43. $dumper = new PhpDumper($container);
  44. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services1.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  45. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services1-1.php', $dumper->dump(array('class' => 'Container', 'base_class' => 'AbstractContainer', 'namespace' => 'Symfony\Component\DependencyInjection\Dump')), '->dump() takes a class and a base_class options');
  46. }
  47. public function testDumpOptimizationString()
  48. {
  49. $definition = new Definition();
  50. $definition->setClass('stdClass');
  51. $definition->addArgument(array(
  52. 'only dot' => '.',
  53. 'concatenation as value' => '.\'\'.',
  54. 'concatenation from the start value' => '\'\'.',
  55. '.' => 'dot as a key',
  56. '.\'\'.' => 'concatenation as a key',
  57. '\'\'.' => 'concatenation from the start key',
  58. 'optimize concatenation' => 'string1%some_string%string2',
  59. 'optimize concatenation with empty string' => 'string1%empty_value%string2',
  60. 'optimize concatenation from the start' => '%empty_value%start',
  61. 'optimize concatenation at the end' => 'end%empty_value%',
  62. 'new line' => "string with \nnew line",
  63. ));
  64. $container = new ContainerBuilder();
  65. $container->setResourceTracking(false);
  66. $container->setDefinition('test', $definition);
  67. $container->setParameter('empty_value', '');
  68. $container->setParameter('some_string', '-');
  69. $container->compile();
  70. $dumper = new PhpDumper($container);
  71. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services10.php', $dumper->dump(), '->dump() dumps an empty container as an empty PHP class');
  72. }
  73. public function testDumpRelativeDir()
  74. {
  75. $definition = new Definition();
  76. $definition->setClass('stdClass');
  77. $definition->addArgument('%foo%');
  78. $definition->addArgument(array('%foo%' => '%buz%/'));
  79. $container = new ContainerBuilder();
  80. $container->setDefinition('test', $definition);
  81. $container->setParameter('foo', 'wiz'.dirname(__DIR__));
  82. $container->setParameter('bar', __DIR__);
  83. $container->setParameter('baz', '%bar%/PhpDumperTest.php');
  84. $container->setParameter('buz', dirname(dirname(__DIR__)));
  85. $container->compile();
  86. $dumper = new PhpDumper($container);
  87. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services12.php', $dumper->dump(array('file' => __FILE__)), '->dump() dumps __DIR__ relative strings');
  88. }
  89. /**
  90. * @dataProvider provideInvalidParameters
  91. * @expectedException \InvalidArgumentException
  92. */
  93. public function testExportParameters($parameters)
  94. {
  95. $container = new ContainerBuilder(new ParameterBag($parameters));
  96. $container->compile();
  97. $dumper = new PhpDumper($container);
  98. $dumper->dump();
  99. }
  100. public function provideInvalidParameters()
  101. {
  102. return array(
  103. array(array('foo' => new Definition('stdClass'))),
  104. array(array('foo' => new Expression('service("foo").foo() ~ (container.hasParameter("foo") ? parameter("foo") : "default")'))),
  105. array(array('foo' => new Reference('foo'))),
  106. array(array('foo' => new Variable('foo'))),
  107. );
  108. }
  109. public function testAddParameters()
  110. {
  111. $container = include self::$fixturesPath.'/containers/container8.php';
  112. $container->compile();
  113. $dumper = new PhpDumper($container);
  114. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services8.php', $dumper->dump(), '->dump() dumps parameters');
  115. }
  116. /**
  117. * @group legacy
  118. * @expectedDeprecation Dumping an uncompiled ContainerBuilder is deprecated since Symfony 3.3 and will not be supported anymore in 4.0. Compile the container beforehand.
  119. */
  120. public function testAddServiceWithoutCompilation()
  121. {
  122. $container = include self::$fixturesPath.'/containers/container9.php';
  123. $dumper = new PhpDumper($container);
  124. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services9.php', str_replace(str_replace('\\', '\\\\', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), '%path%', $dumper->dump()), '->dump() dumps services');
  125. }
  126. public function testAddService()
  127. {
  128. $container = include self::$fixturesPath.'/containers/container9.php';
  129. $container->compile();
  130. $dumper = new PhpDumper($container);
  131. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services9_compiled.php', str_replace(str_replace('\\', '\\\\', self::$fixturesPath.DIRECTORY_SEPARATOR.'includes'.DIRECTORY_SEPARATOR), '%path%', $dumper->dump()), '->dump() dumps services');
  132. $container = new ContainerBuilder();
  133. $container->register('foo', 'FooClass')->addArgument(new \stdClass());
  134. $container->compile();
  135. $dumper = new PhpDumper($container);
  136. try {
  137. $dumper->dump();
  138. $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  139. } catch (\Exception $e) {
  140. $this->assertInstanceOf('\Symfony\Component\DependencyInjection\Exception\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  141. $this->assertEquals('Unable to dump a service container if a parameter is an object or a resource.', $e->getMessage(), '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
  142. }
  143. }
  144. public function testServicesWithAnonymousFactories()
  145. {
  146. $container = include self::$fixturesPath.'/containers/container19.php';
  147. $container->compile();
  148. $dumper = new PhpDumper($container);
  149. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services19.php', $dumper->dump(), '->dump() dumps services with anonymous factories');
  150. }
  151. public function testAddServiceIdWithUnsupportedCharacters()
  152. {
  153. $class = 'Symfony_DI_PhpDumper_Test_Unsupported_Characters';
  154. $container = new ContainerBuilder();
  155. $container->register('bar$', 'FooClass');
  156. $container->register('bar$!', 'FooClass');
  157. $container->compile();
  158. $dumper = new PhpDumper($container);
  159. eval('?>'.$dumper->dump(array('class' => $class)));
  160. $this->assertTrue(method_exists($class, 'getBarService'));
  161. $this->assertTrue(method_exists($class, 'getBar2Service'));
  162. }
  163. public function testConflictingServiceIds()
  164. {
  165. $class = 'Symfony_DI_PhpDumper_Test_Conflicting_Service_Ids';
  166. $container = new ContainerBuilder();
  167. $container->register('foo_bar', 'FooClass');
  168. $container->register('foobar', 'FooClass');
  169. $container->compile();
  170. $dumper = new PhpDumper($container);
  171. eval('?>'.$dumper->dump(array('class' => $class)));
  172. $this->assertTrue(method_exists($class, 'getFooBarService'));
  173. $this->assertTrue(method_exists($class, 'getFoobar2Service'));
  174. }
  175. public function testConflictingMethodsWithParent()
  176. {
  177. $class = 'Symfony_DI_PhpDumper_Test_Conflicting_Method_With_Parent';
  178. $container = new ContainerBuilder();
  179. $container->register('bar', 'FooClass');
  180. $container->register('foo_bar', 'FooClass');
  181. $container->compile();
  182. $dumper = new PhpDumper($container);
  183. eval('?>'.$dumper->dump(array(
  184. 'class' => $class,
  185. 'base_class' => 'Symfony\Component\DependencyInjection\Tests\Fixtures\containers\CustomContainer',
  186. )));
  187. $this->assertTrue(method_exists($class, 'getBar2Service'));
  188. $this->assertTrue(method_exists($class, 'getFoobar2Service'));
  189. }
  190. /**
  191. * @dataProvider provideInvalidFactories
  192. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  193. * @expectedExceptionMessage Cannot dump definition
  194. */
  195. public function testInvalidFactories($factory)
  196. {
  197. $container = new ContainerBuilder();
  198. $def = new Definition('stdClass');
  199. $def->setFactory($factory);
  200. $container->setDefinition('bar', $def);
  201. $container->compile();
  202. $dumper = new PhpDumper($container);
  203. $dumper->dump();
  204. }
  205. public function provideInvalidFactories()
  206. {
  207. return array(
  208. array(array('', 'method')),
  209. array(array('class', '')),
  210. array(array('...', 'method')),
  211. array(array('class', '...')),
  212. );
  213. }
  214. public function testAliases()
  215. {
  216. $container = include self::$fixturesPath.'/containers/container9.php';
  217. $container->setParameter('foo_bar', 'foo_bar');
  218. $container->compile();
  219. $dumper = new PhpDumper($container);
  220. eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Aliases')));
  221. $container = new \Symfony_DI_PhpDumper_Test_Aliases();
  222. $foo = $container->get('foo');
  223. $this->assertSame($foo, $container->get('alias_for_foo'));
  224. $this->assertSame($foo, $container->get('alias_for_alias'));
  225. }
  226. public function testFrozenContainerWithoutAliases()
  227. {
  228. $container = new ContainerBuilder();
  229. $container->compile();
  230. $dumper = new PhpDumper($container);
  231. eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Frozen_No_Aliases')));
  232. $container = new \Symfony_DI_PhpDumper_Test_Frozen_No_Aliases();
  233. $this->assertFalse($container->has('foo'));
  234. }
  235. /**
  236. * @group legacy
  237. * @expectedDeprecation The "bar" service is already initialized, replacing it is deprecated since Symfony 3.3 and will fail in 4.0.
  238. */
  239. public function testOverrideServiceWhenUsingADumpedContainer()
  240. {
  241. require_once self::$fixturesPath.'/php/services9.php';
  242. require_once self::$fixturesPath.'/includes/foo.php';
  243. $container = new \ProjectServiceContainer();
  244. $container->setParameter('foo_bar', 'foo_bar');
  245. $container->get('bar');
  246. $container->set('bar', $bar = new \stdClass());
  247. $this->assertSame($bar, $container->get('bar'), '->set() overrides an already defined service');
  248. }
  249. /**
  250. * @group legacy
  251. * @expectedDeprecation The "bar" service is already initialized, replacing it is deprecated since Symfony 3.3 and will fail in 4.0.
  252. */
  253. public function testOverrideServiceWhenUsingADumpedContainerAndServiceIsUsedFromAnotherOne()
  254. {
  255. require_once self::$fixturesPath.'/php/services9.php';
  256. require_once self::$fixturesPath.'/includes/foo.php';
  257. require_once self::$fixturesPath.'/includes/classes.php';
  258. $container = new \ProjectServiceContainer();
  259. $container->setParameter('foo_bar', 'foo_bar');
  260. $container->get('bar');
  261. $container->set('bar', $bar = new \stdClass());
  262. $this->assertSame($bar, $container->get('foo')->bar, '->set() overrides an already defined service');
  263. }
  264. /**
  265. * @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
  266. */
  267. public function testCircularReference()
  268. {
  269. $container = new ContainerBuilder();
  270. $container->register('foo', 'stdClass')->addArgument(new Reference('bar'));
  271. $container->register('bar', 'stdClass')->setPublic(false)->addMethodCall('setA', array(new Reference('baz')));
  272. $container->register('baz', 'stdClass')->addMethodCall('setA', array(new Reference('foo')));
  273. $container->compile();
  274. $dumper = new PhpDumper($container);
  275. $dumper->dump();
  276. }
  277. public function testDumpAutowireData()
  278. {
  279. $container = include self::$fixturesPath.'/containers/container24.php';
  280. $container->compile();
  281. $dumper = new PhpDumper($container);
  282. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services24.php', $dumper->dump());
  283. }
  284. public function testEnvInId()
  285. {
  286. $container = include self::$fixturesPath.'/containers/container_env_in_id.php';
  287. $container->compile();
  288. $dumper = new PhpDumper($container);
  289. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_env_in_id.php', $dumper->dump());
  290. }
  291. public function testEnvParameter()
  292. {
  293. $container = new ContainerBuilder();
  294. $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
  295. $loader->load('services26.yml');
  296. $container->compile();
  297. $dumper = new PhpDumper($container);
  298. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services26.php', $dumper->dump(), '->dump() dumps inline definitions which reference service_container');
  299. }
  300. /**
  301. * @expectedException \Symfony\Component\DependencyInjection\Exception\EnvParameterException
  302. * @expectedExceptionMessage Environment variables "FOO" are never used. Please, check your container's configuration.
  303. */
  304. public function testUnusedEnvParameter()
  305. {
  306. $container = new ContainerBuilder();
  307. $container->getParameter('env(FOO)');
  308. $container->compile();
  309. $dumper = new PhpDumper($container);
  310. $dumper->dump();
  311. }
  312. public function testInlinedDefinitionReferencingServiceContainer()
  313. {
  314. $container = new ContainerBuilder();
  315. $container->register('foo', 'stdClass')->addMethodCall('add', array(new Reference('service_container')))->setPublic(false);
  316. $container->register('bar', 'stdClass')->addArgument(new Reference('foo'));
  317. $container->compile();
  318. $dumper = new PhpDumper($container);
  319. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services13.php', $dumper->dump(), '->dump() dumps inline definitions which reference service_container');
  320. }
  321. public function testInitializePropertiesBeforeMethodCalls()
  322. {
  323. require_once self::$fixturesPath.'/includes/classes.php';
  324. $container = new ContainerBuilder();
  325. $container->register('foo', 'stdClass');
  326. $container->register('bar', 'MethodCallClass')
  327. ->setProperty('simple', 'bar')
  328. ->setProperty('complex', new Reference('foo'))
  329. ->addMethodCall('callMe');
  330. $container->compile();
  331. $dumper = new PhpDumper($container);
  332. eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Properties_Before_Method_Calls')));
  333. $container = new \Symfony_DI_PhpDumper_Test_Properties_Before_Method_Calls();
  334. $this->assertTrue($container->get('bar')->callPassed(), '->dump() initializes properties before method calls');
  335. }
  336. public function testCircularReferenceAllowanceForLazyServices()
  337. {
  338. $container = new ContainerBuilder();
  339. $container->register('foo', 'stdClass')->addArgument(new Reference('bar'));
  340. $container->register('bar', 'stdClass')->setLazy(true)->addArgument(new Reference('foo'));
  341. $container->compile();
  342. $dumper = new PhpDumper($container);
  343. $dumper->dump();
  344. $this->addToAssertionCount(1);
  345. }
  346. public function testCircularReferenceAllowanceForInlinedDefinitionsForLazyServices()
  347. {
  348. /*
  349. * test graph:
  350. * [connection] -> [event_manager] --> [entity_manager](lazy)
  351. * |
  352. * --(call)- addEventListener ("@lazy_service")
  353. *
  354. * [lazy_service](lazy) -> [entity_manager](lazy)
  355. *
  356. */
  357. $container = new ContainerBuilder();
  358. $eventManagerDefinition = new Definition('stdClass');
  359. $connectionDefinition = $container->register('connection', 'stdClass');
  360. $connectionDefinition->addArgument($eventManagerDefinition);
  361. $container->register('entity_manager', 'stdClass')
  362. ->setLazy(true)
  363. ->addArgument(new Reference('connection'));
  364. $lazyServiceDefinition = $container->register('lazy_service', 'stdClass');
  365. $lazyServiceDefinition->setLazy(true);
  366. $lazyServiceDefinition->addArgument(new Reference('entity_manager'));
  367. $eventManagerDefinition->addMethodCall('addEventListener', array(new Reference('lazy_service')));
  368. $container->compile();
  369. $dumper = new PhpDumper($container);
  370. $dumper->setProxyDumper(new DummyProxyDumper());
  371. $dumper->dump();
  372. $this->addToAssertionCount(1);
  373. }
  374. public function testLazyArgumentProvideGenerator()
  375. {
  376. require_once self::$fixturesPath.'/includes/classes.php';
  377. $container = new ContainerBuilder();
  378. $container->register('lazy_referenced', 'stdClass');
  379. $container
  380. ->register('lazy_context', 'LazyContext')
  381. ->setArguments(array(
  382. new IteratorArgument(array('k1' => new Reference('lazy_referenced'), 'k2' => new Reference('service_container'))),
  383. new IteratorArgument(array()),
  384. ))
  385. ;
  386. $container->compile();
  387. $dumper = new PhpDumper($container);
  388. eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Lazy_Argument_Provide_Generator')));
  389. $container = new \Symfony_DI_PhpDumper_Test_Lazy_Argument_Provide_Generator();
  390. $lazyContext = $container->get('lazy_context');
  391. $this->assertInstanceOf(RewindableGenerator::class, $lazyContext->lazyValues);
  392. $this->assertInstanceOf(RewindableGenerator::class, $lazyContext->lazyEmptyValues);
  393. $this->assertCount(2, $lazyContext->lazyValues);
  394. $this->assertCount(0, $lazyContext->lazyEmptyValues);
  395. $i = -1;
  396. foreach ($lazyContext->lazyValues as $k => $v) {
  397. switch (++$i) {
  398. case 0:
  399. $this->assertEquals('k1', $k);
  400. $this->assertInstanceOf('stdCLass', $v);
  401. break;
  402. case 1:
  403. $this->assertEquals('k2', $k);
  404. $this->assertInstanceOf('Symfony_DI_PhpDumper_Test_Lazy_Argument_Provide_Generator', $v);
  405. break;
  406. }
  407. }
  408. $this->assertEmpty(iterator_to_array($lazyContext->lazyEmptyValues));
  409. }
  410. public function testNormalizedId()
  411. {
  412. $container = include self::$fixturesPath.'/containers/container33.php';
  413. $container->compile();
  414. $dumper = new PhpDumper($container);
  415. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services33.php', $dumper->dump());
  416. }
  417. public function testDumpContainerBuilderWithFrozenConstructorIncludingPrivateServices()
  418. {
  419. $container = new ContainerBuilder();
  420. $container->register('foo_service', 'stdClass')->setArguments(array(new Reference('baz_service')));
  421. $container->register('bar_service', 'stdClass')->setArguments(array(new Reference('baz_service')));
  422. $container->register('baz_service', 'stdClass')->setPublic(false);
  423. $container->compile();
  424. $dumper = new PhpDumper($container);
  425. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_private_frozen.php', $dumper->dump());
  426. }
  427. public function testServiceLocator()
  428. {
  429. $container = new ContainerBuilder();
  430. $container->register('foo_service', ServiceLocator::class)
  431. ->addArgument(array(
  432. 'bar' => new ServiceClosureArgument(new Reference('bar_service')),
  433. 'baz' => new ServiceClosureArgument(new TypedReference('baz_service', 'stdClass')),
  434. 'nil' => $nil = new ServiceClosureArgument(new Reference('nil')),
  435. ))
  436. ;
  437. // no method calls
  438. $container->register('translator.loader_1', 'stdClass');
  439. $container->register('translator.loader_1_locator', ServiceLocator::class)
  440. ->setPublic(false)
  441. ->addArgument(array(
  442. 'translator.loader_1' => new ServiceClosureArgument(new Reference('translator.loader_1')),
  443. ));
  444. $container->register('translator_1', StubbedTranslator::class)
  445. ->addArgument(new Reference('translator.loader_1_locator'));
  446. // one method calls
  447. $container->register('translator.loader_2', 'stdClass');
  448. $container->register('translator.loader_2_locator', ServiceLocator::class)
  449. ->setPublic(false)
  450. ->addArgument(array(
  451. 'translator.loader_2' => new ServiceClosureArgument(new Reference('translator.loader_2')),
  452. ));
  453. $container->register('translator_2', StubbedTranslator::class)
  454. ->addArgument(new Reference('translator.loader_2_locator'))
  455. ->addMethodCall('addResource', array('db', new Reference('translator.loader_2'), 'nl'));
  456. // two method calls
  457. $container->register('translator.loader_3', 'stdClass');
  458. $container->register('translator.loader_3_locator', ServiceLocator::class)
  459. ->setPublic(false)
  460. ->addArgument(array(
  461. 'translator.loader_3' => new ServiceClosureArgument(new Reference('translator.loader_3')),
  462. ));
  463. $container->register('translator_3', StubbedTranslator::class)
  464. ->addArgument(new Reference('translator.loader_3_locator'))
  465. ->addMethodCall('addResource', array('db', new Reference('translator.loader_3'), 'nl'))
  466. ->addMethodCall('addResource', array('db', new Reference('translator.loader_3'), 'en'));
  467. $nil->setValues(array(null));
  468. $container->register('bar_service', 'stdClass')->setArguments(array(new Reference('baz_service')));
  469. $container->register('baz_service', 'stdClass')->setPublic(false);
  470. $container->compile();
  471. $dumper = new PhpDumper($container);
  472. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_locator.php', $dumper->dump());
  473. }
  474. public function testServiceSubscriber()
  475. {
  476. $container = new ContainerBuilder();
  477. $container->register('foo_service', TestServiceSubscriber::class)
  478. ->setAutowired(true)
  479. ->addArgument(new Reference(ContainerInterface::class))
  480. ->addTag('container.service_subscriber', array(
  481. 'key' => 'bar',
  482. 'id' => TestServiceSubscriber::class,
  483. ))
  484. ;
  485. $container->register(TestServiceSubscriber::class, TestServiceSubscriber::class);
  486. $container->compile();
  487. $dumper = new PhpDumper($container);
  488. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_subscriber.php', $dumper->dump());
  489. }
  490. public function testPrivateWithIgnoreOnInvalidReference()
  491. {
  492. require_once self::$fixturesPath.'/includes/classes.php';
  493. $container = new ContainerBuilder();
  494. $container->register('not_invalid', 'BazClass')
  495. ->setPublic(false);
  496. $container->register('bar', 'BarClass')
  497. ->addMethodCall('setBaz', array(new Reference('not_invalid', SymfonyContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
  498. $container->compile();
  499. $dumper = new PhpDumper($container);
  500. eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Private_With_Ignore_On_Invalid_Reference')));
  501. $container = new \Symfony_DI_PhpDumper_Test_Private_With_Ignore_On_Invalid_Reference();
  502. $this->assertInstanceOf('BazClass', $container->get('bar')->getBaz());
  503. }
  504. public function testExpressionReferencingPrivateService()
  505. {
  506. $container = new ContainerBuilder();
  507. $container->register('private_bar', 'stdClass')
  508. ->setPublic(false);
  509. $container->register('private_foo', 'stdClass')
  510. ->setPublic(false);
  511. $container->register('public_foo', 'stdClass')
  512. ->addArgument(new Expression('service("private_foo")'));
  513. $container->compile();
  514. $dumper = new PhpDumper($container);
  515. $this->assertStringEqualsFile(self::$fixturesPath.'/php/services_private_in_expression.php', $dumper->dump());
  516. }
  517. public function testDumpHandlesLiteralClassWithRootNamespace()
  518. {
  519. $container = new ContainerBuilder();
  520. $container->register('foo', '\\stdClass');
  521. $container->compile();
  522. $dumper = new PhpDumper($container);
  523. eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Literal_Class_With_Root_Namespace')));
  524. $container = new \Symfony_DI_PhpDumper_Test_Literal_Class_With_Root_Namespace();
  525. $this->assertInstanceOf('stdClass', $container->get('foo'));
  526. }
  527. /**
  528. * This test checks the trigger of a deprecation note and should not be removed in major releases.
  529. *
  530. * @group legacy
  531. * @expectedDeprecation The "foo" service is deprecated. You should stop using it, as it will soon be removed.
  532. */
  533. public function testPrivateServiceTriggersDeprecation()
  534. {
  535. $container = new ContainerBuilder();
  536. $container->register('foo', 'stdClass')
  537. ->setPublic(false)
  538. ->setDeprecated(true);
  539. $container->register('bar', 'stdClass')
  540. ->setPublic(true)
  541. ->setProperty('foo', new Reference('foo'));
  542. $container->compile();
  543. $dumper = new PhpDumper($container);
  544. eval('?>'.$dumper->dump(array('class' => 'Symfony_DI_PhpDumper_Test_Private_Service_Triggers_Deprecation')));
  545. $container = new \Symfony_DI_PhpDumper_Test_Private_Service_Triggers_Deprecation();
  546. $container->get('bar');
  547. }
  548. }