DefinitionTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\Definition;
  13. class DefinitionTest extends TestCase
  14. {
  15. public function testConstructor()
  16. {
  17. $def = new Definition('stdClass');
  18. $this->assertEquals('stdClass', $def->getClass(), '__construct() takes the class name as its first argument');
  19. $this->assertSame(array('class' => true), $def->getChanges());
  20. $def = new Definition('stdClass', array('foo'));
  21. $this->assertEquals(array('foo'), $def->getArguments(), '__construct() takes an optional array of arguments as its second argument');
  22. }
  23. public function testSetGetFactory()
  24. {
  25. $def = new Definition();
  26. $this->assertSame($def, $def->setFactory('foo'), '->setFactory() implements a fluent interface');
  27. $this->assertEquals('foo', $def->getFactory(), '->getFactory() returns the factory');
  28. $def->setFactory('Foo::bar');
  29. $this->assertEquals(array('Foo', 'bar'), $def->getFactory(), '->setFactory() converts string static method call to the array');
  30. $this->assertSame(array('factory' => true), $def->getChanges());
  31. }
  32. public function testSetGetClass()
  33. {
  34. $def = new Definition('stdClass');
  35. $this->assertSame($def, $def->setClass('foo'), '->setClass() implements a fluent interface');
  36. $this->assertEquals('foo', $def->getClass(), '->getClass() returns the class name');
  37. }
  38. public function testSetGetDecoratedService()
  39. {
  40. $def = new Definition('stdClass');
  41. $this->assertNull($def->getDecoratedService());
  42. $def->setDecoratedService('foo', 'foo.renamed', 5);
  43. $this->assertEquals(array('foo', 'foo.renamed', 5), $def->getDecoratedService());
  44. $def->setDecoratedService(null);
  45. $this->assertNull($def->getDecoratedService());
  46. $def = new Definition('stdClass');
  47. $this->assertNull($def->getDecoratedService());
  48. $def->setDecoratedService('foo', 'foo.renamed');
  49. $this->assertEquals(array('foo', 'foo.renamed', 0), $def->getDecoratedService());
  50. $def->setDecoratedService(null);
  51. $this->assertNull($def->getDecoratedService());
  52. $def = new Definition('stdClass');
  53. $def->setDecoratedService('foo');
  54. $this->assertEquals(array('foo', null, 0), $def->getDecoratedService());
  55. $def->setDecoratedService(null);
  56. $this->assertNull($def->getDecoratedService());
  57. $def = new Definition('stdClass');
  58. if (method_exists($this, 'expectException')) {
  59. $this->expectException('InvalidArgumentException');
  60. $this->expectExceptionMessage('The decorated service inner name for "foo" must be different than the service name itself.');
  61. } else {
  62. $this->setExpectedException('InvalidArgumentException', 'The decorated service inner name for "foo" must be different than the service name itself.');
  63. }
  64. $def->setDecoratedService('foo', 'foo');
  65. }
  66. public function testArguments()
  67. {
  68. $def = new Definition('stdClass');
  69. $this->assertSame($def, $def->setArguments(array('foo')), '->setArguments() implements a fluent interface');
  70. $this->assertEquals(array('foo'), $def->getArguments(), '->getArguments() returns the arguments');
  71. $this->assertSame($def, $def->addArgument('bar'), '->addArgument() implements a fluent interface');
  72. $this->assertEquals(array('foo', 'bar'), $def->getArguments(), '->addArgument() adds an argument');
  73. }
  74. public function testMethodCalls()
  75. {
  76. $def = new Definition('stdClass');
  77. $this->assertSame($def, $def->setMethodCalls(array(array('foo', array('foo')))), '->setMethodCalls() implements a fluent interface');
  78. $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->getMethodCalls() returns the methods to call');
  79. $this->assertSame($def, $def->addMethodCall('bar', array('bar')), '->addMethodCall() implements a fluent interface');
  80. $this->assertEquals(array(array('foo', array('foo')), array('bar', array('bar'))), $def->getMethodCalls(), '->addMethodCall() adds a method to call');
  81. $this->assertTrue($def->hasMethodCall('bar'), '->hasMethodCall() returns true if first argument is a method to call registered');
  82. $this->assertFalse($def->hasMethodCall('no_registered'), '->hasMethodCall() returns false if first argument is not a method to call registered');
  83. $this->assertSame($def, $def->removeMethodCall('bar'), '->removeMethodCall() implements a fluent interface');
  84. $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->removeMethodCall() removes a method to call');
  85. }
  86. /**
  87. * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  88. * @expectedExceptionMessage Method name cannot be empty.
  89. */
  90. public function testExceptionOnEmptyMethodCall()
  91. {
  92. $def = new Definition('stdClass');
  93. $def->addMethodCall('');
  94. }
  95. public function testSetGetFile()
  96. {
  97. $def = new Definition('stdClass');
  98. $this->assertSame($def, $def->setFile('foo'), '->setFile() implements a fluent interface');
  99. $this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
  100. }
  101. public function testSetIsShared()
  102. {
  103. $def = new Definition('stdClass');
  104. $this->assertTrue($def->isShared(), '->isShared() returns true by default');
  105. $this->assertSame($def, $def->setShared(false), '->setShared() implements a fluent interface');
  106. $this->assertFalse($def->isShared(), '->isShared() returns false if the instance must not be shared');
  107. }
  108. public function testSetIsPublic()
  109. {
  110. $def = new Definition('stdClass');
  111. $this->assertTrue($def->isPublic(), '->isPublic() returns true by default');
  112. $this->assertSame($def, $def->setPublic(false), '->setPublic() implements a fluent interface');
  113. $this->assertFalse($def->isPublic(), '->isPublic() returns false if the instance must not be public.');
  114. }
  115. public function testSetIsSynthetic()
  116. {
  117. $def = new Definition('stdClass');
  118. $this->assertFalse($def->isSynthetic(), '->isSynthetic() returns false by default');
  119. $this->assertSame($def, $def->setSynthetic(true), '->setSynthetic() implements a fluent interface');
  120. $this->assertTrue($def->isSynthetic(), '->isSynthetic() returns true if the service is synthetic.');
  121. }
  122. public function testSetIsLazy()
  123. {
  124. $def = new Definition('stdClass');
  125. $this->assertFalse($def->isLazy(), '->isLazy() returns false by default');
  126. $this->assertSame($def, $def->setLazy(true), '->setLazy() implements a fluent interface');
  127. $this->assertTrue($def->isLazy(), '->isLazy() returns true if the service is lazy.');
  128. }
  129. public function testSetIsAbstract()
  130. {
  131. $def = new Definition('stdClass');
  132. $this->assertFalse($def->isAbstract(), '->isAbstract() returns false by default');
  133. $this->assertSame($def, $def->setAbstract(true), '->setAbstract() implements a fluent interface');
  134. $this->assertTrue($def->isAbstract(), '->isAbstract() returns true if the instance must not be public.');
  135. }
  136. public function testSetIsDeprecated()
  137. {
  138. $def = new Definition('stdClass');
  139. $this->assertFalse($def->isDeprecated(), '->isDeprecated() returns false by default');
  140. $this->assertSame($def, $def->setDeprecated(true), '->setDeprecated() implements a fluent interface');
  141. $this->assertTrue($def->isDeprecated(), '->isDeprecated() returns true if the instance should not be used anymore.');
  142. $this->assertSame('The "deprecated_service" service is deprecated. You should stop using it, as it will soon be removed.', $def->getDeprecationMessage('deprecated_service'), '->getDeprecationMessage() should return a formatted message template');
  143. }
  144. /**
  145. * @dataProvider invalidDeprecationMessageProvider
  146. * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  147. */
  148. public function testSetDeprecatedWithInvalidDeprecationTemplate($message)
  149. {
  150. $def = new Definition('stdClass');
  151. $def->setDeprecated(false, $message);
  152. }
  153. public function invalidDeprecationMessageProvider()
  154. {
  155. return array(
  156. "With \rs" => array("invalid \r message %service_id%"),
  157. "With \ns" => array("invalid \n message %service_id%"),
  158. 'With */s' => array('invalid */ message %service_id%'),
  159. 'message not containing require %service_id% variable' => array('this is deprecated'),
  160. );
  161. }
  162. public function testSetGetConfigurator()
  163. {
  164. $def = new Definition('stdClass');
  165. $this->assertSame($def, $def->setConfigurator('foo'), '->setConfigurator() implements a fluent interface');
  166. $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
  167. }
  168. public function testClearTags()
  169. {
  170. $def = new Definition('stdClass');
  171. $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
  172. $def->addTag('foo', array('foo' => 'bar'));
  173. $def->clearTags();
  174. $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
  175. }
  176. public function testClearTag()
  177. {
  178. $def = new Definition('stdClass');
  179. $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
  180. $def->addTag('1foo1', array('foo1' => 'bar1'));
  181. $def->addTag('2foo2', array('foo2' => 'bar2'));
  182. $def->addTag('3foo3', array('foo3' => 'bar3'));
  183. $def->clearTag('2foo2');
  184. $this->assertTrue($def->hasTag('1foo1'));
  185. $this->assertFalse($def->hasTag('2foo2'));
  186. $this->assertTrue($def->hasTag('3foo3'));
  187. $def->clearTag('1foo1');
  188. $this->assertFalse($def->hasTag('1foo1'));
  189. $this->assertTrue($def->hasTag('3foo3'));
  190. }
  191. public function testTags()
  192. {
  193. $def = new Definition('stdClass');
  194. $this->assertEquals(array(), $def->getTag('foo'), '->getTag() returns an empty array if the tag is not defined');
  195. $this->assertFalse($def->hasTag('foo'));
  196. $this->assertSame($def, $def->addTag('foo'), '->addTag() implements a fluent interface');
  197. $this->assertTrue($def->hasTag('foo'));
  198. $this->assertEquals(array(array()), $def->getTag('foo'), '->getTag() returns attributes for a tag name');
  199. $def->addTag('foo', array('foo' => 'bar'));
  200. $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getTag('foo'), '->addTag() can adds the same tag several times');
  201. $def->addTag('bar', array('bar' => 'bar'));
  202. $this->assertEquals($def->getTags(), array(
  203. 'foo' => array(array(), array('foo' => 'bar')),
  204. 'bar' => array(array('bar' => 'bar')),
  205. ), '->getTags() returns all tags');
  206. }
  207. public function testSetArgument()
  208. {
  209. $def = new Definition('stdClass');
  210. $def->addArgument('foo');
  211. $this->assertSame(array('foo'), $def->getArguments());
  212. $this->assertSame($def, $def->replaceArgument(0, 'moo'));
  213. $this->assertSame(array('moo'), $def->getArguments());
  214. $def->addArgument('moo');
  215. $def
  216. ->replaceArgument(0, 'foo')
  217. ->replaceArgument(1, 'bar')
  218. ;
  219. $this->assertSame(array('foo', 'bar'), $def->getArguments());
  220. }
  221. /**
  222. * @expectedException \OutOfBoundsException
  223. */
  224. public function testGetArgumentShouldCheckBounds()
  225. {
  226. $def = new Definition('stdClass');
  227. $def->addArgument('foo');
  228. $def->getArgument(1);
  229. }
  230. /**
  231. * @expectedException \OutOfBoundsException
  232. * @expectedExceptionMessage The index "1" is not in the range [0, 0].
  233. */
  234. public function testReplaceArgumentShouldCheckBounds()
  235. {
  236. $def = new Definition('stdClass');
  237. $def->addArgument('foo');
  238. $def->replaceArgument(1, 'bar');
  239. }
  240. /**
  241. * @expectedException \OutOfBoundsException
  242. * @expectedExceptionMessage Cannot replace arguments if none have been configured yet.
  243. */
  244. public function testReplaceArgumentWithoutExistingArgumentsShouldCheckBounds()
  245. {
  246. $def = new Definition('stdClass');
  247. $def->replaceArgument(0, 'bar');
  248. }
  249. public function testSetGetProperties()
  250. {
  251. $def = new Definition('stdClass');
  252. $this->assertEquals(array(), $def->getProperties());
  253. $this->assertSame($def, $def->setProperties(array('foo' => 'bar')));
  254. $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
  255. }
  256. public function testSetProperty()
  257. {
  258. $def = new Definition('stdClass');
  259. $this->assertEquals(array(), $def->getProperties());
  260. $this->assertSame($def, $def->setProperty('foo', 'bar'));
  261. $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
  262. }
  263. public function testAutowired()
  264. {
  265. $def = new Definition('stdClass');
  266. $this->assertFalse($def->isAutowired());
  267. $def->setAutowired(true);
  268. $this->assertTrue($def->isAutowired());
  269. $def->setAutowired(false);
  270. $this->assertFalse($def->isAutowired());
  271. }
  272. public function testChangesNoChanges()
  273. {
  274. $def = new Definition();
  275. $this->assertSame(array(), $def->getChanges());
  276. }
  277. public function testGetChangesWithChanges()
  278. {
  279. $def = new Definition('stdClass', array('fooarg'));
  280. $def->setAbstract(true);
  281. $def->setAutowired(true);
  282. $def->setConfigurator('configuration_func');
  283. $def->setDecoratedService(null);
  284. $def->setDeprecated(true);
  285. $def->setFactory('factory_func');
  286. $def->setFile('foo.php');
  287. $def->setLazy(true);
  288. $def->setPublic(true);
  289. $def->setShared(true);
  290. $def->setSynthetic(true);
  291. // changes aren't tracked for these, class or arguments
  292. $def->setInstanceofConditionals(array());
  293. $def->addTag('foo_tag');
  294. $def->addMethodCall('methodCall');
  295. $def->setProperty('fooprop', true);
  296. $def->setAutoconfigured(true);
  297. $this->assertSame(array(
  298. 'class' => true,
  299. 'autowired' => true,
  300. 'configurator' => true,
  301. 'decorated_service' => true,
  302. 'deprecated' => true,
  303. 'factory' => true,
  304. 'file' => true,
  305. 'lazy' => true,
  306. 'public' => true,
  307. 'shared' => true,
  308. 'autoconfigured' => true,
  309. ), $def->getChanges());
  310. $def->setChanges(array());
  311. $this->assertSame(array(), $def->getChanges());
  312. }
  313. /**
  314. * @group legacy
  315. */
  316. public function testTypes()
  317. {
  318. $def = new Definition('stdClass');
  319. $this->assertEquals(array(), $def->getAutowiringTypes());
  320. $this->assertSame($def, $def->setAutowiringTypes(array('Foo')));
  321. $this->assertEquals(array('Foo'), $def->getAutowiringTypes());
  322. $this->assertSame($def, $def->addAutowiringType('Bar'));
  323. $this->assertTrue($def->hasAutowiringType('Bar'));
  324. $this->assertSame($def, $def->removeAutowiringType('Foo'));
  325. $this->assertEquals(array('Bar'), $def->getAutowiringTypes());
  326. }
  327. public function testShouldAutoconfigure()
  328. {
  329. $def = new Definition('stdClass');
  330. $this->assertFalse($def->isAutoconfigured());
  331. $def->setAutoconfigured(true);
  332. $this->assertTrue($def->isAutoconfigured());
  333. }
  334. }