ContainerTest.php 13 KB

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