ContainerTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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->assertTrue(!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 (\InvalidArgumentException $e)
  53. {
  54. }
  55. try
  56. {
  57. $sc['baba'];
  58. $this->fail('->offsetGet() thrown an \InvalidArgumentException if the key does not exist');
  59. }
  60. catch (\InvalidArgumentException $e)
  61. {
  62. }
  63. }
  64. public function testHasParameter()
  65. {
  66. $sc = new Container(array('foo' => 'bar'));
  67. $this->assertTrue($sc->hasParameter('foo'), '->hasParameter() returns true if a parameter is defined');
  68. $this->assertTrue($sc->hasParameter('Foo'), '->hasParameter() converts the key to lowercase');
  69. $this->assertTrue(isset($sc['Foo']), '->offsetExists() converts the key to lowercase');
  70. $this->assertTrue(!$sc->hasParameter('bar'), '->hasParameter() returns false if a parameter is not defined');
  71. $this->assertTrue(isset($sc['foo']), '->offsetExists() returns true if a parameter is defined');
  72. $this->assertTrue(!isset($sc['bar']), '->offsetExists() returns false if a parameter is not defined');
  73. }
  74. public function testAddParameters()
  75. {
  76. $sc = new Container(array('foo' => 'bar'));
  77. $sc->addParameters(array('bar' => 'foo'));
  78. $this->assertEquals(array('foo' => 'bar', 'bar' => 'foo'), $sc->getParameters(), '->addParameters() adds parameters to the existing ones');
  79. $sc->addParameters(array('Bar' => 'fooz'));
  80. $this->assertEquals(array('foo' => 'bar', 'bar' => 'fooz'), $sc->getParameters(), '->addParameters() converts keys to lowercase');
  81. }
  82. public function testServices()
  83. {
  84. $sc = new Container();
  85. $sc->setService('foo', $obj = new \stdClass());
  86. $this->assertEquals(spl_object_hash($obj), spl_object_hash($sc->getService('foo')), '->setService() registers a service under a key name');
  87. $sc->foo1 = $obj1 = new \stdClass();
  88. $this->assertEquals(spl_object_hash($obj1), spl_object_hash($sc->foo1), '->__set() sets a service');
  89. $this->assertEquals(spl_object_hash($obj), spl_object_hash($sc->foo), '->__get() gets a service by name');
  90. $this->assertTrue($sc->hasService('foo'), '->hasService() returns true if the service is defined');
  91. $this->assertTrue(isset($sc->foo), '->__isset() returns true if the service is defined');
  92. $this->assertTrue(!$sc->hasService('bar'), '->hasService() returns false if the service is not defined');
  93. $this->assertTrue(!isset($sc->bar), '->__isset() returns false if the service is not defined');
  94. }
  95. public function testGetServiceIds()
  96. {
  97. $sc = new Container();
  98. $sc->setService('foo', $obj = new \stdClass());
  99. $sc->setService('bar', $obj = new \stdClass());
  100. $this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids');
  101. $sc = new ProjectServiceContainer();
  102. $this->assertEquals(spl_object_hash($sc->__bar), spl_object_hash($sc->getService('bar')), '->getService() looks for a getXXXService() method');
  103. $this->assertTrue($sc->hasService('bar'), '->hasService() returns true if the service has been defined as a getXXXService() method');
  104. $sc->setService('bar', $bar = new \stdClass());
  105. $this->assertNotEquals(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()');
  106. try
  107. {
  108. $sc->getService('baba');
  109. $this->fail('->getService() thrown an \InvalidArgumentException if the service does not exist');
  110. }
  111. catch (\InvalidArgumentException $e)
  112. {
  113. }
  114. try
  115. {
  116. $sc->baba;
  117. $this->fail('->__get() thrown an \InvalidArgumentException if the service does not exist');
  118. }
  119. catch (\InvalidArgumentException $e)
  120. {
  121. }
  122. try
  123. {
  124. unset($sc->baba);
  125. $this->fail('->__unset() thrown an LogicException if you try to remove a service');
  126. }
  127. catch (\LogicException $e)
  128. {
  129. }
  130. $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');
  131. $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');
  132. }
  133. public function testMagicCall()
  134. {
  135. $sc = new Container();
  136. $sc->setService('foo_bar.foo', $foo = new \stdClass());
  137. $this->assertEquals($foo, $sc->getFooBar_FooService(), '__call() finds services is the method is getXXXService()');
  138. try
  139. {
  140. $sc->getFooBar_Foo();
  141. $this->fail('__call() throws a \RuntimeException exception if the method is not a service method');
  142. }
  143. catch (\RuntimeException $e)
  144. {
  145. }
  146. }
  147. public function testGetService()
  148. {
  149. $sc = new Container();
  150. try
  151. {
  152. $sc->getService('');
  153. $this->fail('->getService() throws a \InvalidArgumentException exception if the service is empty');
  154. }
  155. catch (\InvalidArgumentException $e)
  156. {
  157. }
  158. }
  159. }
  160. class ProjectServiceContainer extends Container
  161. {
  162. public $__bar, $__foo_bar, $__foo_baz;
  163. public function __construct()
  164. {
  165. parent::__construct();
  166. $this->__bar = new \stdClass();
  167. $this->__foo_bar = new \stdClass();
  168. $this->__foo_baz = new \stdClass();
  169. }
  170. protected function getBarService()
  171. {
  172. return $this->__bar;
  173. }
  174. protected function getFooBarService()
  175. {
  176. return $this->__foo_bar;
  177. }
  178. protected function getFoo_BazService()
  179. {
  180. return $this->__foo_baz;
  181. }
  182. }