ContainerBuilderTest.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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;
  11. require_once __DIR__.'/Fixtures/includes/classes.php';
  12. require_once __DIR__.'/Fixtures/includes/ProjectExtension.php';
  13. use Symfony\Component\Config\Resource\ResourceInterface;
  14. use Symfony\Component\DependencyInjection\Alias;
  15. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\ContainerInterface;
  18. use Symfony\Component\DependencyInjection\Definition;
  19. use Symfony\Component\DependencyInjection\Exception\RuntimeException;
  20. use Symfony\Component\DependencyInjection\Reference;
  21. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  22. use Symfony\Component\DependencyInjection\Scope;
  23. use Symfony\Component\Config\Resource\FileResource;
  24. class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
  25. {
  26. /**
  27. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::setDefinitions
  28. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getDefinitions
  29. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::setDefinition
  30. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getDefinition
  31. */
  32. public function testDefinitions()
  33. {
  34. $builder = new ContainerBuilder();
  35. $definitions = array(
  36. 'foo' => new Definition('FooClass'),
  37. 'bar' => new Definition('BarClass'),
  38. );
  39. $builder->setDefinitions($definitions);
  40. $this->assertEquals($definitions, $builder->getDefinitions(), '->setDefinitions() sets the service definitions');
  41. $this->assertTrue($builder->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists');
  42. $this->assertFalse($builder->hasDefinition('foobar'), '->hasDefinition() returns false if a service definition does not exist');
  43. $builder->setDefinition('foobar', $foo = new Definition('FooBarClass'));
  44. $this->assertEquals($foo, $builder->getDefinition('foobar'), '->getDefinition() returns a service definition if defined');
  45. $this->assertTrue($builder->setDefinition('foobar', $foo = new Definition('FooBarClass')) === $foo, '->setDefinition() implements a fluid interface by returning the service reference');
  46. $builder->addDefinitions($defs = array('foobar' => new Definition('FooBarClass')));
  47. $this->assertEquals(array_merge($definitions, $defs), $builder->getDefinitions(), '->addDefinitions() adds the service definitions');
  48. try {
  49. $builder->getDefinition('baz');
  50. $this->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
  51. } catch (\InvalidArgumentException $e) {
  52. $this->assertEquals('The service definition "baz" does not exist.', $e->getMessage(), '->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
  53. }
  54. }
  55. /**
  56. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::register
  57. */
  58. public function testRegister()
  59. {
  60. $builder = new ContainerBuilder();
  61. $builder->register('foo', 'FooClass');
  62. $this->assertTrue($builder->hasDefinition('foo'), '->register() registers a new service definition');
  63. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $builder->getDefinition('foo'), '->register() returns the newly created Definition instance');
  64. }
  65. /**
  66. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::has
  67. */
  68. public function testHas()
  69. {
  70. $builder = new ContainerBuilder();
  71. $this->assertFalse($builder->has('foo'), '->has() returns false if the service does not exist');
  72. $builder->register('foo', 'FooClass');
  73. $this->assertTrue($builder->has('foo'), '->has() returns true if a service definition exists');
  74. $builder->set('bar', new \stdClass());
  75. $this->assertTrue($builder->has('bar'), '->has() returns true if a service exists');
  76. }
  77. /**
  78. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::get
  79. */
  80. public function testGet()
  81. {
  82. $builder = new ContainerBuilder();
  83. try {
  84. $builder->get('foo');
  85. $this->fail('->get() throws an InvalidArgumentException if the service does not exist');
  86. } catch (\InvalidArgumentException $e) {
  87. $this->assertEquals('The service definition "foo" does not exist.', $e->getMessage(), '->get() throws an InvalidArgumentException if the service does not exist');
  88. }
  89. $this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE), '->get() returns null if the service does not exist and NULL_ON_INVALID_REFERENCE is passed as a second argument');
  90. $builder->register('foo', 'stdClass');
  91. $this->assertInternalType('object', $builder->get('foo'), '->get() returns the service definition associated with the id');
  92. $builder->set('bar', $bar = new \stdClass());
  93. $this->assertEquals($bar, $builder->get('bar'), '->get() returns the service associated with the id');
  94. $builder->register('bar', 'stdClass');
  95. $this->assertEquals($bar, $builder->get('bar'), '->get() returns the service associated with the id even if a definition has been defined');
  96. $builder->register('baz', 'stdClass')->setArguments(array(new Reference('baz')));
  97. try {
  98. @$builder->get('baz');
  99. $this->fail('->get() throws a ServiceCircularReferenceException if the service has a circular reference to itself');
  100. } catch (\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException $e) {
  101. $this->assertEquals('Circular reference detected for service "baz", path: "baz".', $e->getMessage(), '->get() throws a LogicException if the service has a circular reference to itself');
  102. }
  103. $builder->register('foobar', 'stdClass')->setScope('container');
  104. $this->assertTrue($builder->get('bar') === $builder->get('bar'), '->get() always returns the same instance if the service is shared');
  105. }
  106. /**
  107. * @covers \Symfony\Component\DependencyInjection\ContainerBuilder::get
  108. * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
  109. * @expectedExceptionMessage You have requested a synthetic service ("foo"). The DIC does not know how to construct this service.
  110. */
  111. public function testGetUnsetLoadingServiceWhenCreateServiceThrowsAnException()
  112. {
  113. $builder = new ContainerBuilder();
  114. $builder->register('foo', 'stdClass')->setSynthetic(true);
  115. // we expect a RuntimeException here as foo is synthetic
  116. try {
  117. $builder->get('foo');
  118. } catch (RuntimeException $e) {
  119. }
  120. // we must also have the same RuntimeException here
  121. $builder->get('foo');
  122. }
  123. /**
  124. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::get
  125. */
  126. public function testGetReturnsNullOnInactiveScope()
  127. {
  128. $builder = new ContainerBuilder();
  129. $builder->register('foo', 'stdClass')->setScope('request');
  130. $this->assertNull($builder->get('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE));
  131. }
  132. /**
  133. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getServiceIds
  134. */
  135. public function testGetServiceIds()
  136. {
  137. $builder = new ContainerBuilder();
  138. $builder->register('foo', 'stdClass');
  139. $builder->bar = $bar = new \stdClass();
  140. $builder->register('bar', 'stdClass');
  141. $this->assertEquals(array('foo', 'bar', 'service_container'), $builder->getServiceIds(), '->getServiceIds() returns all defined service ids');
  142. }
  143. /**
  144. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::setAlias
  145. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::hasAlias
  146. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getAlias
  147. */
  148. public function testAliases()
  149. {
  150. $builder = new ContainerBuilder();
  151. $builder->register('foo', 'stdClass');
  152. $builder->setAlias('bar', 'foo');
  153. $this->assertTrue($builder->hasAlias('bar'), '->hasAlias() returns true if the alias exists');
  154. $this->assertFalse($builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist');
  155. $this->assertEquals('foo', (string) $builder->getAlias('bar'), '->getAlias() returns the aliased service');
  156. $this->assertTrue($builder->has('bar'), '->setAlias() defines a new service');
  157. $this->assertTrue($builder->get('bar') === $builder->get('foo'), '->setAlias() creates a service that is an alias to another one');
  158. try {
  159. $builder->getAlias('foobar');
  160. $this->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist');
  161. } catch (\InvalidArgumentException $e) {
  162. $this->assertEquals('The service alias "foobar" does not exist.', $e->getMessage(), '->getAlias() throws an InvalidArgumentException if the alias does not exist');
  163. }
  164. }
  165. /**
  166. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getAliases
  167. */
  168. public function testGetAliases()
  169. {
  170. $builder = new ContainerBuilder();
  171. $builder->setAlias('bar', 'foo');
  172. $builder->setAlias('foobar', 'foo');
  173. $builder->setAlias('moo', new Alias('foo', false));
  174. $aliases = $builder->getAliases();
  175. $this->assertEquals('foo', (string) $aliases['bar']);
  176. $this->assertTrue($aliases['bar']->isPublic());
  177. $this->assertEquals('foo', (string) $aliases['foobar']);
  178. $this->assertEquals('foo', (string) $aliases['moo']);
  179. $this->assertFalse($aliases['moo']->isPublic());
  180. $builder->register('bar', 'stdClass');
  181. $this->assertFalse($builder->hasAlias('bar'));
  182. $builder->set('foobar', 'stdClass');
  183. $builder->set('moo', 'stdClass');
  184. $this->assertCount(0, $builder->getAliases(), '->getAliases() does not return aliased services that have been overridden');
  185. }
  186. /**
  187. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::setAliases
  188. */
  189. public function testSetAliases()
  190. {
  191. $builder = new ContainerBuilder();
  192. $builder->setAliases(array('bar' => 'foo', 'foobar' => 'foo'));
  193. $aliases = $builder->getAliases();
  194. $this->assertTrue(isset($aliases['bar']));
  195. $this->assertTrue(isset($aliases['foobar']));
  196. }
  197. /**
  198. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addAliases
  199. */
  200. public function testAddAliases()
  201. {
  202. $builder = new ContainerBuilder();
  203. $builder->setAliases(array('bar' => 'foo'));
  204. $builder->addAliases(array('foobar' => 'foo'));
  205. $aliases = $builder->getAliases();
  206. $this->assertTrue(isset($aliases['bar']));
  207. $this->assertTrue(isset($aliases['foobar']));
  208. }
  209. /**
  210. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addCompilerPass
  211. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getCompilerPassConfig
  212. */
  213. public function testAddGetCompilerPass()
  214. {
  215. $builder = new ContainerBuilder();
  216. $builder->setResourceTracking(false);
  217. $builderCompilerPasses = $builder->getCompiler()->getPassConfig()->getPasses();
  218. $builder->addCompilerPass($this->getMock('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface'));
  219. $this->assertEquals(sizeof($builderCompilerPasses) + 1, sizeof($builder->getCompiler()->getPassConfig()->getPasses()));
  220. }
  221. /**
  222. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  223. */
  224. public function testCreateService()
  225. {
  226. $builder = new ContainerBuilder();
  227. $builder->register('foo1', 'FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
  228. $this->assertInstanceOf('\FooClass', $builder->get('foo1'), '->createService() requires the file defined by the service definition');
  229. $builder->register('foo2', 'FooClass')->setFile(__DIR__.'/Fixtures/includes/%file%.php');
  230. $builder->setParameter('file', 'foo');
  231. $this->assertInstanceOf('\FooClass', $builder->get('foo2'), '->createService() replaces parameters in the file provided by the service definition');
  232. }
  233. /**
  234. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  235. */
  236. public function testCreateProxyWithRealServiceInstantiator()
  237. {
  238. $builder = new ContainerBuilder();
  239. $builder->register('foo1', 'FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
  240. $builder->getDefinition('foo1')->setLazy(true);
  241. $foo1 = $builder->get('foo1');
  242. $this->assertSame($foo1, $builder->get('foo1'), 'The same proxy is retrieved on multiple subsequent calls');
  243. $this->assertSame('FooClass', get_class($foo1));
  244. }
  245. /**
  246. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  247. */
  248. public function testCreateServiceClass()
  249. {
  250. $builder = new ContainerBuilder();
  251. $builder->register('foo1', '%class%');
  252. $builder->setParameter('class', 'stdClass');
  253. $this->assertInstanceOf('\stdClass', $builder->get('foo1'), '->createService() replaces parameters in the class provided by the service definition');
  254. }
  255. /**
  256. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  257. */
  258. public function testCreateServiceArguments()
  259. {
  260. $builder = new ContainerBuilder();
  261. $builder->register('bar', 'stdClass');
  262. $builder->register('foo1', 'FooClass')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar'), '%%unescape_it%%'));
  263. $builder->setParameter('value', 'bar');
  264. $this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar'), '%unescape_it%'), $builder->get('foo1')->arguments, '->createService() replaces parameters and service references in the arguments provided by the service definition');
  265. }
  266. /**
  267. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  268. */
  269. public function testCreateServiceFactoryMethod()
  270. {
  271. $builder = new ContainerBuilder();
  272. $builder->register('bar', 'stdClass');
  273. $builder->register('foo1', 'FooClass')->setFactoryClass('FooClass')->setFactoryMethod('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
  274. $builder->setParameter('value', 'bar');
  275. $this->assertTrue($builder->get('foo1')->called, '->createService() calls the factory method to create the service instance');
  276. $this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar')), $builder->get('foo1')->arguments, '->createService() passes the arguments to the factory method');
  277. }
  278. /**
  279. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  280. */
  281. public function testCreateServiceFactoryService()
  282. {
  283. $builder = new ContainerBuilder();
  284. $builder->register('baz_service')->setFactoryService('baz_factory')->setFactoryMethod('getInstance');
  285. $builder->register('baz_factory', 'BazClass');
  286. $this->assertInstanceOf('BazClass', $builder->get('baz_service'));
  287. }
  288. /**
  289. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  290. */
  291. public function testCreateServiceMethodCalls()
  292. {
  293. $builder = new ContainerBuilder();
  294. $builder->register('bar', 'stdClass');
  295. $builder->register('foo1', 'FooClass')->addMethodCall('setBar', array(array('%value%', new Reference('bar'))));
  296. $builder->setParameter('value', 'bar');
  297. $this->assertEquals(array('bar', $builder->get('bar')), $builder->get('foo1')->bar, '->createService() replaces the values in the method calls arguments');
  298. }
  299. /**
  300. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  301. */
  302. public function testCreateServiceConfigurator()
  303. {
  304. $builder = new ContainerBuilder();
  305. $builder->register('foo1', 'FooClass')->setConfigurator('sc_configure');
  306. $this->assertTrue($builder->get('foo1')->configured, '->createService() calls the configurator');
  307. $builder->register('foo2', 'FooClass')->setConfigurator(array('%class%', 'configureStatic'));
  308. $builder->setParameter('class', 'BazClass');
  309. $this->assertTrue($builder->get('foo2')->configured, '->createService() calls the configurator');
  310. $builder->register('baz', 'BazClass');
  311. $builder->register('foo3', 'FooClass')->setConfigurator(array(new Reference('baz'), 'configure'));
  312. $this->assertTrue($builder->get('foo3')->configured, '->createService() calls the configurator');
  313. $builder->register('foo4', 'FooClass')->setConfigurator('foo');
  314. try {
  315. $builder->get('foo4');
  316. $this->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
  317. } catch (\InvalidArgumentException $e) {
  318. $this->assertEquals('The configure callable for class "FooClass" is not a callable.', $e->getMessage(), '->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
  319. }
  320. }
  321. /**
  322. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  323. * @expectedException \RuntimeException
  324. */
  325. public function testCreateSyntheticService()
  326. {
  327. $builder = new ContainerBuilder();
  328. $builder->register('foo', 'FooClass')->setSynthetic(true);
  329. $builder->get('foo');
  330. }
  331. /**
  332. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::resolveServices
  333. */
  334. public function testResolveServices()
  335. {
  336. $builder = new ContainerBuilder();
  337. $builder->register('foo', 'FooClass');
  338. $this->assertEquals($builder->get('foo'), $builder->resolveServices(new Reference('foo')), '->resolveServices() resolves service references to service instances');
  339. $this->assertEquals(array('foo' => array('foo', $builder->get('foo'))), $builder->resolveServices(array('foo' => array('foo', new Reference('foo')))), '->resolveServices() resolves service references to service instances in nested arrays');
  340. }
  341. /**
  342. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::merge
  343. */
  344. public function testMerge()
  345. {
  346. $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
  347. $container->setResourceTracking(false);
  348. $config = new ContainerBuilder(new ParameterBag(array('foo' => 'bar')));
  349. $container->merge($config);
  350. $this->assertEquals(array('bar' => 'foo', 'foo' => 'bar'), $container->getParameterBag()->all(), '->merge() merges current parameters with the loaded ones');
  351. $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
  352. $container->setResourceTracking(false);
  353. $config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%')));
  354. $container->merge($config);
  355. ////// FIXME
  356. $container->compile();
  357. $this->assertEquals(array('bar' => 'foo', 'foo' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');
  358. $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
  359. $container->setResourceTracking(false);
  360. $config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%', 'baz' => '%foo%')));
  361. $container->merge($config);
  362. ////// FIXME
  363. $container->compile();
  364. $this->assertEquals(array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');
  365. $container = new ContainerBuilder();
  366. $container->setResourceTracking(false);
  367. $container->register('foo', 'FooClass');
  368. $container->register('bar', 'BarClass');
  369. $config = new ContainerBuilder();
  370. $config->setDefinition('baz', new Definition('BazClass'));
  371. $config->setAlias('alias_for_foo', 'foo');
  372. $container->merge($config);
  373. $this->assertEquals(array('foo', 'bar', 'baz'), array_keys($container->getDefinitions()), '->merge() merges definitions already defined ones');
  374. $aliases = $container->getAliases();
  375. $this->assertTrue(isset($aliases['alias_for_foo']));
  376. $this->assertEquals('foo', (string) $aliases['alias_for_foo']);
  377. $container = new ContainerBuilder();
  378. $container->setResourceTracking(false);
  379. $container->register('foo', 'FooClass');
  380. $config->setDefinition('foo', new Definition('BazClass'));
  381. $container->merge($config);
  382. $this->assertEquals('BazClass', $container->getDefinition('foo')->getClass(), '->merge() overrides already defined services');
  383. }
  384. /**
  385. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::merge
  386. * @expectedException LogicException
  387. */
  388. public function testMergeLogicException()
  389. {
  390. $container = new ContainerBuilder();
  391. $container->setResourceTracking(false);
  392. $container->compile();
  393. $container->merge(new ContainerBuilder());
  394. }
  395. /**
  396. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::findTaggedServiceIds
  397. */
  398. public function testfindTaggedServiceIds()
  399. {
  400. $builder = new ContainerBuilder();
  401. $builder
  402. ->register('foo', 'FooClass')
  403. ->addTag('foo', array('foo' => 'foo'))
  404. ->addTag('bar', array('bar' => 'bar'))
  405. ->addTag('foo', array('foofoo' => 'foofoo'))
  406. ;
  407. $this->assertEquals($builder->findTaggedServiceIds('foo'), array(
  408. 'foo' => array(
  409. array('foo' => 'foo'),
  410. array('foofoo' => 'foofoo'),
  411. )
  412. ), '->findTaggedServiceIds() returns an array of service ids and its tag attributes');
  413. $this->assertEquals(array(), $builder->findTaggedServiceIds('foobar'), '->findTaggedServiceIds() returns an empty array if there is annotated services');
  414. }
  415. /**
  416. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::findDefinition
  417. */
  418. public function testFindDefinition()
  419. {
  420. $container = new ContainerBuilder();
  421. $container->setDefinition('foo', $definition = new Definition('FooClass'));
  422. $container->setAlias('bar', 'foo');
  423. $container->setAlias('foobar', 'bar');
  424. $this->assertEquals($definition, $container->findDefinition('foobar'), '->findDefinition() returns a Definition');
  425. }
  426. /**
  427. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addObjectResource
  428. */
  429. public function testAddObjectResource()
  430. {
  431. if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
  432. $this->markTestSkipped('The "Config" component is not available');
  433. }
  434. $container = new ContainerBuilder();
  435. $container->setResourceTracking(false);
  436. $container->addObjectResource(new \BarClass());
  437. $this->assertEmpty($container->getResources(), 'No resources get registered without resource tracking');
  438. $container->setResourceTracking(true);
  439. $container->addObjectResource(new \BarClass());
  440. $resources = $container->getResources();
  441. $this->assertCount(1, $resources, '1 resource was registered');
  442. /* @var $resource \Symfony\Component\Config\Resource\FileResource */
  443. $resource = end($resources);
  444. $this->assertInstanceOf('Symfony\Component\Config\Resource\FileResource', $resource);
  445. $this->assertSame(realpath(__DIR__.'/Fixtures/includes/classes.php'), realpath($resource->getResource()));
  446. }
  447. /**
  448. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addClassResource
  449. */
  450. public function testAddClassResource()
  451. {
  452. if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
  453. $this->markTestSkipped('The "Config" component is not available');
  454. }
  455. $container = new ContainerBuilder();
  456. $container->setResourceTracking(false);
  457. $container->addClassResource(new \ReflectionClass('BarClass'));
  458. $this->assertEmpty($container->getResources(), 'No resources get registered without resource tracking');
  459. $container->setResourceTracking(true);
  460. $container->addClassResource(new \ReflectionClass('BarClass'));
  461. $resources = $container->getResources();
  462. $this->assertCount(1, $resources, '1 resource was registered');
  463. /* @var $resource \Symfony\Component\Config\Resource\FileResource */
  464. $resource = end($resources);
  465. $this->assertInstanceOf('Symfony\Component\Config\Resource\FileResource', $resource);
  466. $this->assertSame(realpath(__DIR__.'/Fixtures/includes/classes.php'), realpath($resource->getResource()));
  467. }
  468. /**
  469. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::compile
  470. */
  471. public function testCompilesClassDefinitionsOfLazyServices()
  472. {
  473. if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
  474. $this->markTestSkipped('The "Config" component is not available');
  475. }
  476. $container = new ContainerBuilder();
  477. $this->assertEmpty($container->getResources(), 'No resources get registered without resource tracking');
  478. $container->register('foo', 'BarClass');
  479. $container->getDefinition('foo')->setLazy(true);
  480. $container->compile();
  481. $classesPath = realpath(__DIR__.'/Fixtures/includes/classes.php');
  482. $matchingResources = array_filter(
  483. $container->getResources(),
  484. function (ResourceInterface $resource) use ($classesPath) {
  485. return $resource instanceof FileResource && $classesPath === realpath($resource->getResource());
  486. }
  487. );
  488. $this->assertNotEmpty($matchingResources);
  489. }
  490. /**
  491. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getResources
  492. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addResource
  493. */
  494. public function testResources()
  495. {
  496. if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
  497. $this->markTestSkipped('The "Config" component is not available');
  498. }
  499. $container = new ContainerBuilder();
  500. $container->addResource($a = new FileResource(__DIR__.'/Fixtures/xml/services1.xml'));
  501. $container->addResource($b = new FileResource(__DIR__.'/Fixtures/xml/services2.xml'));
  502. $resources = array();
  503. foreach ($container->getResources() as $resource) {
  504. if (false === strpos($resource, '.php')) {
  505. $resources[] = $resource;
  506. }
  507. }
  508. $this->assertEquals(array($a, $b), $resources, '->getResources() returns an array of resources read for the current configuration');
  509. $this->assertSame($container, $container->setResources(array()));
  510. $this->assertEquals(array(), $container->getResources());
  511. }
  512. /**
  513. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::registerExtension
  514. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getExtension
  515. */
  516. public function testExtension()
  517. {
  518. $container = new ContainerBuilder();
  519. $container->setResourceTracking(false);
  520. $container->registerExtension($extension = new \ProjectExtension());
  521. $this->assertTrue($container->getExtension('project') === $extension, '->registerExtension() registers an extension');
  522. $this->setExpectedException('LogicException');
  523. $container->getExtension('no_registered');
  524. }
  525. public function testRegisteredButNotLoadedExtension()
  526. {
  527. $extension = $this->getMock('Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface');
  528. $extension->expects($this->once())->method('getAlias')->will($this->returnValue('project'));
  529. $extension->expects($this->never())->method('load');
  530. $container = new ContainerBuilder();
  531. $container->setResourceTracking(false);
  532. $container->registerExtension($extension);
  533. $container->compile();
  534. }
  535. public function testRegisteredAndLoadedExtension()
  536. {
  537. $extension = $this->getMock('Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface');
  538. $extension->expects($this->exactly(2))->method('getAlias')->will($this->returnValue('project'));
  539. $extension->expects($this->once())->method('load')->with(array(array('foo' => 'bar')));
  540. $container = new ContainerBuilder();
  541. $container->setResourceTracking(false);
  542. $container->registerExtension($extension);
  543. $container->loadFromExtension('project', array('foo' => 'bar'));
  544. $container->compile();
  545. }
  546. public function testPrivateServiceUser()
  547. {
  548. $fooDefinition = new Definition('BarClass');
  549. $fooUserDefinition = new Definition('BarUserClass', array(new Reference('bar')));
  550. $container = new ContainerBuilder();
  551. $container->setResourceTracking(false);
  552. $fooDefinition->setPublic(false);
  553. $container->addDefinitions(array(
  554. 'bar' => $fooDefinition,
  555. 'bar_user' => $fooUserDefinition
  556. ));
  557. $container->compile();
  558. $this->assertInstanceOf('BarClass', $container->get('bar_user')->bar);
  559. }
  560. /**
  561. * @expectedException BadMethodCallException
  562. */
  563. public function testThrowsExceptionWhenSetServiceOnAFrozenContainer()
  564. {
  565. $container = new ContainerBuilder();
  566. $container->setResourceTracking(false);
  567. $container->setDefinition('a', new Definition('stdClass'));
  568. $container->compile();
  569. $container->set('a', new \stdClass());
  570. }
  571. /**
  572. * @expectedException BadMethodCallException
  573. */
  574. public function testThrowsExceptionWhenAddServiceOnAFrozenContainer()
  575. {
  576. if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
  577. $this->markTestSkipped('The "Config" component is not available');
  578. }
  579. $container = new ContainerBuilder();
  580. $container->compile();
  581. $container->set('a', new \stdClass());
  582. }
  583. public function testNoExceptionWhenSetSyntheticServiceOnAFrozenContainer()
  584. {
  585. if (!class_exists('Symfony\Component\Config\Resource\FileResource')) {
  586. $this->markTestSkipped('The "Config" component is not available');
  587. }
  588. $container = new ContainerBuilder();
  589. $def = new Definition('stdClass');
  590. $def->setSynthetic(true);
  591. $container->setDefinition('a', $def);
  592. $container->compile();
  593. $container->set('a', $a = new \stdClass());
  594. $this->assertEquals($a, $container->get('a'));
  595. }
  596. public function testSetOnSynchronizedService()
  597. {
  598. $container = new ContainerBuilder();
  599. $container->register('baz', 'BazClass')
  600. ->setSynchronized(true)
  601. ;
  602. $container->register('bar', 'BarClass')
  603. ->addMethodCall('setBaz', array(new Reference('baz')))
  604. ;
  605. $container->set('baz', $baz = new \BazClass());
  606. $this->assertSame($baz, $container->get('bar')->getBaz());
  607. $container->set('baz', $baz = new \BazClass());
  608. $this->assertSame($baz, $container->get('bar')->getBaz());
  609. }
  610. public function testSynchronizedServiceWithScopes()
  611. {
  612. $container = new ContainerBuilder();
  613. $container->addScope(new Scope('foo'));
  614. $container->register('baz', 'BazClass')
  615. ->setSynthetic(true)
  616. ->setSynchronized(true)
  617. ->setScope('foo')
  618. ;
  619. $container->register('bar', 'BarClass')
  620. ->addMethodCall('setBaz', array(new Reference('baz', ContainerInterface::NULL_ON_INVALID_REFERENCE, false)))
  621. ;
  622. $container->compile();
  623. $container->enterScope('foo');
  624. $container->set('baz', $outerBaz = new \BazClass(), 'foo');
  625. $this->assertSame($outerBaz, $container->get('bar')->getBaz());
  626. $container->enterScope('foo');
  627. $container->set('baz', $innerBaz = new \BazClass(), 'foo');
  628. $this->assertSame($innerBaz, $container->get('bar')->getBaz());
  629. $container->leaveScope('foo');
  630. $this->assertNotSame($innerBaz, $container->get('bar')->getBaz());
  631. $this->assertSame($outerBaz, $container->get('bar')->getBaz());
  632. $container->leaveScope('foo');
  633. }
  634. /**
  635. * @expectedException BadMethodCallException
  636. */
  637. public function testThrowsExceptionWhenSetDefinitionOnAFrozenContainer()
  638. {
  639. $container = new ContainerBuilder();
  640. $container->setResourceTracking(false);
  641. $container->compile();
  642. $container->setDefinition('a', new Definition());
  643. }
  644. /**
  645. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getExtensionConfig
  646. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::prependExtensionConfig
  647. */
  648. public function testExtensionConfig()
  649. {
  650. $container = new ContainerBuilder();
  651. $configs = $container->getExtensionConfig('foo');
  652. $this->assertEmpty($configs);
  653. $first = array('foo' => 'bar');
  654. $container->prependExtensionConfig('foo', $first);
  655. $configs = $container->getExtensionConfig('foo');
  656. $this->assertEquals(array($first), $configs);
  657. $second = array('ding' => 'dong');
  658. $container->prependExtensionConfig('foo', $second);
  659. $configs = $container->getExtensionConfig('foo');
  660. $this->assertEquals(array($second, $first), $configs);
  661. }
  662. }
  663. class FooClass {}