BuilderTest.php 22 KB

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