ContainerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Container;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  14. class ContainerTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @covers Symfony\Component\DependencyInjection\Container::__construct
  18. */
  19. public function testConstructor()
  20. {
  21. $sc = new Container();
  22. $this->assertSame($sc, $sc->get('service_container'), '__construct() automatically registers itself as a service');
  23. $sc = new Container(new ParameterBag(array('foo' => 'bar')));
  24. $this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '__construct() takes an array of parameters as its first argument');
  25. }
  26. /**
  27. * @covers Symfony\Component\DependencyInjection\Container::compile
  28. */
  29. public function testcompile()
  30. {
  31. $sc = new Container(new ParameterBag(array('foo' => 'bar')));
  32. $sc->compile();
  33. $this->assertInstanceOf('Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag', $sc->getParameterBag(), '->compile() changes the parameter bag to a FrozenParameterBag instance');
  34. $this->assertEquals(array('foo' => 'bar'), $sc->getParameterBag()->all(), '->compile() copies the current parameters to the new parameter bag');
  35. }
  36. /**
  37. * @covers Symfony\Component\DependencyInjection\Container::isFrozen
  38. */
  39. public function testIsFrozen()
  40. {
  41. $sc = new Container(new ParameterBag(array('foo' => 'bar')));
  42. $this->assertFalse($sc->isFrozen(), '->isFrozen() returns false if the parameters are not frozen');
  43. $sc->compile();
  44. $this->assertTrue($sc->isFrozen(), '->isFrozen() returns true if the parameters are frozen');
  45. }
  46. /**
  47. * @covers Symfony\Component\DependencyInjection\Container::getParameterBag
  48. */
  49. public function testGetParameterBag()
  50. {
  51. $sc = new Container();
  52. $this->assertEquals(array(), $sc->getParameterBag()->all(), '->getParameterBag() returns an empty array if no parameter has been defined');
  53. }
  54. /**
  55. * @covers Symfony\Component\DependencyInjection\Container::setParameter
  56. * @covers Symfony\Component\DependencyInjection\Container::getParameter
  57. */
  58. public function testGetSetParameter()
  59. {
  60. $sc = new Container(new ParameterBag(array('foo' => 'bar')));
  61. $sc->setParameter('bar', 'foo');
  62. $this->assertEquals('foo', $sc->getParameter('bar'), '->setParameter() sets the value of a new parameter');
  63. $sc->setParameter('foo', 'baz');
  64. $this->assertEquals('baz', $sc->getParameter('foo'), '->setParameter() overrides previously set parameter');
  65. $sc->setParameter('Foo', 'baz1');
  66. $this->assertEquals('baz1', $sc->getParameter('foo'), '->setParameter() converts the key to lowercase');
  67. $this->assertEquals('baz1', $sc->getParameter('FOO'), '->getParameter() converts the key to lowercase');
  68. try {
  69. $sc->getParameter('baba');
  70. $this->fail('->getParameter() thrown an \InvalidArgumentException if the key does not exist');
  71. } catch (\Exception $e) {
  72. $this->assertInstanceOf('\InvalidArgumentException', $e, '->getParameter() thrown an \InvalidArgumentException if the key does not exist');
  73. $this->assertEquals('The parameter "baba" must be defined.', $e->getMessage(), '->getParameter() thrown an \InvalidArgumentException if the key does not exist');
  74. }
  75. }
  76. /**
  77. * @covers Symfony\Component\DependencyInjection\Container::getServiceIds
  78. */
  79. public function testGetServiceIds()
  80. {
  81. $sc = new Container();
  82. $sc->set('foo', $obj = new \stdClass());
  83. $sc->set('bar', $obj = new \stdClass());
  84. $this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids');
  85. $sc = new ProjectServiceContainer();
  86. $this->assertEquals(array('bar', 'foo_bar', 'foo.baz', 'service_container'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods');
  87. }
  88. /**
  89. * @covers Symfony\Component\DependencyInjection\Container::set
  90. */
  91. public function testSet()
  92. {
  93. $sc = new Container();
  94. $sc->set('foo', $foo = new \stdClass());
  95. $this->assertEquals($foo, $sc->get('foo'), '->set() sets a service');
  96. }
  97. /**
  98. * @covers Symfony\Component\DependencyInjection\Container::get
  99. */
  100. public function testGet()
  101. {
  102. $sc = new ProjectServiceContainer();
  103. $sc->set('foo', $foo = new \stdClass());
  104. $this->assertEquals($foo, $sc->get('foo'), '->get() returns the service for the given id');
  105. $this->assertEquals($sc->__bar, $sc->get('bar'), '->get() returns the service for the given id');
  106. $this->assertEquals($sc->__foo_bar, $sc->get('foo_bar'), '->get() returns the service if a get*Method() is defined');
  107. $this->assertEquals($sc->__foo_baz, $sc->get('foo.baz'), '->get() returns the service if a get*Method() is defined');
  108. $sc->set('bar', $bar = new \stdClass());
  109. $this->assertSame($sc->get('bar'), $bar, '->getServiceIds() prefers to return a service defined with a getXXXService() method than one defined with set()');
  110. try {
  111. $sc->get('');
  112. $this->fail('->get() throws a \InvalidArgumentException exception if the service is empty');
  113. } catch (\Exception $e) {
  114. $this->assertInstanceOf('\InvalidArgumentException', $e, '->get() throws a \InvalidArgumentException exception if the service is empty');
  115. $this->assertEquals('The service "" does not exist.', $e->getMessage(), '->get() throws a \InvalidArgumentException exception if the service is empty');
  116. }
  117. $this->assertNull($sc->get('', ContainerInterface::NULL_ON_INVALID_REFERENCE));
  118. }
  119. /**
  120. * @covers Symfony\Component\DependencyInjection\Container::has
  121. */
  122. public function testHas()
  123. {
  124. $sc = new ProjectServiceContainer();
  125. $sc->set('foo', new \stdClass());
  126. $this->assertFalse($sc->has('foo1'), '->has() returns false if the service does not exist');
  127. $this->assertTrue($sc->has('foo'), '->has() returns true if the service exists');
  128. $this->assertTrue($sc->has('bar'), '->has() returns true if a get*Method() is defined');
  129. $this->assertTrue($sc->has('foo_bar'), '->has() returns true if a get*Method() is defined');
  130. $this->assertTrue($sc->has('foo.baz'), '->has() returns true if a get*Method() is defined');
  131. }
  132. }
  133. class ProjectServiceContainer extends Container
  134. {
  135. public $__bar, $__foo_bar, $__foo_baz;
  136. public function __construct()
  137. {
  138. parent::__construct();
  139. $this->__bar = new \stdClass();
  140. $this->__foo_bar = new \stdClass();
  141. $this->__foo_baz = new \stdClass();
  142. }
  143. protected function getBarService()
  144. {
  145. return $this->__bar;
  146. }
  147. protected function getFooBarService()
  148. {
  149. return $this->__foo_bar;
  150. }
  151. protected function getFoo_BazService()
  152. {
  153. return $this->__foo_baz;
  154. }
  155. }