ContainerBuilderTest.php 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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\Tests\Component\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Alias;
  12. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Component\DependencyInjection\Definition;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  18. use Symfony\Component\Config\Resource\FileResource;
  19. require_once __DIR__.'/Fixtures/includes/classes.php';
  20. require_once __DIR__.'/Fixtures/includes/ProjectExtension.php';
  21. class ContainerBuilderTest extends \PHPUnit_Framework_TestCase
  22. {
  23. /**
  24. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::setDefinitions
  25. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getDefinitions
  26. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::setDefinition
  27. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getDefinition
  28. */
  29. public function testDefinitions()
  30. {
  31. $builder = new ContainerBuilder();
  32. $definitions = array(
  33. 'foo' => new Definition('FooClass'),
  34. 'bar' => new Definition('BarClass'),
  35. );
  36. $builder->setDefinitions($definitions);
  37. $this->assertEquals($definitions, $builder->getDefinitions(), '->setDefinitions() sets the service definitions');
  38. $this->assertTrue($builder->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists');
  39. $this->assertFalse($builder->hasDefinition('foobar'), '->hasDefinition() returns false if a service definition does not exist');
  40. $builder->setDefinition('foobar', $foo = new Definition('FooBarClass'));
  41. $this->assertEquals($foo, $builder->getDefinition('foobar'), '->getDefinition() returns a service definition if defined');
  42. $this->assertTrue($builder->setDefinition('foobar', $foo = new Definition('FooBarClass')) === $foo, '->setDefinition() implements a fluid interface by returning the service reference');
  43. $builder->addDefinitions($defs = array('foobar' => new Definition('FooBarClass')));
  44. $this->assertEquals(array_merge($definitions, $defs), $builder->getDefinitions(), '->addDefinitions() adds the service definitions');
  45. try {
  46. $builder->getDefinition('baz');
  47. $this->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
  48. } catch (\InvalidArgumentException $e) {
  49. $this->assertEquals('The service definition "baz" does not exist.', $e->getMessage(), '->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
  50. }
  51. }
  52. /**
  53. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::register
  54. */
  55. public function testRegister()
  56. {
  57. $builder = new ContainerBuilder();
  58. $builder->register('foo', 'FooClass');
  59. $this->assertTrue($builder->hasDefinition('foo'), '->register() registers a new service definition');
  60. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $builder->getDefinition('foo'), '->register() returns the newly created Definition instance');
  61. }
  62. /**
  63. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::has
  64. */
  65. public function testHas()
  66. {
  67. $builder = new ContainerBuilder();
  68. $this->assertFalse($builder->has('foo'), '->has() returns false if the service does not exist');
  69. $builder->register('foo', 'FooClass');
  70. $this->assertTrue($builder->has('foo'), '->has() returns true if a service definition exists');
  71. $builder->set('bar', new \stdClass());
  72. $this->assertTrue($builder->has('bar'), '->has() returns true if a service exists');
  73. }
  74. /**
  75. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::get
  76. */
  77. public function testGet()
  78. {
  79. $builder = new ContainerBuilder();
  80. try {
  81. $builder->get('foo');
  82. $this->fail('->get() throws an InvalidArgumentException if the service does not exist');
  83. } catch (\InvalidArgumentException $e) {
  84. $this->assertEquals('The service definition "foo" does not exist.', $e->getMessage(), '->get() throws an InvalidArgumentException if the service does not exist');
  85. }
  86. $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');
  87. $builder->register('foo', 'stdClass');
  88. $this->assertInternalType('object', $builder->get('foo'), '->get() returns the service definition associated with the id');
  89. $builder->set('bar', $bar = new \stdClass());
  90. $this->assertEquals($bar, $builder->get('bar'), '->get() returns the service associated with the id');
  91. $builder->register('bar', 'stdClass');
  92. $this->assertEquals($bar, $builder->get('bar'), '->get() returns the service associated with the id even if a definition has been defined');
  93. $builder->register('baz', 'stdClass')->setArguments(array(new Reference('baz')));
  94. try {
  95. @$builder->get('baz');
  96. $this->fail('->get() throws a ServiceCircularReferenceException if the service has a circular reference to itself');
  97. } catch (\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException $e) {
  98. $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');
  99. }
  100. $builder->register('foobar', 'stdClass')->setScope('container');
  101. $this->assertTrue($builder->get('bar') === $builder->get('bar'), '->get() always returns the same instance if the service is shared');
  102. }
  103. /**
  104. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getServiceIds
  105. */
  106. public function testGetServiceIds()
  107. {
  108. $builder = new ContainerBuilder();
  109. $builder->register('foo', 'stdClass');
  110. $builder->bar = $bar = new \stdClass();
  111. $builder->register('bar', 'stdClass');
  112. $this->assertEquals(array('foo', 'bar', 'service_container'), $builder->getServiceIds(), '->getServiceIds() returns all defined service ids');
  113. }
  114. /**
  115. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::setAlias
  116. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::hasAlias
  117. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getAlias
  118. */
  119. public function testAliases()
  120. {
  121. $builder = new ContainerBuilder();
  122. $builder->register('foo', 'stdClass');
  123. $builder->setAlias('bar', 'foo');
  124. $this->assertTrue($builder->hasAlias('bar'), '->hasAlias() returns true if the alias exists');
  125. $this->assertFalse($builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist');
  126. $this->assertEquals('foo', (string) $builder->getAlias('bar'), '->getAlias() returns the aliased service');
  127. $this->assertTrue($builder->has('bar'), '->setAlias() defines a new service');
  128. $this->assertTrue($builder->get('bar') === $builder->get('foo'), '->setAlias() creates a service that is an alias to another one');
  129. try {
  130. $builder->getAlias('foobar');
  131. $this->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist');
  132. } catch (\InvalidArgumentException $e) {
  133. $this->assertEquals('The service alias "foobar" does not exist.', $e->getMessage(), '->getAlias() throws an InvalidArgumentException if the alias does not exist');
  134. }
  135. }
  136. /**
  137. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getAliases
  138. */
  139. public function testGetAliases()
  140. {
  141. $builder = new ContainerBuilder();
  142. $builder->setAlias('bar', 'foo');
  143. $builder->setAlias('foobar', 'foo');
  144. $builder->setAlias('moo', new Alias('foo', false));
  145. $aliases = $builder->getAliases();
  146. $this->assertEquals('foo', (string) $aliases['bar']);
  147. $this->assertTrue($aliases['bar']->isPublic());
  148. $this->assertEquals('foo', (string) $aliases['foobar']);
  149. $this->assertEquals('foo', (string) $aliases['moo']);
  150. $this->assertFalse($aliases['moo']->isPublic());
  151. $builder->register('bar', 'stdClass');
  152. $this->assertFalse($builder->hasAlias('bar'));
  153. $builder->set('foobar', 'stdClass');
  154. $builder->set('moo', 'stdClass');
  155. $this->assertEquals(0, count($builder->getAliases()), '->getAliases() does not return aliased services that have been overridden');
  156. }
  157. /**
  158. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::setAliases
  159. */
  160. public function testSetAliases()
  161. {
  162. $builder = new ContainerBuilder();
  163. $builder->setAliases(array('bar' => 'foo', 'foobar' => 'foo'));
  164. $aliases = $builder->getAliases();
  165. $this->assertTrue(isset($aliases['bar']));
  166. $this->assertTrue(isset($aliases['foobar']));
  167. }
  168. /**
  169. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addAliases
  170. */
  171. public function testAddAliases()
  172. {
  173. $builder = new ContainerBuilder();
  174. $builder->setAliases(array('bar' => 'foo'));
  175. $builder->addAliases(array('foobar' => 'foo'));
  176. $aliases = $builder->getAliases();
  177. $this->assertTrue(isset($aliases['bar']));
  178. $this->assertTrue(isset($aliases['foobar']));
  179. }
  180. /**
  181. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addCompilerPass
  182. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getCompilerPassConfig
  183. */
  184. public function testAddGetCompilerPass()
  185. {
  186. $builder = new ContainerBuilder();
  187. $builderCompilerPasses = $builder->getCompiler()->getPassConfig()->getPasses();
  188. $builder->addCompilerPass($this->getMock('Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface'));
  189. $this->assertEquals(sizeof($builderCompilerPasses) + 1, sizeof($builder->getCompiler()->getPassConfig()->getPasses()));
  190. }
  191. /**
  192. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  193. */
  194. public function testCreateService()
  195. {
  196. $builder = new ContainerBuilder();
  197. $builder->register('foo1', 'FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
  198. $this->assertInstanceOf('\FooClass', $builder->get('foo1'), '->createService() requires the file defined by the service definition');
  199. $builder->register('foo2', 'FooClass')->setFile(__DIR__.'/Fixtures/includes/%file%.php');
  200. $builder->setParameter('file', 'foo');
  201. $this->assertInstanceOf('\FooClass', $builder->get('foo2'), '->createService() replaces parameters in the file provided by the service definition');
  202. }
  203. /**
  204. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  205. */
  206. public function testCreateServiceClass()
  207. {
  208. $builder = new ContainerBuilder();
  209. $builder->register('foo1', '%class%');
  210. $builder->setParameter('class', 'stdClass');
  211. $this->assertInstanceOf('\stdClass', $builder->get('foo1'), '->createService() replaces parameters in the class provided by the service definition');
  212. }
  213. /**
  214. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  215. */
  216. public function testCreateServiceArguments()
  217. {
  218. $builder = new ContainerBuilder();
  219. $builder->register('bar', 'stdClass');
  220. $builder->register('foo1', 'FooClass')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
  221. $builder->setParameter('value', 'bar');
  222. $this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar')), $builder->get('foo1')->arguments, '->createService() replaces parameters and service references in the arguments provided by the service definition');
  223. }
  224. /**
  225. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  226. */
  227. public function testCreateServiceFactoryMethod()
  228. {
  229. $builder = new ContainerBuilder();
  230. $builder->register('bar', 'stdClass');
  231. $builder->register('foo1', 'FooClass')->setFactoryClass('FooClass')->setFactoryMethod('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
  232. $builder->setParameter('value', 'bar');
  233. $this->assertTrue($builder->get('foo1')->called, '->createService() calls the factory method to create the service instance');
  234. $this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', $builder->get('bar')), $builder->get('foo1')->arguments, '->createService() passes the arguments to the factory method');
  235. }
  236. /**
  237. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  238. */
  239. public function testCreateServiceFactoryService()
  240. {
  241. $builder = new ContainerBuilder();
  242. $builder->register('baz_service')->setFactoryService('baz_factory')->setFactoryMethod('getInstance');
  243. $builder->register('baz_factory', 'BazClass');
  244. $this->assertInstanceOf('BazClass', $builder->get('baz_service'));
  245. }
  246. /**
  247. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  248. */
  249. public function testCreateServiceMethodCalls()
  250. {
  251. $builder = new ContainerBuilder();
  252. $builder->register('bar', 'stdClass');
  253. $builder->register('foo1', 'FooClass')->addMethodCall('setBar', array(array('%value%', new Reference('bar'))));
  254. $builder->setParameter('value', 'bar');
  255. $this->assertEquals(array('bar', $builder->get('bar')), $builder->get('foo1')->bar, '->createService() replaces the values in the method calls arguments');
  256. }
  257. /**
  258. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::createService
  259. */
  260. public function testCreateServiceConfigurator()
  261. {
  262. $builder = new ContainerBuilder();
  263. $builder->register('foo1', 'FooClass')->setConfigurator('sc_configure');
  264. $this->assertTrue($builder->get('foo1')->configured, '->createService() calls the configurator');
  265. $builder->register('foo2', 'FooClass')->setConfigurator(array('%class%', 'configureStatic'));
  266. $builder->setParameter('class', 'BazClass');
  267. $this->assertTrue($builder->get('foo2')->configured, '->createService() calls the configurator');
  268. $builder->register('baz', 'BazClass');
  269. $builder->register('foo3', 'FooClass')->setConfigurator(array(new Reference('baz'), 'configure'));
  270. $this->assertTrue($builder->get('foo3')->configured, '->createService() calls the configurator');
  271. $builder->register('foo4', 'FooClass')->setConfigurator('foo');
  272. try {
  273. $builder->get('foo4');
  274. $this->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
  275. } catch (\InvalidArgumentException $e) {
  276. $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');
  277. }
  278. }
  279. /**
  280. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::resolveServices
  281. */
  282. public function testResolveServices()
  283. {
  284. $builder = new ContainerBuilder();
  285. $builder->register('foo', 'FooClass');
  286. $this->assertEquals($builder->get('foo'), $builder->resolveServices(new Reference('foo')), '->resolveServices() resolves service references to service instances');
  287. $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');
  288. }
  289. /**
  290. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::merge
  291. */
  292. public function testMerge()
  293. {
  294. $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
  295. $config = new ContainerBuilder(new ParameterBag(array('foo' => 'bar')));
  296. $container->merge($config);
  297. $this->assertEquals(array('bar' => 'foo', 'foo' => 'bar'), $container->getParameterBag()->all(), '->merge() merges current parameters with the loaded ones');
  298. $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
  299. $config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%')));
  300. $container->merge($config);
  301. ////// FIXME
  302. $container->compile();
  303. $this->assertEquals(array('bar' => 'foo', 'foo' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');
  304. $container = new ContainerBuilder(new ParameterBag(array('bar' => 'foo')));
  305. $config = new ContainerBuilder(new ParameterBag(array('foo' => '%bar%', 'baz' => '%foo%')));
  306. $container->merge($config);
  307. ////// FIXME
  308. $container->compile();
  309. $this->assertEquals(array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');
  310. $container = new ContainerBuilder();
  311. $container->register('foo', 'FooClass');
  312. $container->register('bar', 'BarClass');
  313. $config = new ContainerBuilder();
  314. $config->setDefinition('baz', new Definition('BazClass'));
  315. $config->setAlias('alias_for_foo', 'foo');
  316. $container->merge($config);
  317. $this->assertEquals(array('foo', 'bar', 'baz'), array_keys($container->getDefinitions()), '->merge() merges definitions already defined ones');
  318. $aliases = $container->getAliases();
  319. $this->assertTrue(isset($aliases['alias_for_foo']));
  320. $this->assertEquals('foo', (string) $aliases['alias_for_foo']);
  321. $container = new ContainerBuilder();
  322. $container->register('foo', 'FooClass');
  323. $config->setDefinition('foo', new Definition('BazClass'));
  324. $container->merge($config);
  325. $this->assertEquals('BazClass', $container->getDefinition('foo')->getClass(), '->merge() overrides already defined services');
  326. }
  327. /**
  328. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::merge
  329. * @expectedException LogicException
  330. */
  331. public function testMergeLogicException()
  332. {
  333. $container = new ContainerBuilder();
  334. $container->compile();
  335. $container->merge(new ContainerBuilder());
  336. }
  337. /**
  338. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::findTaggedServiceIds
  339. */
  340. public function testfindTaggedServiceIds()
  341. {
  342. $builder = new ContainerBuilder();
  343. $builder
  344. ->register('foo', 'FooClass')
  345. ->addTag('foo', array('foo' => 'foo'))
  346. ->addTag('bar', array('bar' => 'bar'))
  347. ->addTag('foo', array('foofoo' => 'foofoo'))
  348. ;
  349. $this->assertEquals($builder->findTaggedServiceIds('foo'), array(
  350. 'foo' => array(
  351. array('foo' => 'foo'),
  352. array('foofoo' => 'foofoo'),
  353. )
  354. ), '->findTaggedServiceIds() returns an array of service ids and its tag attributes');
  355. $this->assertEquals(array(), $builder->findTaggedServiceIds('foobar'), '->findTaggedServiceIds() returns an empty array if there is annotated services');
  356. }
  357. /**
  358. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::findDefinition
  359. */
  360. public function testFindDefinition()
  361. {
  362. $container = new ContainerBuilder();
  363. $container->setDefinition('foo', $definition = new Definition('FooClass'));
  364. $container->setAlias('bar', 'foo');
  365. $container->setAlias('foobar', 'bar');
  366. $this->assertEquals($definition, $container->findDefinition('foobar'), '->findDefinition() returns a Definition');
  367. }
  368. /**
  369. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getResources
  370. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::addResource
  371. */
  372. public function testResources()
  373. {
  374. $container = new ContainerBuilder();
  375. $container->addResource($a = new FileResource(__DIR__.'/Fixtures/xml/services1.xml'));
  376. $container->addResource($b = new FileResource(__DIR__.'/Fixtures/xml/services2.xml'));
  377. $resources = array();
  378. foreach ($container->getResources() as $resource) {
  379. if (false === strpos($resource, '.php')) {
  380. $resources[] = $resource;
  381. }
  382. }
  383. $this->assertEquals(array($a, $b), $resources, '->getResources() returns an array of resources read for the current configuration');
  384. }
  385. /**
  386. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::registerExtension
  387. * @covers Symfony\Component\DependencyInjection\ContainerBuilder::getExtension
  388. */
  389. public function testExtension()
  390. {
  391. $container = new ContainerBuilder();
  392. $container->registerExtension($extension = new \ProjectExtension());
  393. $this->assertTrue($container->getExtension('project') === $extension, '->registerExtension() registers an extension');
  394. $this->setExpectedException('LogicException');
  395. $container->getExtension('no_registered');
  396. }
  397. public function testRegisteredButNotLoadedExtension()
  398. {
  399. $extension = $this->getMock('Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface');
  400. $extension->expects($this->once())->method('getAlias')->will($this->returnValue('project'));
  401. $extension->expects($this->never())->method('load');
  402. $container = new ContainerBuilder();
  403. $container->registerExtension($extension);
  404. $container->compile();
  405. }
  406. public function testRegisteredAndLoadedExtension()
  407. {
  408. $extension = $this->getMock('Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface');
  409. $extension->expects($this->exactly(2))->method('getAlias')->will($this->returnValue('project'));
  410. $extension->expects($this->once())->method('load')->with(array(array('foo' => 'bar')));
  411. $container = new ContainerBuilder();
  412. $container->registerExtension($extension);
  413. $container->loadFromExtension('project', array('foo' => 'bar'));
  414. $container->compile();
  415. }
  416. public function testPrivateServiceUser()
  417. {
  418. $fooDefinition = new Definition('BarClass');
  419. $fooUserDefinition = new Definition('BarUserClass', array(new Reference('bar')));
  420. $container = new ContainerBuilder();
  421. $fooDefinition->setPublic(false);
  422. $container->addDefinitions(array(
  423. 'bar' => $fooDefinition,
  424. 'bar_user' => $fooUserDefinition
  425. ));
  426. $container->compile();
  427. $this->assertInstanceOf('BarClass', $container->get('bar_user')->bar);
  428. }
  429. /**
  430. * @expectedException BadMethodCallException
  431. */
  432. public function testThrowsExceptionWhenSetServiceOnAFrozenContainer()
  433. {
  434. $container = new ContainerBuilder();
  435. $container->compile();
  436. $container->set('a', new \stdClass());
  437. }
  438. /**
  439. * @expectedException BadMethodCallException
  440. */
  441. public function testThrowsExceptionWhenSetDefinitionOnAFrozenContainer()
  442. {
  443. $container = new ContainerBuilder();
  444. $container->compile();
  445. $container->setDefinition('a', new Definition());
  446. }
  447. }
  448. class FooClass {}