ContainerTest.php 11 KB

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