ContainerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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', '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->assertSame($sc->get('bar'), $bar, '->getServiceIds() prefers to return a service defined with a getXXXService() method than one defined with set()');
  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. /**
  148. * @covers Symfony\Component\DependencyInjection\Container::has
  149. */
  150. public function testHas()
  151. {
  152. $sc = new ProjectServiceContainer();
  153. $sc->set('foo', new \stdClass());
  154. $this->assertFalse($sc->has('foo1'), '->has() returns false if the service does not exist');
  155. $this->assertTrue($sc->has('foo'), '->has() returns true if the service exists');
  156. $this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
  157. $this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
  158. $this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
  159. }
  160. public function testEnterLeaveCurrentScope()
  161. {
  162. $container = new ProjectServiceContainer();
  163. $container->addScope(new Scope('foo'));
  164. $container->enterScope('foo');
  165. $scoped1 = $container->get('scoped');
  166. $scopedFoo1 = $container->get('scoped_foo');
  167. $container->enterScope('foo');
  168. $scoped2 = $container->get('scoped');
  169. $scoped3 = $container->get('scoped');
  170. $scopedFoo2 = $container->get('scoped_foo');
  171. $container->leaveScope('foo');
  172. $scoped4 = $container->get('scoped');
  173. $scopedFoo3 = $container->get('scoped_foo');
  174. $this->assertNotSame($scoped1, $scoped2);
  175. $this->assertSame($scoped2, $scoped3);
  176. $this->assertSame($scoped1, $scoped4);
  177. $this->assertNotSame($scopedFoo1, $scopedFoo2);
  178. $this->assertSame($scopedFoo1, $scopedFoo3);
  179. }
  180. public function testEnterLeaveScopeWithChildScopes()
  181. {
  182. $container = new Container();
  183. $container->addScope(new Scope('foo'));
  184. $container->addScope(new Scope('bar', 'foo'));
  185. $this->assertFalse($container->isScopeActive('foo'));
  186. $container->enterScope('foo');
  187. $container->enterScope('bar');
  188. $this->assertTrue($container->isScopeActive('foo'));
  189. $this->assertFalse($container->has('a'));
  190. $a = new \stdClass();
  191. $container->set('a', $a, 'bar');
  192. $services = $this->getField($container, 'scopedServices');
  193. $this->assertTrue(isset($services['bar']['a']));
  194. $this->assertSame($a, $services['bar']['a']);
  195. $this->assertTrue($container->has('a'));
  196. $container->leaveScope('foo');
  197. $services = $this->getField($container, 'scopedServices');
  198. $this->assertFalse(isset($services['bar']));
  199. $this->assertFalse($container->isScopeActive('foo'));
  200. $this->assertFalse($container->has('a'));
  201. }
  202. /**
  203. * @expectedException \InvalidArgumentException
  204. * @dataProvider getBuiltInScopes
  205. */
  206. public function testAddScopeDoesNotAllowBuiltInScopes($scope)
  207. {
  208. $container = new Container();
  209. $container->addScope(new Scope($scope));
  210. }
  211. /**
  212. * @expectedException \InvalidArgumentException
  213. */
  214. public function testAddScopeDoesNotAllowExistingScope()
  215. {
  216. $container = new Container();
  217. $container->addScope(new Scope('foo'));
  218. $container->addScope(new Scope('foo'));
  219. }
  220. /**
  221. * @expectedException \InvalidArgumentException
  222. * @dataProvider getInvalidParentScopes
  223. */
  224. public function testAddScopeDoesNotAllowInvalidParentScope($scope)
  225. {
  226. $c = new Container();
  227. $c->addScope(new Scope('foo', $scope));
  228. }
  229. public function testAddScope()
  230. {
  231. $c = new Container();
  232. $c->addScope(new Scope('foo'));
  233. $c->addScope(new Scope('bar', 'foo'));
  234. $this->assertSame(array('foo' => 'container', 'bar' => 'foo'), $this->getField($c, 'scopes'));
  235. $this->assertSame(array('foo' => array('bar'), 'bar' => array()), $this->getField($c, 'scopeChildren'));
  236. }
  237. public function testHasScope()
  238. {
  239. $c = new Container();
  240. $this->assertFalse($c->hasScope('foo'));
  241. $c->addScope(new Scope('foo'));
  242. $this->assertTrue($c->hasScope('foo'));
  243. }
  244. public function testIsScopeActive()
  245. {
  246. $c = new Container();
  247. $this->assertFalse($c->isScopeActive('foo'));
  248. $c->addScope(new Scope('foo'));
  249. $this->assertFalse($c->isScopeActive('foo'));
  250. $c->enterScope('foo');
  251. $this->assertTrue($c->isScopeActive('foo'));
  252. $c->leaveScope('foo');
  253. $this->assertFalse($c->isScopeActive('foo'));
  254. }
  255. public function getInvalidParentScopes()
  256. {
  257. return array(
  258. array(ContainerInterface::SCOPE_PROTOTYPE),
  259. array('bar'),
  260. );
  261. }
  262. public function getBuiltInScopes()
  263. {
  264. return array(
  265. array(ContainerInterface::SCOPE_CONTAINER),
  266. array(ContainerInterface::SCOPE_PROTOTYPE),
  267. );
  268. }
  269. protected function getField($obj, $field)
  270. {
  271. $reflection = new \ReflectionProperty($obj, $field);
  272. $reflection->setAccessible(true);
  273. return $reflection->getValue($obj);
  274. }
  275. }
  276. class ProjectServiceContainer extends Container
  277. {
  278. public $__bar, $__foo_bar, $__foo_baz;
  279. public function __construct()
  280. {
  281. parent::__construct();
  282. $this->__bar = new \stdClass();
  283. $this->__foo_bar = new \stdClass();
  284. $this->__foo_baz = new \stdClass();
  285. }
  286. protected function getScopedService()
  287. {
  288. if (!isset($this->scopedServices['foo'])) {
  289. throw new \RuntimeException('Invalid call');
  290. }
  291. return $this->services['scoped'] = $this->scopedServices['foo']['scoped'] = new \stdClass();
  292. }
  293. protected function getScopedFooService()
  294. {
  295. if (!isset($this->scopedServices['foo'])) {
  296. throw new \RuntimeException('invalid call');
  297. }
  298. return $this->services['scoped_foo'] = $this->scopedServices['foo']['scoped_foo'] = new \stdClass();
  299. }
  300. protected function getBarService()
  301. {
  302. return $this->__bar;
  303. }
  304. protected function getFooBarService()
  305. {
  306. return $this->__foo_bar;
  307. }
  308. protected function getFoo_BazService()
  309. {
  310. return $this->__foo_baz;
  311. }
  312. }