ContainerTest.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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\Scope;
  12. use Symfony\Component\DependencyInjection\Container;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  15. class ContainerTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @covers Symfony\Component\DependencyInjection\Container::__construct
  19. */
  20. public function testConstructor()
  21. {
  22. $sc = new Container();
  23. $this->assertSame($sc, $sc->get('service_container'), '__construct() automatically registers itself as a service');
  24. $sc = new Container(new ParameterBag(array('foo' => 'bar')));
  25. $this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '__construct() takes an array of parameters as its first argument');
  26. }
  27. /**
  28. * @covers Symfony\Component\DependencyInjection\Container::compile
  29. */
  30. public function testCompile()
  31. {
  32. $sc = new Container(new ParameterBag(array('foo' => 'bar')));
  33. $sc->compile();
  34. $this->assertInstanceOf('Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag', $sc->getParameterBag(), '->compile() changes the parameter bag to a FrozenParameterBag instance');
  35. $this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '->compile() copies the current parameters to the new parameter bag');
  36. }
  37. /**
  38. * @covers Symfony\Component\DependencyInjection\Container::isFrozen
  39. */
  40. public function testIsFrozen()
  41. {
  42. $sc = new Container(new ParameterBag(array('foo' => 'bar')));
  43. $this->assertFalse($sc->isFrozen(), '->isFrozen() returns false if the parameters are not frozen');
  44. $sc->compile();
  45. $this->assertTrue($sc->isFrozen(), '->isFrozen() returns true if the parameters are frozen');
  46. }
  47. /**
  48. * @covers Symfony\Component\DependencyInjection\Container::getParameterBag
  49. */
  50. public function testGetParameterBag()
  51. {
  52. $sc = new Container();
  53. $this->assertEquals(array(), $sc->getParameterBag()->all(), '->getParameterBag() returns an empty array if no parameter has been defined');
  54. }
  55. /**
  56. * @covers Symfony\Component\DependencyInjection\Container::setParameter
  57. * @covers Symfony\Component\DependencyInjection\Container::getParameter
  58. */
  59. public function testGetSetParameter()
  60. {
  61. $sc = new Container(new ParameterBag(array('foo' => 'bar')));
  62. $sc->setParameter('bar', 'foo');
  63. $this->assertEquals('foo', $sc->getParameter('bar'), '->setParameter() sets the value of a new parameter');
  64. $sc->setParameter('foo', 'baz');
  65. $this->assertEquals('baz', $sc->getParameter('foo'), '->setParameter() overrides previously set parameter');
  66. $sc->setParameter('Foo', 'baz1');
  67. $this->assertEquals('baz1', $sc->getParameter('foo'), '->setParameter() converts the key to lowercase');
  68. $this->assertEquals('baz1', $sc->getParameter('FOO'), '->getParameter() converts the key to lowercase');
  69. try {
  70. $sc->getParameter('baba');
  71. $this->fail('->getParameter() thrown an \InvalidArgumentException if the key does not exist');
  72. } catch (\Exception $e) {
  73. $this->assertInstanceOf('\InvalidArgumentException', $e, '->getParameter() thrown an \InvalidArgumentException if the key does not exist');
  74. $this->assertEquals('You have requested a non-existent parameter "baba".', $e->getMessage(), '->getParameter() thrown an \InvalidArgumentException if the key does not exist');
  75. }
  76. }
  77. /**
  78. * @covers Symfony\Component\DependencyInjection\Container::getServiceIds
  79. */
  80. public function testGetServiceIds()
  81. {
  82. $sc = new Container();
  83. $sc->set('foo', $obj = new \stdClass());
  84. $sc->set('bar', $obj = new \stdClass());
  85. $this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids');
  86. $sc = new ProjectServiceContainer();
  87. $this->assertEquals(array('scoped', 'scoped_foo', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'service_container'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods');
  88. }
  89. /**
  90. * @covers Symfony\Component\DependencyInjection\Container::set
  91. */
  92. public function testSet()
  93. {
  94. $sc = new Container();
  95. $sc->set('foo', $foo = new \stdClass());
  96. $this->assertEquals($foo, $sc->get('foo'), '->set() sets a service');
  97. }
  98. /**
  99. * @expectedException \InvalidArgumentException
  100. */
  101. public function testSetDoesNotAllowPrototypeScope()
  102. {
  103. $c = new Container();
  104. $c->set('foo', new \stdClass(), 'prototype');
  105. }
  106. /**
  107. * @expectedException \RuntimeException
  108. */
  109. public function testSetDoesNotAllowInactiveScope()
  110. {
  111. $c = new Container();
  112. $c->addScope(new Scope('foo'));
  113. $c->set('foo', new \stdClass(), 'foo');
  114. }
  115. public function testSetAlsoSetsScopedService()
  116. {
  117. $c = new Container();
  118. $c->addScope(new Scope('foo'));
  119. $c->enterScope('foo');
  120. $c->set('foo', $foo = new \stdClass(), 'foo');
  121. $services = $this->getField($c, 'scopedServices');
  122. $this->assertTrue(isset($services['foo']['foo']));
  123. $this->assertSame($foo, $services['foo']['foo']);
  124. }
  125. /**
  126. * @covers Symfony\Component\DependencyInjection\Container::get
  127. */
  128. public function testGet()
  129. {
  130. $sc = new ProjectServiceContainer();
  131. $sc->set('foo', $foo = new \stdClass());
  132. $this->assertEquals($foo, $sc->get('foo'), '->get() returns the service for the given id');
  133. $this->assertEquals($sc->__bar, $sc->get('bar'), '->get() returns the service for the given id');
  134. $this->assertEquals($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined');
  135. $this->assertEquals($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined');
  136. $sc->set('bar', $bar = new \stdClass());
  137. $this->assertEquals($bar, $sc->get('bar'), '->get() prefers to return a service defined with set() than one defined with a getXXXMethod()');
  138. try {
  139. $sc->get('');
  140. $this->fail('->get() throws a \InvalidArgumentException exception if the service is empty');
  141. } catch (\Exception $e) {
  142. $this->assertInstanceOf('Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException', $e, '->get() throws a ServiceNotFoundException exception if the service is empty');
  143. }
  144. $this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE));
  145. }
  146. public function testGetCircularReference()
  147. {
  148. $sc = new ProjectServiceContainer();
  149. try {
  150. $sc->get('circular');
  151. $this->fail('->get() throws a ServiceCircularReferenceException if it contains circular reference');
  152. } catch (\Exception $e) {
  153. $this->assertInstanceOf('\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException', $e, '->get() throws a ServiceCircularReferenceException if it contains circular reference');
  154. $this->assertStringStartsWith('Circular reference detected for service "circular"', $e->getMessage(), '->get() throws a \LogicException if it contains circular reference');
  155. }
  156. }
  157. /**
  158. * @covers Symfony\Component\DependencyInjection\Container::has
  159. */
  160. public function testHas()
  161. {
  162. $sc = new ProjectServiceContainer();
  163. $sc->set('foo', new \stdClass());
  164. $this->assertFalse($sc->has('foo1'), '->has() returns false if the service does not exist');
  165. $this->assertTrue($sc->has('foo'), '->has() returns true if the service exists');
  166. $this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
  167. $this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
  168. $this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
  169. }
  170. public function testEnterLeaveCurrentScope()
  171. {
  172. $container = new ProjectServiceContainer();
  173. $container->addScope(new Scope('foo'));
  174. $container->enterScope('foo');
  175. $scoped1 = $container->get('scoped');
  176. $scopedFoo1 = $container->get('scoped_foo');
  177. $container->enterScope('foo');
  178. $scoped2 = $container->get('scoped');
  179. $scoped3 = $container->get('scoped');
  180. $scopedFoo2 = $container->get('scoped_foo');
  181. $container->leaveScope('foo');
  182. $scoped4 = $container->get('scoped');
  183. $scopedFoo3 = $container->get('scoped_foo');
  184. $this->assertNotSame($scoped1, $scoped2);
  185. $this->assertSame($scoped2, $scoped3);
  186. $this->assertSame($scoped1, $scoped4);
  187. $this->assertNotSame($scopedFoo1, $scopedFoo2);
  188. $this->assertSame($scopedFoo1, $scopedFoo3);
  189. }
  190. public function testEnterLeaveScopeWithChildScopes()
  191. {
  192. $container = new Container();
  193. $container->addScope(new Scope('foo'));
  194. $container->addScope(new Scope('bar', 'foo'));
  195. $this->assertFalse($container->isScopeActive('foo'));
  196. $container->enterScope('foo');
  197. $container->enterScope('bar');
  198. $this->assertTrue($container->isScopeActive('foo'));
  199. $this->assertFalse($container->has('a'));
  200. $a = new \stdClass();
  201. $container->set('a', $a, 'bar');
  202. $services = $this->getField($container, 'scopedServices');
  203. $this->assertTrue(isset($services['bar']['a']));
  204. $this->assertSame($a, $services['bar']['a']);
  205. $this->assertTrue($container->has('a'));
  206. $container->leaveScope('foo');
  207. $services = $this->getField($container, 'scopedServices');
  208. $this->assertFalse(isset($services['bar']));
  209. $this->assertFalse($container->isScopeActive('foo'));
  210. $this->assertFalse($container->has('a'));
  211. }
  212. public function testLeaveScopeNotActive()
  213. {
  214. $container = new Container();
  215. $container->addScope(new Scope('foo'));
  216. try {
  217. $container->leaveScope('foo');
  218. $this->fail('->leaveScope() throws a \LogicException if the scope is not active yet');
  219. } catch (\Exception $e) {
  220. $this->assertInstanceOf('\LogicException', $e, '->leaveScope() throws a \LogicException if the scope is not active yet');
  221. $this->assertEquals('The scope "foo" is not active.', $e->getMessage(), '->leaveScope() throws a \LogicException if the scope is not active yet');
  222. }
  223. try {
  224. $container->leaveScope('bar');
  225. $this->fail('->leaveScope() throws a \LogicException if the scope does not exist');
  226. } catch (\Exception $e) {
  227. $this->assertInstanceOf('\LogicException', $e, '->leaveScope() throws a \LogicException if the scope does not exist');
  228. $this->assertEquals('The scope "bar" is not active.', $e->getMessage(), '->leaveScope() throws a \LogicException if the scope does not exist');
  229. }
  230. }
  231. /**
  232. * @expectedException \InvalidArgumentException
  233. * @dataProvider getBuiltInScopes
  234. */
  235. public function testAddScopeDoesNotAllowBuiltInScopes($scope)
  236. {
  237. $container = new Container();
  238. $container->addScope(new Scope($scope));
  239. }
  240. /**
  241. * @expectedException \InvalidArgumentException
  242. */
  243. public function testAddScopeDoesNotAllowExistingScope()
  244. {
  245. $container = new Container();
  246. $container->addScope(new Scope('foo'));
  247. $container->addScope(new Scope('foo'));
  248. }
  249. /**
  250. * @expectedException \InvalidArgumentException
  251. * @dataProvider getInvalidParentScopes
  252. */
  253. public function testAddScopeDoesNotAllowInvalidParentScope($scope)
  254. {
  255. $c = new Container();
  256. $c->addScope(new Scope('foo', $scope));
  257. }
  258. public function testAddScope()
  259. {
  260. $c = new Container();
  261. $c->addScope(new Scope('foo'));
  262. $c->addScope(new Scope('bar', 'foo'));
  263. $this->assertSame(array('foo' => 'container', 'bar' => 'foo'), $this->getField($c, 'scopes'));
  264. $this->assertSame(array('foo' => array('bar'), 'bar' => array()), $this->getField($c, 'scopeChildren'));
  265. }
  266. public function testHasScope()
  267. {
  268. $c = new Container();
  269. $this->assertFalse($c->hasScope('foo'));
  270. $c->addScope(new Scope('foo'));
  271. $this->assertTrue($c->hasScope('foo'));
  272. }
  273. public function testIsScopeActive()
  274. {
  275. $c = new Container();
  276. $this->assertFalse($c->isScopeActive('foo'));
  277. $c->addScope(new Scope('foo'));
  278. $this->assertFalse($c->isScopeActive('foo'));
  279. $c->enterScope('foo');
  280. $this->assertTrue($c->isScopeActive('foo'));
  281. $c->leaveScope('foo');
  282. $this->assertFalse($c->isScopeActive('foo'));
  283. }
  284. public function testGetThrowsException()
  285. {
  286. $c = new ProjectServiceContainer();
  287. try {
  288. $c->get('throw_exception');
  289. $this->fail();
  290. } catch (\Exception $e) {
  291. $this->assertEquals('Something went terribly wrong!', $e->getMessage());
  292. }
  293. try {
  294. $c->get('throw_exception');
  295. $this->fail();
  296. } catch (\Exception $e) {
  297. $this->assertEquals('Something went terribly wrong!', $e->getMessage());
  298. }
  299. }
  300. public function getInvalidParentScopes()
  301. {
  302. return array(
  303. array(ContainerInterface::SCOPE_PROTOTYPE),
  304. array('bar'),
  305. );
  306. }
  307. public function getBuiltInScopes()
  308. {
  309. return array(
  310. array(ContainerInterface::SCOPE_CONTAINER),
  311. array(ContainerInterface::SCOPE_PROTOTYPE),
  312. );
  313. }
  314. protected function getField($obj, $field)
  315. {
  316. $reflection = new \ReflectionProperty($obj, $field);
  317. $reflection->setAccessible(true);
  318. return $reflection->getValue($obj);
  319. }
  320. }
  321. class ProjectServiceContainer extends Container
  322. {
  323. public $__bar, $__foo_bar, $__foo_baz;
  324. public function __construct()
  325. {
  326. parent::__construct();
  327. $this->__bar = new \stdClass();
  328. $this->__foo_bar = new \stdClass();
  329. $this->__foo_baz = new \stdClass();
  330. }
  331. protected function getScopedService()
  332. {
  333. if (!isset($this->scopedServices['foo'])) {
  334. throw new \RuntimeException('Invalid call');
  335. }
  336. return $this->services['scoped'] = $this->scopedServices['foo']['scoped'] = new \stdClass();
  337. }
  338. protected function getScopedFooService()
  339. {
  340. if (!isset($this->scopedServices['foo'])) {
  341. throw new \RuntimeException('invalid call');
  342. }
  343. return $this->services['scoped_foo'] = $this->scopedServices['foo']['scoped_foo'] = new \stdClass();
  344. }
  345. protected function getBarService()
  346. {
  347. return $this->__bar;
  348. }
  349. protected function getFooBarService()
  350. {
  351. return $this->__foo_bar;
  352. }
  353. protected function getFoo_BazService()
  354. {
  355. return $this->__foo_baz;
  356. }
  357. protected function getCircularService()
  358. {
  359. return $this->get('circular');
  360. }
  361. protected function getThrowExceptionService()
  362. {
  363. throw new \Exception('Something went terribly wrong!');
  364. }
  365. }