/tests/Symfony/Tests/Component/DependencyInjection/ContainerTest.php

https://github.com/rsky/symfony · PHP · 204 lines · 138 code · 28 blank · 38 comment · 0 complexity · fe8c0814edb8fb2a8240bf9a0296a2ec MD5 · raw file

  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::set
  105. */
  106. public function testSet()
  107. {
  108. $sc = new Container();
  109. $sc->set('foo', $foo = new \stdClass());
  110. $this->assertEquals($foo, $sc->get('foo'), '->set() sets a service');
  111. }
  112. /**
  113. * @covers Symfony\Component\DependencyInjection\Container::get
  114. */
  115. public function testGet()
  116. {
  117. $sc = new ProjectServiceContainer();
  118. $sc->set('foo', $foo = new \stdClass());
  119. $this->assertEquals($foo, $sc->get('foo'), '->get() returns the service for the given id');
  120. $this->assertEquals($sc->__bar, $sc->get('bar'), '->get() returns the service for the given id');
  121. $this->assertEquals($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined');
  122. $this->assertEquals($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined');
  123. $sc->set('bar', $bar = new \stdClass());
  124. $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()');
  125. try {
  126. $sc->get(new \stdClass());
  127. $this->fail('->get() throws a \InvalidArgumentException exception if the service id is not a string');
  128. } catch (\Exception $e) {
  129. $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws a \InvalidArgumentException exception if the service id is not a string');
  130. }
  131. try {
  132. $sc->get('');
  133. $this->fail('->get() throws a \InvalidArgumentException exception if the service is empty');
  134. } catch (\Exception $e) {
  135. $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws a \InvalidArgumentException exception if the service is empty');
  136. $this->assertEquals('The service "" does not exist.', $e->getMessage(), '->get() throws a \InvalidArgumentException exception if the service is empty');
  137. }
  138. $this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE));
  139. }
  140. /**
  141. * @covers Symfony\Component\DependencyInjection\Container::has
  142. */
  143. public function testHas()
  144. {
  145. $sc = new ProjectServiceContainer();
  146. $sc->set('foo', new \stdClass());
  147. $this->assertFalse($sc->has('foo1'), '->has() returns false if the service does not exist');
  148. $this->assertTrue($sc->has('foo'), '->has() returns true if the service exists');
  149. $this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
  150. $this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
  151. $this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
  152. }
  153. }
  154. class ProjectServiceContainer extends Container
  155. {
  156. public $__bar, $__foo_bar, $__foo_baz;
  157. public function __construct()
  158. {
  159. parent::__construct();
  160. $this->__bar = new \stdClass();
  161. $this->__foo_bar = new \stdClass();
  162. $this->__foo_baz = new \stdClass();
  163. }
  164. protected function getBarService()
  165. {
  166. return $this->__bar;
  167. }
  168. protected function getFooBarService()
  169. {
  170. return $this->__foo_bar;
  171. }
  172. protected function getFoo_BazService()
  173. {
  174. return $this->__foo_baz;
  175. }
  176. }