ContainerTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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('The parameter "baba" must be defined.', $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', '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('\InvalidArgumentException', $e, '->get() throws a \InvalidArgumentException exception if the service is empty');
  143. $this->assertEquals('The service "" does not exist.', $e->getMessage(), '->get() throws a \InvalidArgumentException exception if the service is empty');
  144. }
  145. $this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE));
  146. }
  147. public function testGetCircularReference()
  148. {
  149. $sc = new ProjectServiceContainer();
  150. try {
  151. $sc->get('circular');
  152. $this->fail('->get() throws a \LogicException if it contains circular reference');
  153. } catch (\Exception $e) {
  154. $this->assertInstanceOf('\LogicException', $e, '->get() throws a \LogicException if it contains circular reference');
  155. $this->assertStringStartsWith('Circular reference detected for service "circular"', $e->getMessage(), '->get() throws a \LogicException if it contains circular reference');
  156. }
  157. }
  158. /**
  159. * @covers Symfony\Component\DependencyInjection\Container::has
  160. */
  161. public function testHas()
  162. {
  163. $sc = new ProjectServiceContainer();
  164. $sc->set('foo', new \stdClass());
  165. $this->assertFalse($sc->has('foo1'), '->has() returns false if the service does not exist');
  166. $this->assertTrue($sc->has('foo'), '->has() returns true if the service exists');
  167. $this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
  168. $this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
  169. $this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
  170. }
  171. public function testEnterLeaveCurrentScope()
  172. {
  173. $container = new ProjectServiceContainer();
  174. $container->addScope(new Scope('foo'));
  175. $container->enterScope('foo');
  176. $scoped1 = $container->get('scoped');
  177. $scopedFoo1 = $container->get('scoped_foo');
  178. $container->enterScope('foo');
  179. $scoped2 = $container->get('scoped');
  180. $scoped3 = $container->get('scoped');
  181. $scopedFoo2 = $container->get('scoped_foo');
  182. $container->leaveScope('foo');
  183. $scoped4 = $container->get('scoped');
  184. $scopedFoo3 = $container->get('scoped_foo');
  185. $this->assertNotSame($scoped1, $scoped2);
  186. $this->assertSame($scoped2, $scoped3);
  187. $this->assertSame($scoped1, $scoped4);
  188. $this->assertNotSame($scopedFoo1, $scopedFoo2);
  189. $this->assertSame($scopedFoo1, $scopedFoo3);
  190. }
  191. public function testEnterLeaveScopeWithChildScopes()
  192. {
  193. $container = new Container();
  194. $container->addScope(new Scope('foo'));
  195. $container->addScope(new Scope('bar', 'foo'));
  196. $this->assertFalse($container->isScopeActive('foo'));
  197. $container->enterScope('foo');
  198. $container->enterScope('bar');
  199. $this->assertTrue($container->isScopeActive('foo'));
  200. $this->assertFalse($container->has('a'));
  201. $a = new \stdClass();
  202. $container->set('a', $a, 'bar');
  203. $services = $this->getField($container, 'scopedServices');
  204. $this->assertTrue(isset($services['bar']['a']));
  205. $this->assertSame($a, $services['bar']['a']);
  206. $this->assertTrue($container->has('a'));
  207. $container->leaveScope('foo');
  208. $services = $this->getField($container, 'scopedServices');
  209. $this->assertFalse(isset($services['bar']));
  210. $this->assertFalse($container->isScopeActive('foo'));
  211. $this->assertFalse($container->has('a'));
  212. }
  213. public function testLeaveScopeNotActive()
  214. {
  215. $container = new Container();
  216. $container->addScope(new Scope('foo'));
  217. try {
  218. $container->leaveScope('foo');
  219. $this->fail('->leaveScope() throws a \LogicException if the scope is not active yet');
  220. } catch (\Exception $e) {
  221. $this->assertInstanceOf('\LogicException', $e, '->leaveScope() throws a \LogicException if the scope is not active yet');
  222. $this->assertEquals('The scope "foo" is not active.', $e->getMessage(), '->leaveScope() throws a \LogicException if the scope is not active yet');
  223. }
  224. try {
  225. $container->leaveScope('bar');
  226. $this->fail('->leaveScope() throws a \LogicException if the scope does not exist');
  227. } catch (\Exception $e) {
  228. $this->assertInstanceOf('\LogicException', $e, '->leaveScope() throws a \LogicException if the scope does not exist');
  229. $this->assertEquals('The scope "bar" is not active.', $e->getMessage(), '->leaveScope() throws a \LogicException if the scope does not exist');
  230. }
  231. }
  232. /**
  233. * @expectedException \InvalidArgumentException
  234. * @dataProvider getBuiltInScopes
  235. */
  236. public function testAddScopeDoesNotAllowBuiltInScopes($scope)
  237. {
  238. $container = new Container();
  239. $container->addScope(new Scope($scope));
  240. }
  241. /**
  242. * @expectedException \InvalidArgumentException
  243. */
  244. public function testAddScopeDoesNotAllowExistingScope()
  245. {
  246. $container = new Container();
  247. $container->addScope(new Scope('foo'));
  248. $container->addScope(new Scope('foo'));
  249. }
  250. /**
  251. * @expectedException \InvalidArgumentException
  252. * @dataProvider getInvalidParentScopes
  253. */
  254. public function testAddScopeDoesNotAllowInvalidParentScope($scope)
  255. {
  256. $c = new Container();
  257. $c->addScope(new Scope('foo', $scope));
  258. }
  259. public function testAddScope()
  260. {
  261. $c = new Container();
  262. $c->addScope(new Scope('foo'));
  263. $c->addScope(new Scope('bar', 'foo'));
  264. $this->assertSame(array('foo' => 'container', 'bar' => 'foo'), $this->getField($c, 'scopes'));
  265. $this->assertSame(array('foo' => array('bar'), 'bar' => array()), $this->getField($c, 'scopeChildren'));
  266. }
  267. public function testHasScope()
  268. {
  269. $c = new Container();
  270. $this->assertFalse($c->hasScope('foo'));
  271. $c->addScope(new Scope('foo'));
  272. $this->assertTrue($c->hasScope('foo'));
  273. }
  274. public function testIsScopeActive()
  275. {
  276. $c = new Container();
  277. $this->assertFalse($c->isScopeActive('foo'));
  278. $c->addScope(new Scope('foo'));
  279. $this->assertFalse($c->isScopeActive('foo'));
  280. $c->enterScope('foo');
  281. $this->assertTrue($c->isScopeActive('foo'));
  282. $c->leaveScope('foo');
  283. $this->assertFalse($c->isScopeActive('foo'));
  284. }
  285. public function getInvalidParentScopes()
  286. {
  287. return array(
  288. array(ContainerInterface::SCOPE_PROTOTYPE),
  289. array('bar'),
  290. );
  291. }
  292. public function getBuiltInScopes()
  293. {
  294. return array(
  295. array(ContainerInterface::SCOPE_CONTAINER),
  296. array(ContainerInterface::SCOPE_PROTOTYPE),
  297. );
  298. }
  299. protected function getField($obj, $field)
  300. {
  301. $reflection = new \ReflectionProperty($obj, $field);
  302. $reflection->setAccessible(true);
  303. return $reflection->getValue($obj);
  304. }
  305. }
  306. class ProjectServiceContainer extends Container
  307. {
  308. public $__bar, $__foo_bar, $__foo_baz;
  309. public function __construct()
  310. {
  311. parent::__construct();
  312. $this->__bar = new \stdClass();
  313. $this->__foo_bar = new \stdClass();
  314. $this->__foo_baz = new \stdClass();
  315. }
  316. protected function getScopedService()
  317. {
  318. if (!isset($this->scopedServices['foo'])) {
  319. throw new \RuntimeException('Invalid call');
  320. }
  321. return $this->services['scoped'] = $this->scopedServices['foo']['scoped'] = new \stdClass();
  322. }
  323. protected function getScopedFooService()
  324. {
  325. if (!isset($this->scopedServices['foo'])) {
  326. throw new \RuntimeException('invalid call');
  327. }
  328. return $this->services['scoped_foo'] = $this->scopedServices['foo']['scoped_foo'] = new \stdClass();
  329. }
  330. protected function getBarService()
  331. {
  332. return $this->__bar;
  333. }
  334. protected function getFooBarService()
  335. {
  336. return $this->__foo_bar;
  337. }
  338. protected function getFoo_BazService()
  339. {
  340. return $this->__foo_baz;
  341. }
  342. protected function getCircularService(){
  343. return $this->get('circular');
  344. }
  345. }