ContainerTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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\Components\DependencyInjection;
  10. use Symfony\Components\DependencyInjection\Container;
  11. class ContainerTest extends \PHPUnit_Framework_TestCase
  12. {
  13. public function testConstructor()
  14. {
  15. $sc = new Container();
  16. $this->assertEquals(spl_object_hash($sc), spl_object_hash($sc->getService('service_container')), '__construct() automatically registers itself as a service');
  17. $sc = new Container(array('foo' => 'bar'));
  18. $this->assertEquals(array('foo' => 'bar'), $sc->getParameters(), '__construct() takes an array of parameters as its first argument');
  19. }
  20. public function testGetSetParameters()
  21. {
  22. $sc = new Container();
  23. $this->assertEquals(array(), $sc->getParameters(), '->getParameters() returns an empty array if no parameter has been defined');
  24. $sc->setParameters(array('foo' => 'bar'));
  25. $this->assertEquals(array('foo' => 'bar'), $sc->getParameters(), '->setParameters() sets the parameters');
  26. $sc->setParameters(array('bar' => 'foo'));
  27. $this->assertEquals(array('bar' => 'foo'), $sc->getParameters(), '->setParameters() overrides the previous defined parameters');
  28. $sc->setParameters(array('Bar' => 'foo'));
  29. $this->assertEquals(array('bar' => 'foo'), $sc->getParameters(), '->setParameters() converts the key to lowercase');
  30. }
  31. public function testGetSetParameter()
  32. {
  33. $sc = new Container(array('foo' => 'bar'));
  34. $sc->setParameter('bar', 'foo');
  35. $this->assertEquals('foo', $sc->getParameter('bar'), '->setParameter() sets the value of a new parameter');
  36. $this->assertEquals('foo', $sc['bar'], '->offsetGet() gets the value of a parameter');
  37. $sc['bar1'] = 'foo1';
  38. $this->assertEquals('foo1', $sc['bar1'], '->offsetset() sets the value of a parameter');
  39. unset($sc['bar1']);
  40. $this->assertFalse(isset($sc['bar1']), '->offsetUnset() removes a parameter');
  41. $sc->setParameter('foo', 'baz');
  42. $this->assertEquals('baz', $sc->getParameter('foo'), '->setParameter() overrides previously set parameter');
  43. $sc->setParameter('Foo', 'baz1');
  44. $this->assertEquals('baz1', $sc->getParameter('foo'), '->setParameter() converts the key to lowercase');
  45. $this->assertEquals('baz1', $sc->getParameter('FOO'), '->getParameter() converts the key to lowercase');
  46. $this->assertEquals('baz1', $sc['FOO'], '->offsetGet() converts the key to lowercase');
  47. try
  48. {
  49. $sc->getParameter('baba');
  50. $this->fail('->getParameter() thrown an \InvalidArgumentException if the key does not exist');
  51. }
  52. catch (\Exception $e)
  53. {
  54. $this->assertType('\InvalidArgumentException', $e, '->getParameter() thrown an \InvalidArgumentException if the key does not exist');
  55. $this->assertEquals('The parameter "baba" must be defined.', $e->getMessage(), '->getParameter() thrown an \InvalidArgumentException if the key does not exist');
  56. }
  57. try
  58. {
  59. $sc['baba'];
  60. $this->fail('->offsetGet() thrown an \InvalidArgumentException if the key does not exist');
  61. }
  62. catch (\Exception $e)
  63. {
  64. $this->assertType('\InvalidArgumentException', $e, '->offsetGet() thrown an \InvalidArgumentException if the key does not exist');
  65. $this->assertEquals('The parameter "baba" must be defined.', $e->getMessage(), '->offsetGet() thrown an \InvalidArgumentException if the key does not exist');
  66. }
  67. }
  68. public function testHasParameter()
  69. {
  70. $sc = new Container(array('foo' => 'bar'));
  71. $this->assertTrue($sc->hasParameter('foo'), '->hasParameter() returns true if a parameter is defined');
  72. $this->assertTrue($sc->hasParameter('Foo'), '->hasParameter() converts the key to lowercase');
  73. $this->assertTrue(isset($sc['Foo']), '->offsetExists() converts the key to lowercase');
  74. $this->assertFalse($sc->hasParameter('bar'), '->hasParameter() returns false if a parameter is not defined');
  75. $this->assertTrue(isset($sc['foo']), '->offsetExists() returns true if a parameter is defined');
  76. $this->assertFalse(isset($sc['bar']), '->offsetExists() returns false if a parameter is not defined');
  77. }
  78. public function testAddParameters()
  79. {
  80. $sc = new Container(array('foo' => 'bar'));
  81. $sc->addParameters(array('bar' => 'foo'));
  82. $this->assertEquals(array('foo' => 'bar', 'bar' => 'foo'), $sc->getParameters(), '->addParameters() adds parameters to the existing ones');
  83. $sc->addParameters(array('Bar' => 'fooz'));
  84. $this->assertEquals(array('foo' => 'bar', 'bar' => 'fooz'), $sc->getParameters(), '->addParameters() converts keys to lowercase');
  85. }
  86. public function testServices()
  87. {
  88. $sc = new Container();
  89. $sc->setService('foo', $obj = new \stdClass());
  90. $this->assertEquals(spl_object_hash($obj), spl_object_hash($sc->getService('foo')), '->setService() registers a service under a key name');
  91. $sc->foo1 = $obj1 = new \stdClass();
  92. $this->assertEquals(spl_object_hash($obj1), spl_object_hash($sc->foo1), '->__set() sets a service');
  93. $this->assertEquals(spl_object_hash($obj), spl_object_hash($sc->foo), '->__get() gets a service by name');
  94. $this->assertTrue($sc->hasService('foo'), '->hasService() returns true if the service is defined');
  95. $this->assertTrue(isset($sc->foo), '->__isset() returns true if the service is defined');
  96. $this->assertFalse($sc->hasService('bar'), '->hasService() returns false if the service is not defined');
  97. $this->assertFalse(isset($sc->bar), '->__isset() returns false if the service is not defined');
  98. }
  99. public function testGetServiceIds()
  100. {
  101. $sc = new Container();
  102. $sc->setService('foo', $obj = new \stdClass());
  103. $sc->setService('bar', $obj = new \stdClass());
  104. $this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids');
  105. $sc = new ProjectServiceContainer();
  106. $this->assertEquals(spl_object_hash($sc->__bar), spl_object_hash($sc->getService('bar')), '->getService() looks for a getXXXService() method');
  107. $this->assertTrue($sc->hasService('bar'), '->hasService() returns true if the service has been defined as a getXXXService() method');
  108. $sc->setService('bar', $bar = new \stdClass());
  109. $this->assertEquals(spl_object_hash($sc->getService('bar')), spl_object_hash($bar), '->getService() prefers to return a service defined with a getXXXService() method than one defined with setService()');
  110. try
  111. {
  112. $sc->getService('baba');
  113. $this->fail('->getService() thrown an \InvalidArgumentException if the service does not exist');
  114. }
  115. catch (\Exception $e)
  116. {
  117. $this->assertType('\InvalidArgumentException', $e, '->getService() thrown an \InvalidArgumentException if the service does not exist');
  118. $this->assertEquals('The service "baba" does not exist.', $e->getMessage(), '->getService() thrown an \InvalidArgumentException if the service does not exist');
  119. }
  120. try
  121. {
  122. $sc->baba;
  123. $this->fail('->__get() thrown an \InvalidArgumentException if the service does not exist');
  124. }
  125. catch (\Exception $e)
  126. {
  127. $this->assertType('\InvalidArgumentException', $e, '->__get() thrown an \InvalidArgumentException if the service does not exist');
  128. $this->assertEquals('The service "baba" does not exist.', $e->getMessage(), '->__get() thrown an \InvalidArgumentException if the service does not exist');
  129. }
  130. try
  131. {
  132. unset($sc->baba);
  133. $this->fail('->__unset() thrown an LogicException if you try to remove a service');
  134. }
  135. catch (\Exception $e)
  136. {
  137. $this->assertType('\LogicException', $e, '->__unset() thrown an LogicException if you try to remove a service');
  138. $this->assertEquals('You can\'t unset a service.', $e->getMessage(), '->__unset() thrown an LogicException if you try to remove a service');
  139. }
  140. $this->assertEquals(spl_object_hash($sc->__foo_bar), spl_object_hash($sc->getService('foo_bar')), '->getService() camelizes the service id when looking for a method');
  141. $this->assertEquals(spl_object_hash($sc->__foo_baz), spl_object_hash($sc->getService('foo.baz')), '->getService() camelizes the service id when looking for a method');
  142. }
  143. public function testMagicCall()
  144. {
  145. $sc = new Container();
  146. $sc->setService('foo_bar.foo', $foo = new \stdClass());
  147. $this->assertEquals($foo, $sc->getFooBar_FooService(), '__call() finds services is the method is getXXXService()');
  148. try
  149. {
  150. $sc->getFooBar_Foo();
  151. $this->fail('__call() throws a \BadMethodCallException exception if the method is not a service method');
  152. }
  153. catch (\Exception $e)
  154. {
  155. $this->assertType('\BadMethodCallException', $e, '__call() throws a \BadMethodCallException exception if the method is not a service method');
  156. $this->assertEquals('Call to undefined method Symfony\Components\DependencyInjection\Container::getFooBar_Foo.', $e->getMessage(), '__call() throws a \BadMethodCallException exception if the method is not a service method');
  157. }
  158. }
  159. public function testGetService()
  160. {
  161. $sc = new Container();
  162. try
  163. {
  164. $sc->getService('');
  165. $this->fail('->getService() throws a \InvalidArgumentException exception if the service is empty');
  166. }
  167. catch (\Exception $e)
  168. {
  169. $this->assertType('\InvalidArgumentException', $e, '->getService() throws a \InvalidArgumentException exception if the service is empty');
  170. $this->assertEquals('The service "" does not exist.', $e->getMessage(), '->getService() throws a \InvalidArgumentException exception if the service is empty');
  171. }
  172. }
  173. }
  174. class ProjectServiceContainer extends Container
  175. {
  176. public $__bar, $__foo_bar, $__foo_baz;
  177. public function __construct()
  178. {
  179. parent::__construct();
  180. $this->__bar = new \stdClass();
  181. $this->__foo_bar = new \stdClass();
  182. $this->__foo_baz = new \stdClass();
  183. }
  184. protected function getBarService()
  185. {
  186. return $this->__bar;
  187. }
  188. protected function getFooBarService()
  189. {
  190. return $this->__foo_bar;
  191. }
  192. protected function getFoo_BazService()
  193. {
  194. return $this->__foo_baz;
  195. }
  196. }