ContainerTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. require_once __DIR__.'/../../bootstrap.php';
  11. use Symfony\Components\DependencyInjection\Container;
  12. class ContainerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructor()
  15. {
  16. $sc = new Container();
  17. $this->assertEquals(spl_object_hash($sc), spl_object_hash($sc->getService('service_container')), '__construct() automatically registers itself as a service');
  18. $sc = new Container(array('foo' => 'bar'));
  19. $this->assertEquals(array('foo' => 'bar'), $sc->getParameters(), '__construct() takes an array of parameters as its first argument');
  20. }
  21. public function testGetSetParameters()
  22. {
  23. $sc = new Container();
  24. $this->assertEquals(array(), $sc->getParameters(), '->getParameters() returns an empty array if no parameter has been defined');
  25. $sc->setParameters(array('foo' => 'bar'));
  26. $this->assertEquals(array('foo' => 'bar'), $sc->getParameters(), '->setParameters() sets the parameters');
  27. $sc->setParameters(array('bar' => 'foo'));
  28. $this->assertEquals(array('bar' => 'foo'), $sc->getParameters(), '->setParameters() overrides the previous defined parameters');
  29. $sc->setParameters(array('Bar' => 'foo'));
  30. $this->assertEquals(array('bar' => 'foo'), $sc->getParameters(), '->setParameters() converts the key to lowercase');
  31. }
  32. public function testGetSetParameter()
  33. {
  34. $sc = new Container(array('foo' => 'bar'));
  35. $sc->setParameter('bar', 'foo');
  36. $this->assertEquals('foo', $sc->getParameter('bar'), '->setParameter() sets the value of a new parameter');
  37. $this->assertEquals('foo', $sc['bar'], '->offsetGet() gets the value of a parameter');
  38. $sc['bar1'] = 'foo1';
  39. $this->assertEquals('foo1', $sc['bar1'], '->offsetset() sets the value of a parameter');
  40. unset($sc['bar1']);
  41. $this->assertTrue(!isset($sc['bar1']), '->offsetUnset() removes a parameter');
  42. $sc->setParameter('foo', 'baz');
  43. $this->assertEquals('baz', $sc->getParameter('foo'), '->setParameter() overrides previously set parameter');
  44. $sc->setParameter('Foo', 'baz1');
  45. $this->assertEquals('baz1', $sc->getParameter('foo'), '->setParameter() converts the key to lowercase');
  46. $this->assertEquals('baz1', $sc->getParameter('FOO'), '->getParameter() converts the key to lowercase');
  47. $this->assertEquals('baz1', $sc['FOO'], '->offsetGet() converts the key to lowercase');
  48. try
  49. {
  50. $sc->getParameter('baba');
  51. $this->fail('->getParameter() thrown an \InvalidArgumentException if the key does not exist');
  52. }
  53. catch (\InvalidArgumentException $e)
  54. {
  55. }
  56. try
  57. {
  58. $sc['baba'];
  59. $this->fail('->offsetGet() thrown an \InvalidArgumentException if the key does not exist');
  60. }
  61. catch (\InvalidArgumentException $e)
  62. {
  63. }
  64. }
  65. public function testHasParameter()
  66. {
  67. $sc = new Container(array('foo' => 'bar'));
  68. $this->assertTrue($sc->hasParameter('foo'), '->hasParameter() returns true if a parameter is defined');
  69. $this->assertTrue($sc->hasParameter('Foo'), '->hasParameter() converts the key to lowercase');
  70. $this->assertTrue(isset($sc['Foo']), '->offsetExists() converts the key to lowercase');
  71. $this->assertTrue(!$sc->hasParameter('bar'), '->hasParameter() returns false if a parameter is not defined');
  72. $this->assertTrue(isset($sc['foo']), '->offsetExists() returns true if a parameter is defined');
  73. $this->assertTrue(!isset($sc['bar']), '->offsetExists() returns false if a parameter is not defined');
  74. }
  75. public function testAddParameters()
  76. {
  77. $sc = new Container(array('foo' => 'bar'));
  78. $sc->addParameters(array('bar' => 'foo'));
  79. $this->assertEquals(array('foo' => 'bar', 'bar' => 'foo'), $sc->getParameters(), '->addParameters() adds parameters to the existing ones');
  80. $sc->addParameters(array('Bar' => 'fooz'));
  81. $this->assertEquals(array('foo' => 'bar', 'bar' => 'fooz'), $sc->getParameters(), '->addParameters() converts keys to lowercase');
  82. }
  83. public function testServices()
  84. {
  85. $sc = new Container();
  86. $sc->setService('foo', $obj = new \stdClass());
  87. $this->assertEquals(spl_object_hash($obj), spl_object_hash($sc->getService('foo')), '->setService() registers a service under a key name');
  88. $sc->foo1 = $obj1 = new \stdClass();
  89. $this->assertEquals(spl_object_hash($obj1), spl_object_hash($sc->foo1), '->__set() sets a service');
  90. $this->assertEquals(spl_object_hash($obj), spl_object_hash($sc->foo), '->__get() gets a service by name');
  91. $this->assertTrue($sc->hasService('foo'), '->hasService() returns true if the service is defined');
  92. $this->assertTrue(isset($sc->foo), '->__isset() returns true if the service is defined');
  93. $this->assertTrue(!$sc->hasService('bar'), '->hasService() returns false if the service is not defined');
  94. $this->assertTrue(!isset($sc->bar), '->__isset() returns false if the service is not defined');
  95. }
  96. public function testGetServiceIds()
  97. {
  98. $sc = new Container();
  99. $sc->setService('foo', $obj = new \stdClass());
  100. $sc->setService('bar', $obj = new \stdClass());
  101. $this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids');
  102. $sc = new ProjectServiceContainer();
  103. $this->assertEquals(spl_object_hash($sc->__bar), spl_object_hash($sc->getService('bar')), '->getService() looks for a getXXXService() method');
  104. $this->assertTrue($sc->hasService('bar'), '->hasService() returns true if the service has been defined as a getXXXService() method');
  105. $sc->setService('bar', $bar = new \stdClass());
  106. $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()');
  107. try
  108. {
  109. $sc->getService('baba');
  110. $this->fail('->getService() thrown an \InvalidArgumentException if the service does not exist');
  111. }
  112. catch (\InvalidArgumentException $e)
  113. {
  114. }
  115. try
  116. {
  117. $sc->baba;
  118. $this->fail('->__get() thrown an \InvalidArgumentException if the service does not exist');
  119. }
  120. catch (\InvalidArgumentException $e)
  121. {
  122. }
  123. try
  124. {
  125. unset($sc->baba);
  126. $this->fail('->__unset() thrown an LogicException if you try to remove a service');
  127. }
  128. catch (\LogicException $e)
  129. {
  130. }
  131. $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');
  132. $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');
  133. }
  134. public function testMagicCall()
  135. {
  136. $sc = new Container();
  137. $sc->setService('foo_bar.foo', $foo = new \stdClass());
  138. $this->assertEquals($foo, $sc->getFooBar_FooService(), '__call() finds services is the method is getXXXService()');
  139. try
  140. {
  141. $sc->getFooBar_Foo();
  142. $this->fail('__call() throws a \RuntimeException exception if the method is not a service method');
  143. }
  144. catch (\RuntimeException $e)
  145. {
  146. }
  147. }
  148. }
  149. class ProjectServiceContainer extends Container
  150. {
  151. public $__bar, $__foo_bar, $__foo_baz;
  152. public function __construct()
  153. {
  154. parent::__construct();
  155. $this->__bar = new \stdClass();
  156. $this->__foo_bar = new \stdClass();
  157. $this->__foo_baz = new \stdClass();
  158. }
  159. protected function getBarService()
  160. {
  161. return $this->__bar;
  162. }
  163. protected function getFooBarService()
  164. {
  165. return $this->__foo_bar;
  166. }
  167. protected function getFoo_BazService()
  168. {
  169. return $this->__foo_baz;
  170. }
  171. }