BuilderTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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(55);
  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()
  93. $t->diag('->setAlias()');
  94. $builder = new Builder();
  95. $builder->register('foo', 'stdClass');
  96. $builder->setAlias('bar', 'foo');
  97. $t->ok($builder->hasService('bar'), '->setAlias() defines a new service');
  98. $t->ok($builder->getService('bar') === $builder->getService('foo'), '->setAlias() creates a service that is an alias to another one');
  99. // ->getAliases()
  100. $t->diag('->getAliases()');
  101. $builder = new Builder();
  102. $builder->setAlias('bar', 'foo');
  103. $builder->setAlias('foobar', 'foo');
  104. $t->is($builder->getAliases(), array('bar' => 'foo', 'foobar' => 'foo'), '->getAliases() returns all service aliases');
  105. $builder->register('bar', 'stdClass');
  106. $t->is($builder->getAliases(), array('foobar' => 'foo'), '->getAliases() does not return aliased services that have been overridden');
  107. $builder->setService('foobar', 'stdClass');
  108. $t->is($builder->getAliases(), array(), '->getAliases() does not return aliased services that have been overridden');
  109. // ->createService() # file
  110. $t->diag('->createService() # file');
  111. $builder = new Builder();
  112. $builder->register('foo1', 'FooClass')->setFile($fixturesPath.'/includes/foo.php');
  113. $t->ok($builder->getService('foo1'), '->createService() requires the file defined by the service definition');
  114. $builder->register('foo2', 'FooClass')->setFile($fixturesPath.'/includes/%file%.php');
  115. $builder->setParameter('file', 'foo');
  116. $t->ok($builder->getService('foo2'), '->createService() replaces parameters in the file provided by the service definition');
  117. // ->createService() # class
  118. $t->diag('->createService() # class');
  119. $builder = new Builder();
  120. $builder->register('foo1', '%class%');
  121. $builder->setParameter('class', 'stdClass');
  122. $t->ok($builder->getService('foo1') instanceof stdClass, '->createService() replaces parameters in the class provided by the service definition');
  123. // ->createService() # arguments
  124. $t->diag('->createService() # arguments');
  125. $builder = new Builder();
  126. $builder->register('bar', 'stdClass');
  127. $builder->register('foo1', 'FooClass')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
  128. $builder->setParameter('value', 'bar');
  129. $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');
  130. // ->createService() # constructor
  131. $t->diag('->createService() # constructor');
  132. $builder = new Builder();
  133. $builder->register('bar', 'stdClass');
  134. $builder->register('foo1', 'FooClass')->setConstructor('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar')));
  135. $builder->setParameter('value', 'bar');
  136. $t->ok($builder->getService('foo1')->called, '->createService() calls the constructor to create the service instance');
  137. $t->is($builder->getService('foo1')->arguments, array('foo' => 'bar', 'bar' => 'foo', $builder->getService('bar')), '->createService() passes the arguments to the constructor');
  138. // ->createService() # method calls
  139. $t->diag('->createService() # method calls');
  140. $builder = new Builder();
  141. $builder->register('bar', 'stdClass');
  142. $builder->register('foo1', 'FooClass')->addMethodCall('setBar', array(array('%value%', new Reference('bar'))));
  143. $builder->setParameter('value', 'bar');
  144. $t->is($builder->getService('foo1')->bar, array('bar', $builder->getService('bar')), '->createService() replaces the values in the method calls arguments');
  145. // ->createService() # configurator
  146. require_once $fixturesPath.'/includes/classes.php';
  147. $t->diag('->createService() # configurator');
  148. $builder = new Builder();
  149. $builder->register('foo1', 'FooClass')->setConfigurator('sc_configure');
  150. $t->ok($builder->getService('foo1')->configured, '->createService() calls the configurator');
  151. $builder->register('foo2', 'FooClass')->setConfigurator(array('%class%', 'configureStatic'));
  152. $builder->setParameter('class', 'BazClass');
  153. $t->ok($builder->getService('foo2')->configured, '->createService() calls the configurator');
  154. $builder->register('baz', 'BazClass');
  155. $builder->register('foo3', 'FooClass')->setConfigurator(array(new Reference('baz'), 'configure'));
  156. $t->ok($builder->getService('foo3')->configured, '->createService() calls the configurator');
  157. $builder->register('foo4', 'FooClass')->setConfigurator('foo');
  158. try
  159. {
  160. $builder->getService('foo4');
  161. $t->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
  162. }
  163. catch (InvalidArgumentException $e)
  164. {
  165. $t->pass('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable');
  166. }
  167. // ::resolveValue()
  168. $t->diag('::resolveValue()');
  169. $t->is(Builder::resolveValue('foo', array()), 'foo', '->resolveValue() returns its argument unmodified if no placeholders are found');
  170. $t->is(Builder::resolveValue('I\'m a %foo%', array('foo' => 'bar')), 'I\'m a bar', '->resolveValue() replaces placeholders by their values');
  171. $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');
  172. $t->is(Builder::resolveValue(array('%foo%' => '%foo%'), array('foo' => 'bar')), array('bar' => 'bar'), '->resolveValue() replaces placeholders in keys and values of arrays');
  173. $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');
  174. $t->is(Builder::resolveValue('I\'m a %%foo%%', array('foo' => 'bar')), 'I\'m a %foo%', '->resolveValue() supports % escaping by doubling it');
  175. $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');
  176. try
  177. {
  178. Builder::resolveValue('%foobar%', array());
  179. $t->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
  180. }
  181. catch (RuntimeException $e)
  182. {
  183. $t->pass('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
  184. }
  185. try
  186. {
  187. Builder::resolveValue('foo %foobar% bar', array());
  188. $t->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
  189. }
  190. catch (RuntimeException $e)
  191. {
  192. $t->pass('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter');
  193. }
  194. // ->resolveServices()
  195. $t->diag('->resolveServices()');
  196. $builder = new Builder();
  197. $builder->register('foo', 'FooClass');
  198. $t->is($builder->resolveServices(new Reference('foo')), $builder->getService('foo'), '->resolveServices() resolves service references to service instances');
  199. $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');
  200. // ->merge()
  201. $t->diag('->merge()');
  202. $container = new Builder();
  203. $container->merge(null);
  204. $t->is($container->getParameters(), array(), '->merge() accepts null as an argument');
  205. $t->is($container->getDefinitions(), array(), '->merge() accepts null as an argument');
  206. $container = new Builder(array('bar' => 'foo'));
  207. $config = new BuilderConfiguration();
  208. $config->setParameters(array('foo' => 'bar'));
  209. $container->merge($config);
  210. $t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'bar'), '->merge() merges current parameters with the loaded ones');
  211. $container = new Builder(array('bar' => 'foo', 'foo' => 'baz'));
  212. $config = new BuilderConfiguration();
  213. $config->setParameters(array('foo' => 'bar'));
  214. $container->merge($config);
  215. $t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'baz'), '->merge() does not change the already defined parameters');
  216. $container = new Builder(array('bar' => 'foo'));
  217. $config = new BuilderConfiguration();
  218. $config->setParameters(array('foo' => '%bar%'));
  219. $container->merge($config);
  220. $t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones');
  221. $container = new Builder(array('bar' => 'foo'));
  222. $config = new BuilderConfiguration();
  223. $config->setParameters(array('foo' => '%bar%', 'baz' => '%foo%'));
  224. $container->merge($config);
  225. $t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones');
  226. $container = new Builder();
  227. $container->register('foo', 'FooClass');
  228. $container->register('bar', 'BarClass');
  229. $config = new BuilderConfiguration();
  230. $config->setDefinition('baz', new Definition('BazClass'));
  231. $config->setAlias('alias_for_foo', 'foo');
  232. $container->merge($config);
  233. $t->is(array_keys($container->getDefinitions()), array('foo', 'bar', 'baz'), '->load() merges definitions already defined ones');
  234. $t->is($container->getAliases(), array('alias_for_foo' => 'foo'), '->merge() registers defined aliases');
  235. $container = new Builder();
  236. $container->register('foo', 'FooClass');
  237. $config->setDefinition('foo', new Definition('BazClass'));
  238. $container->merge($config);
  239. $t->is($container->getDefinition('foo')->getClass(), 'BazClass', '->merge() overrides already defined services');