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