BuilderTest.php 16 KB

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