BuilderTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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\Builder;
  11. use Symfony\Components\DependencyInjection\BuilderConfiguration;
  12. use Symfony\Components\DependencyInjection\Definition;
  13. use Symfony\Components\DependencyInjection\Reference;
  14. $fixturesPath = __DIR__.'/../../../../fixtures/Symfony/Components/DependencyInjection/';
  15. $t = new LimeTest(59);
  16. // ->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition()
  17. $t->diag('->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition()');
  18. $builder = new Builder();
  19. $definitions = array(
  20. 'foo' => new Definition('FooClass'),
  21. 'bar' => new Definition('BarClass'),
  22. );
  23. $builder->setDefinitions($definitions);
  24. $t->is($builder->getDefinitions(), $definitions, '->setDefinitions() sets the service definitions');
  25. $t->ok($builder->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists');
  26. $t->ok(!$builder->hasDefinition('foobar'), '->hasDefinition() returns false if a service definition does not exist');
  27. $builder->setDefinition('foobar', $foo = new Definition('FooBarClass'));
  28. $t->is($builder->getDefinition('foobar'), $foo, '->getDefinition() returns a service definition if defined');
  29. $t->ok($builder->setDefinition('foobar', $foo = new Definition('FooBarClass')) === $foo, '->setDefinition() implements a fuild interface by returning the service reference');
  30. $builder->addDefinitions($defs = array('foobar' => new Definition('FooBarClass')));
  31. $t->is($builder->getDefinitions(), array_merge($definitions, $defs), '->addDefinitions() adds the service definitions');
  32. try
  33. {
  34. $builder->getDefinition('baz');
  35. $t->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
  36. }
  37. catch (InvalidArgumentException $e)
  38. {
  39. $t->pass('->getDefinition() throws an InvalidArgumentException if the service definition does not exist');
  40. }
  41. // ->register()
  42. $t->diag('->register()');
  43. $builder = new Builder();
  44. $builder->register('foo', 'FooClass');
  45. $t->ok($builder->hasDefinition('foo'), '->register() registers a new service definition');
  46. $t->ok($builder->getDefinition('foo') instanceof Definition, '->register() returns the newly created Definition instance');
  47. // ->hasService()
  48. $t->diag('->hasService()');
  49. $builder = new Builder();
  50. $t->ok(!$builder->hasService('foo'), '->hasService() returns false if the service does not exist');
  51. $builder->register('foo', 'FooClass');
  52. $t->ok($builder->hasService('foo'), '->hasService() returns true if a service definition exists');
  53. $builder->bar = new stdClass();
  54. $t->ok($builder->hasService('bar'), '->hasService() returns true if a service exists');
  55. // ->getService()
  56. $t->diag('->getService()');
  57. $builder = new Builder();
  58. try
  59. {
  60. $builder->getService('foo');
  61. $t->fail('->getService() throws an InvalidArgumentException if the service does not exist');
  62. }
  63. catch (InvalidArgumentException $e)
  64. {
  65. $t->pass('->getService() throws an InvalidArgumentException if the service does not exist');
  66. }
  67. $builder->register('foo', 'stdClass');
  68. $t->ok(is_object($builder->getService('foo')), '->getService() returns the service definition associated with the id');
  69. $builder->bar = $bar = new stdClass();
  70. $t->is($builder->getService('bar'), $bar, '->getService() returns the service associated with the id');
  71. $builder->register('bar', 'stdClass');
  72. $t->is($builder->getService('bar'), $bar, '->getService() returns the service associated with the id even if a definition has been defined');
  73. $builder->register('baz', 'stdClass')->setArguments(array(new Reference('baz')));
  74. try
  75. {
  76. @$builder->getService('baz');
  77. $t->fail('->getService() throws a LogicException if the service has a circular reference to itself');
  78. }
  79. catch (\LogicException $e)
  80. {
  81. $t->pass('->getService() throws a LogicException if the service has a circular reference to itself');
  82. }
  83. $builder->register('foobar', 'stdClass')->setShared(true);
  84. $t->ok($builder->getService('bar') === $builder->getService('bar'), '->getService() always returns the same instance if the service is shared');
  85. // ->getServiceIds()
  86. $t->diag('->getServiceIds()');
  87. $builder = new Builder();
  88. $builder->register('foo', 'stdClass');
  89. $builder->bar = $bar = new stdClass();
  90. $builder->register('bar', 'stdClass');
  91. $t->is($builder->getServiceIds(), array('foo', 'bar', 'service_container'), '->getServiceIds() returns all defined service ids');
  92. // ->setAlias() ->getAlias() ->hasAlias()
  93. $t->diag('->setAlias() ->getAlias() ->hasAlias()');
  94. $builder = new Builder();
  95. $builder->register('foo', 'stdClass');
  96. $builder->setAlias('bar', 'foo');
  97. $t->ok($builder->hasAlias('bar'), '->hasAlias() returns true if the alias exists');
  98. $t->ok(!$builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist');
  99. $t->is($builder->getAlias('bar'), 'foo', '->getAlias() returns the aliased service');
  100. $t->ok($builder->hasService('bar'), '->setAlias() defines a new service');
  101. $t->ok($builder->getService('bar') === $builder->getService('foo'), '->setAlias() creates a service that is an alias to another one');
  102. try
  103. {
  104. $builder->getAlias('foobar');
  105. $t->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist');
  106. }
  107. catch (\InvalidArgumentException $e)
  108. {
  109. $t->pass('->getAlias() throws an InvalidArgumentException if the alias does not exist');
  110. }
  111. // ->getAliases()
  112. $t->diag('->getAliases()');
  113. $builder = new Builder();
  114. $builder->setAlias('bar', 'foo');
  115. $builder->setAlias('foobar', 'foo');
  116. $t->is($builder->getAliases(), array('bar' => 'foo', 'foobar' => 'foo'), '->getAliases() returns all service aliases');
  117. $builder->register('bar', 'stdClass');
  118. $t->is($builder->getAliases(), array('foobar' => 'foo'), '->getAliases() does not return aliased services that have been overridden');
  119. $builder->setService('foobar', 'stdClass');
  120. $t->is($builder->getAliases(), array(), '->getAliases() does not return aliased services that have been overridden');
  121. // ->createService() # file
  122. $t->diag('->createService() # file');
  123. $builder = new Builder();
  124. $builder->register('foo1', 'FooClass')->setFile($fixturesPath.'/includes/foo.php');
  125. $t->ok($builder->getService('foo1'), '->createService() requires the file defined by the service definition');
  126. $builder->register('foo2', 'FooClass')->setFile($fixturesPath.'/includes/%file%.php');
  127. $builder->setParameter('file', 'foo');
  128. $t->ok($builder->getService('foo2'), '->createService() replaces parameters in the file provided by the service definition');
  129. // ->createService() # class
  130. $t->diag('->createService() # class');
  131. $builder = new Builder();
  132. $builder->register('foo1', '%class%');
  133. $builder->setParameter('class', 'stdClass');
  134. $t->ok($builder->getService('foo1') instanceof stdClass, '->createService() replaces parameters in the class provided by the service definition');
  135. // ->createService() # arguments
  136. $t->diag('->createService() # arguments');
  137. $builder = new Builder();
  138. $builder->register('bar', 'stdClass');
  139. $builder->register('foo1', 'FooClass')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
  140. $builder->setParameter('value', 'bar');
  141. $t->is($builder->getService('foo1')->arguments, array('foo' => 'bar', 'bar' => 'foo', $builder->getService('bar')), '->createService() replaces parameters and service references in the arguments provided by the service definition');
  142. // ->createService() # constructor
  143. $t->diag('->createService() # constructor');
  144. $builder = new Builder();
  145. $builder->register('bar', 'stdClass');
  146. $builder->register('foo1', 'FooClass')->setConstructor('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
  147. $builder->setParameter('value', 'bar');
  148. $t->ok($builder->getService('foo1')->called, '->createService() calls the constructor to create the service instance');
  149. $t->is($builder->getService('foo1')->arguments, array('foo' => 'bar', 'bar' => 'foo', $builder->getService('bar')), '->createService() passes the arguments to the constructor');
  150. // ->createService() # method calls
  151. $t->diag('->createService() # method calls');
  152. $builder = new Builder();
  153. $builder->register('bar', 'stdClass');
  154. $builder->register('foo1', 'FooClass')->addMethodCall('setBar', array(array('%value%', new Reference('bar'))));
  155. $builder->setParameter('value', 'bar');
  156. $t->is($builder->getService('foo1')->bar, array('bar', $builder->getService('bar')), '->createService() replaces the values in the method calls arguments');
  157. // ->createService() # configurator
  158. require_once $fixturesPath.'/includes/classes.php';
  159. $t->diag('->createService() # configurator');
  160. $builder = new Builder();
  161. $builder->register('foo1', 'FooClass')->setConfigurator('sc_configure');
  162. $t->ok($builder->getService('foo1')->configured, '->createService() calls the configurator');
  163. $builder->register('foo2', 'FooClass')->setConfigurator(array('%class%', 'configureStatic'));
  164. $builder->setParameter('class', 'BazClass');
  165. $t->ok($builder->getService('foo2')->configured, '->createService() calls the configurator');
  166. $builder->register('baz', 'BazClass');
  167. $builder->register('foo3', 'FooClass')->setConfigurator(array(new Reference('baz'), 'configure'));
  168. $t->ok($builder->getService('foo3')->configured, '->createService() calls the configurator');
  169. $builder->register('foo4', 'FooClass')->setConfigurator('foo');
  170. try
  171. {
  172. $builder->getService('foo4');
  173. $t->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
  174. }
  175. catch (\InvalidArgumentException $e)
  176. {
  177. $t->pass('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
  178. }
  179. // ::resolveValue()
  180. $t->diag('::resolveValue()');
  181. $t->is(Builder::resolveValue('foo', array()), 'foo', '->resolveValue() returns its argument unmodified if no placeholders are found');
  182. $t->is(Builder::resolveValue('I\'m a %foo%', array('foo' => 'bar')), 'I\'m a bar', '->resolveValue() replaces placeholders by their values');
  183. $t->ok(Builder::resolveValue('%foo%', array('foo' => true)) === true, '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings');
  184. $t->is(Builder::resolveValue(array('%foo%' => '%foo%'), array('foo' => 'bar')), array('bar' => 'bar'), '->resolveValue() replaces placeholders in keys and values of arrays');
  185. $t->is(Builder::resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%'))), array('foo' => 'bar')), array('bar' => array('bar' => array('bar' => 'bar'))), '->resolveValue() replaces placeholders in nested arrays');
  186. $t->is(Builder::resolveValue('I\'m a %%foo%%', array('foo' => 'bar')), 'I\'m a %foo%', '->resolveValue() supports % escaping by doubling it');
  187. $t->is(Builder::resolveValue('I\'m a %foo% %%foo %foo%', array('foo' => 'bar')), 'I\'m a bar %foo bar', '->resolveValue() supports % escaping by doubling it');
  188. try
  189. {
  190. Builder::resolveValue('%foobar%', array());
  191. $t->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
  192. }
  193. catch (RuntimeException $e)
  194. {
  195. $t->pass('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
  196. }
  197. try
  198. {
  199. Builder::resolveValue('foo %foobar% bar', array());
  200. $t->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
  201. }
  202. catch (RuntimeException $e)
  203. {
  204. $t->pass('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
  205. }
  206. // ->resolveServices()
  207. $t->diag('->resolveServices()');
  208. $builder = new Builder();
  209. $builder->register('foo', 'FooClass');
  210. $t->is($builder->resolveServices(new Reference('foo')), $builder->getService('foo'), '->resolveServices() resolves service references to service instances');
  211. $t->is($builder->resolveServices(array('foo' => array('foo', new Reference('foo')))), array('foo' => array('foo', $builder->getService('foo'))), '->resolveServices() resolves service references to service instances in nested arrays');
  212. // ->merge()
  213. $t->diag('->merge()');
  214. $container = new Builder();
  215. $container->merge(null);
  216. $t->is($container->getParameters(), array(), '->merge() accepts null as an argument');
  217. $t->is($container->getDefinitions(), array(), '->merge() accepts null as an argument');
  218. $container = new Builder(array('bar' => 'foo'));
  219. $config = new BuilderConfiguration();
  220. $config->setParameters(array('foo' => 'bar'));
  221. $container->merge($config);
  222. $t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'bar'), '->merge() merges current parameters with the loaded ones');
  223. $container = new Builder(array('bar' => 'foo', 'foo' => 'baz'));
  224. $config = new BuilderConfiguration();
  225. $config->setParameters(array('foo' => 'bar'));
  226. $container->merge($config);
  227. $t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'baz'), '->merge() does not change the already defined parameters');
  228. $container = new Builder(array('bar' => 'foo'));
  229. $config = new BuilderConfiguration();
  230. $config->setParameters(array('foo' => '%bar%'));
  231. $container->merge($config);
  232. $t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones');
  233. $container = new Builder(array('bar' => 'foo'));
  234. $config = new BuilderConfiguration();
  235. $config->setParameters(array('foo' => '%bar%', 'baz' => '%foo%'));
  236. $container->merge($config);
  237. $t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones');
  238. $container = new Builder();
  239. $container->register('foo', 'FooClass');
  240. $container->register('bar', 'BarClass');
  241. $config = new BuilderConfiguration();
  242. $config->setDefinition('baz', new Definition('BazClass'));
  243. $config->setAlias('alias_for_foo', 'foo');
  244. $container->merge($config);
  245. $t->is(array_keys($container->getDefinitions()), array('foo', 'bar', 'baz'), '->merge() merges definitions already defined ones');
  246. $t->is($container->getAliases(), array('alias_for_foo' => 'foo'), '->merge() registers defined aliases');
  247. $container = new Builder();
  248. $container->register('foo', 'FooClass');
  249. $config->setDefinition('foo', new Definition('BazClass'));
  250. $container->merge($config);
  251. $t->is($container->getDefinition('foo')->getClass(), 'BazClass', '->merge() overrides already defined services');