BuilderTest.php 19 KB

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