ContainerTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Component\DependencyInjection;
  10. use Symfony\Component\DependencyInjection\Container;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  13. class ContainerTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @covers Symfony\Component\DependencyInjection\Container::__construct
  17. */
  18. public function testConstructor()
  19. {
  20. $sc = new Container();
  21. $this->assertEquals(spl_object_hash($sc), spl_object_hash($sc->get('service_container')), '__construct() automatically registers itself as a service');
  22. $sc = new Container(new ParameterBag(array('foo' => 'bar')));
  23. $this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '__construct() takes an array of parameters as its first argument');
  24. }
  25. /**
  26. * @covers Symfony\Component\DependencyInjection\Container::freeze
  27. */
  28. public function testFreeze()
  29. {
  30. $sc = new Container(new ParameterBag(array('foo' => 'bar')));
  31. $sc->freeze();
  32. $this->assertInstanceOf('Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag', $sc->getParameterBag(), '->freeze() changes the parameter bag to a FrozenParameterBag instance');
  33. $this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '->freeze() copies the current parameters to the new parameter bag');
  34. }
  35. /**
  36. * @covers Symfony\Component\DependencyInjection\Container::isFrozen
  37. */
  38. public function testIsFrozen()
  39. {
  40. $sc = new Container(new ParameterBag(array('foo' => 'bar')));
  41. $this->assertFalse($sc->isFrozen(), '->isFrozen() returns false if the parameters are not frozen');
  42. $sc->freeze();
  43. $this->assertTrue($sc->isFrozen(), '->isFrozen() returns true if the parameters are frozen');
  44. }
  45. /**
  46. * @covers Symfony\Component\DependencyInjection\Container::getParameterBag
  47. */
  48. public function testGetParameterBag()
  49. {
  50. $sc = new Container();
  51. $this->assertEquals(array(), $sc->getParameterBag()->all(), '->getParameterBag() returns an empty array if no parameter has been defined');
  52. }
  53. /**
  54. * @covers Symfony\Component\DependencyInjection\Container::setParameter
  55. * @covers Symfony\Component\DependencyInjection\Container::getParameter
  56. */
  57. public function testGetSetParameter()
  58. {
  59. $sc = new Container(new ParameterBag(array('foo' => 'bar')));
  60. $sc->setParameter('bar', 'foo');
  61. $this->assertEquals('foo', $sc->getParameter('bar'), '->setParameter() sets the value of a new parameter');
  62. $sc->setParameter('foo', 'baz');
  63. $this->assertEquals('baz', $sc->getParameter('foo'), '->setParameter() overrides previously set parameter');
  64. $sc->setParameter('Foo', 'baz1');
  65. $this->assertEquals('baz1', $sc->getParameter('foo'), '->setParameter() converts the key to lowercase');
  66. $this->assertEquals('baz1', $sc->getParameter('FOO'), '->getParameter() converts the key to lowercase');
  67. try {
  68. $sc->getParameter('baba');
  69. $this->fail('->getParameter() thrown an \InvalidArgumentException if the key does not exist');
  70. } catch (\Exception $e) {
  71. $this->assertInstanceOf('\InvalidArgumentException', $e, '->getParameter() thrown an \InvalidArgumentException if the key does not exist');
  72. $this->assertEquals('The parameter "baba" must be defined.', $e->getMessage(), '->getParameter() thrown an \InvalidArgumentException if the key does not exist');
  73. }
  74. }
  75. /**
  76. * @covers Symfony\Component\DependencyInjection\Container::getServiceIds
  77. */
  78. public function testGetServiceIds()
  79. {
  80. $sc = new Container();
  81. $sc->set('foo', $obj = new \stdClass());
  82. $sc->set('bar', $obj = new \stdClass());
  83. $this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids');
  84. $sc = new ProjectServiceContainer();
  85. $this->assertEquals(array('bar', 'foo_bar', 'foo.baz', 'service_container'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods');
  86. }
  87. /**
  88. * @covers Symfony\Component\DependencyInjection\Container::__call
  89. */
  90. public function testGetCall()
  91. {
  92. $sc = new Container();
  93. $sc->set('foo_bar.foo', $foo = new \stdClass());
  94. $this->assertEquals($foo, $sc->getFooBar_FooService(), '__call() finds services is the method is getXXXService()');
  95. try {
  96. $sc->getFooBar_Foo();
  97. $this->fail('__call() throws a \BadMethodCallException exception if the method is not a service method');
  98. } catch (\Exception $e) {
  99. $this->assertInstanceOf('\BadMethodCallException', $e, '__call() throws a \BadMethodCallException exception if the method is not a service method');
  100. $this->assertEquals('Call to undefined method Symfony\Component\DependencyInjection\Container::getFooBar_Foo.', $e->getMessage(), '__call() throws a \BadMethodCallException exception if the method is not a service method');
  101. }
  102. }
  103. /**
  104. * @covers Symfony\Component\DependencyInjection\Container::offsetUnset
  105. * @expectedException LogicException
  106. */
  107. public function testOffetUnset()
  108. {
  109. $sc = new Container();
  110. unset($sc['foo']);
  111. }
  112. /**
  113. * @covers Symfony\Component\DependencyInjection\Container::set
  114. * @covers Symfony\Component\DependencyInjection\Container::offsetSet
  115. */
  116. public function testSet()
  117. {
  118. $sc = new Container();
  119. $sc->set('foo', $foo = new \stdClass());
  120. $this->assertEquals($foo, $sc->get('foo'), '->set() sets a service');
  121. $sc['bar'] = $foo = new \stdClass();
  122. $this->assertEquals($foo, $sc->get('bar'), '->offsetSet() sets a service');
  123. }
  124. /**
  125. * @covers Symfony\Component\DependencyInjection\Container::get
  126. * @covers Symfony\Component\DependencyInjection\Container::offsetGet
  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(spl_object_hash($sc->get('bar')), spl_object_hash($bar), '->getServiceIds() prefers to return a service defined with a getXXXService() method than one defined with set()');
  138. try {
  139. $sc->get(new \stdClass());
  140. $this->fail('->get() throws a \InvalidArgumentException exception if the service id is not a string');
  141. } catch (\Exception $e) {
  142. $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws a \InvalidArgumentException exception if the service id is not a string');
  143. }
  144. try {
  145. $sc->get('');
  146. $this->fail('->get() throws a \InvalidArgumentException exception if the service is empty');
  147. } catch (\Exception $e) {
  148. $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws a \InvalidArgumentException exception if the service is empty');
  149. $this->assertEquals('The service "" does not exist.', $e->getMessage(), '->get() throws a \InvalidArgumentException exception if the service is empty');
  150. }
  151. $this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE));
  152. try {
  153. $sc[''];
  154. $this->fail('->get() throws a \InvalidArgumentException exception if the service is empty');
  155. } catch (\Exception $e) {
  156. $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws a \InvalidArgumentException exception if the service is empty');
  157. $this->assertEquals('The service "" does not exist.', $e->getMessage(), '->get() throws a \InvalidArgumentException exception if the service is empty');
  158. }
  159. }
  160. /**
  161. * @covers Symfony\Component\DependencyInjection\Container::has
  162. * @covers Symfony\Component\DependencyInjection\Container::offsetExists
  163. */
  164. public function testHas()
  165. {
  166. $sc = new ProjectServiceContainer();
  167. $sc->set('foo', new \stdClass());
  168. $this->assertFalse($sc->has('foo1'), '->has() returns false if the service does not exist');
  169. $this->assertTrue($sc->has('foo'), '->has() returns true if the service exists');
  170. $this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
  171. $this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
  172. $this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
  173. $this->assertFalse(isset($sc['foo1']), '->offsetExists() returns false if the service does not exist');
  174. $this->assertTrue(isset($sc['foo']), '->offsetExists() returns true if the service exists');
  175. $this->assertTrue(isset($sc['bar']), '->offsetExists() returns true if a get*Method() is defined');
  176. $this->assertTrue(isset($sc['foo_bar']), '->offsetExists() returns true if a get*Method() is defined');
  177. $this->assertTrue(isset($sc['foo.baz']), '->offsetExists() returns true if a get*Method() is defined');
  178. }
  179. }
  180. class ProjectServiceContainer extends Container
  181. {
  182. public $__bar, $__foo_bar, $__foo_baz;
  183. public function __construct()
  184. {
  185. parent::__construct();
  186. $this->__bar = new \stdClass();
  187. $this->__foo_bar = new \stdClass();
  188. $this->__foo_baz = new \stdClass();
  189. }
  190. protected function getBarService()
  191. {
  192. return $this->__bar;
  193. }
  194. protected function getFooBarService()
  195. {
  196. return $this->__foo_bar;
  197. }
  198. protected function getFoo_BazService()
  199. {
  200. return $this->__foo_baz;
  201. }
  202. }