BuilderTest.php 18 KB

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