BuilderTest.php 16 KB

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