ContainerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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->assertSame($sc, $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::compile
  27. */
  28. public function testcompile()
  29. {
  30. $sc = new Container(new ParameterBag(array('foo' => 'bar')));
  31. $sc->compile();
  32. $this->assertInstanceOf('Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag', $sc->getParameterBag(), '->compile() changes the parameter bag to a FrozenParameterBag instance');
  33. $this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '->compile() 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->compile();
  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::set
  89. */
  90. public function testSet()
  91. {
  92. $sc = new Container();
  93. $sc->set('foo', $foo = new \stdClass());
  94. $this->assertEquals($foo, $sc->get('foo'), '->set() sets a service');
  95. }
  96. /**
  97. * @covers Symfony\Component\DependencyInjection\Container::get
  98. */
  99. public function testGet()
  100. {
  101. $sc = new ProjectServiceContainer();
  102. $sc->set('foo', $foo = new \stdClass());
  103. $this->assertEquals($foo, $sc->get('foo'), '->get() returns the service for the given id');
  104. $this->assertEquals($sc->__bar, $sc->get('bar'), '->get() returns the service for the given id');
  105. $this->assertEquals($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined');
  106. $this->assertEquals($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined');
  107. $sc->set('bar', $bar = new \stdClass());
  108. $this->assertSame($sc->get('bar'), $bar, '->getServiceIds() prefers to return a service defined with a getXXXService() method than one defined with set()');
  109. try {
  110. $sc->get('');
  111. $this->fail('->get() throws a \InvalidArgumentException exception if the service is empty');
  112. } catch (\Exception $e) {
  113. $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws a \InvalidArgumentException exception if the service is empty');
  114. $this->assertEquals('The service "" does not exist.', $e->getMessage(), '->get() throws a \InvalidArgumentException exception if the service is empty');
  115. }
  116. $this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE));
  117. }
  118. /**
  119. * @covers Symfony\Component\DependencyInjection\Container::has
  120. */
  121. public function testHas()
  122. {
  123. $sc = new ProjectServiceContainer();
  124. $sc->set('foo', new \stdClass());
  125. $this->assertFalse($sc->has('foo1'), '->has() returns false if the service does not exist');
  126. $this->assertTrue($sc->has('foo'), '->has() returns true if the service exists');
  127. $this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
  128. $this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
  129. $this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
  130. }
  131. }
  132. class ProjectServiceContainer extends Container
  133. {
  134. public $__bar, $__foo_bar, $__foo_baz;
  135. public function __construct()
  136. {
  137. parent::__construct();
  138. $this->__bar = new \stdClass();
  139. $this->__foo_bar = new \stdClass();
  140. $this->__foo_baz = new \stdClass();
  141. }
  142. protected function getBarService()
  143. {
  144. return $this->__bar;
  145. }
  146. protected function getFooBarService()
  147. {
  148. return $this->__foo_bar;
  149. }
  150. protected function getFoo_BazService()
  151. {
  152. return $this->__foo_baz;
  153. }
  154. }