DefinitionTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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\ContainerInterface;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. class DefinitionTest extends TestCase
  15. {
  16. public function testConstructor()
  17. {
  18. $def = new Definition('stdClass');
  19. $this->assertEquals('stdClass', $def->getClass(), '__construct() takes the class name as its first argument');
  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('stdClass');
  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. }
  31. public function testSetGetClass()
  32. {
  33. $def = new Definition('stdClass');
  34. $this->assertSame($def, $def->setClass('foo'), '->setClass() implements a fluent interface');
  35. $this->assertEquals('foo', $def->getClass(), '->getClass() returns the class name');
  36. }
  37. public function testSetGetDecoratedService()
  38. {
  39. $def = new Definition('stdClass');
  40. $this->assertNull($def->getDecoratedService());
  41. $def->setDecoratedService('foo', 'foo.renamed', 5);
  42. $this->assertEquals(array('foo', 'foo.renamed', 5), $def->getDecoratedService());
  43. $def->setDecoratedService(null);
  44. $this->assertNull($def->getDecoratedService());
  45. $def = new Definition('stdClass');
  46. $this->assertNull($def->getDecoratedService());
  47. $def->setDecoratedService('foo', 'foo.renamed');
  48. $this->assertEquals(array('foo', 'foo.renamed', 0), $def->getDecoratedService());
  49. $def->setDecoratedService(null);
  50. $this->assertNull($def->getDecoratedService());
  51. $def = new Definition('stdClass');
  52. $def->setDecoratedService('foo');
  53. $this->assertEquals(array('foo', null, 0), $def->getDecoratedService());
  54. $def->setDecoratedService(null);
  55. $this->assertNull($def->getDecoratedService());
  56. $def = new Definition('stdClass');
  57. if (method_exists($this, 'expectException')) {
  58. $this->expectException('InvalidArgumentException');
  59. $this->expectExceptionMessage('The decorated service inner name for "foo" must be different than the service name itself.');
  60. } else {
  61. $this->setExpectedException('InvalidArgumentException', 'The decorated service inner name for "foo" must be different than the service name itself.');
  62. }
  63. $def->setDecoratedService('foo', 'foo');
  64. }
  65. public function testArguments()
  66. {
  67. $def = new Definition('stdClass');
  68. $this->assertSame($def, $def->setArguments(array('foo')), '->setArguments() implements a fluent interface');
  69. $this->assertEquals(array('foo'), $def->getArguments(), '->getArguments() returns the arguments');
  70. $this->assertSame($def, $def->addArgument('bar'), '->addArgument() implements a fluent interface');
  71. $this->assertEquals(array('foo', 'bar'), $def->getArguments(), '->addArgument() adds an argument');
  72. }
  73. public function testMethodCalls()
  74. {
  75. $def = new Definition('stdClass');
  76. $this->assertSame($def, $def->setMethodCalls(array(array('foo', array('foo')))), '->setMethodCalls() implements a fluent interface');
  77. $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->getMethodCalls() returns the methods to call');
  78. $this->assertSame($def, $def->addMethodCall('bar', array('bar')), '->addMethodCall() implements a fluent interface');
  79. $this->assertEquals(array(array('foo', array('foo')), array('bar', array('bar'))), $def->getMethodCalls(), '->addMethodCall() adds a method to call');
  80. $this->assertTrue($def->hasMethodCall('bar'), '->hasMethodCall() returns true if first argument is a method to call registered');
  81. $this->assertFalse($def->hasMethodCall('no_registered'), '->hasMethodCall() returns false if first argument is not a method to call registered');
  82. $this->assertSame($def, $def->removeMethodCall('bar'), '->removeMethodCall() implements a fluent interface');
  83. $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->removeMethodCall() removes a method to call');
  84. }
  85. /**
  86. * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  87. * @expectedExceptionMessage Method name cannot be empty.
  88. */
  89. public function testExceptionOnEmptyMethodCall()
  90. {
  91. $def = new Definition('stdClass');
  92. $def->addMethodCall('');
  93. }
  94. public function testSetGetFile()
  95. {
  96. $def = new Definition('stdClass');
  97. $this->assertSame($def, $def->setFile('foo'), '->setFile() implements a fluent interface');
  98. $this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
  99. }
  100. public function testSetIsShared()
  101. {
  102. $def = new Definition('stdClass');
  103. $this->assertTrue($def->isShared(), '->isShared() returns true by default');
  104. $this->assertSame($def, $def->setShared(false), '->setShared() implements a fluent interface');
  105. $this->assertFalse($def->isShared(), '->isShared() returns false if the instance must not be shared');
  106. }
  107. /**
  108. * @group legacy
  109. */
  110. public function testPrototypeScopedDefinitionAreNotShared()
  111. {
  112. $def = new Definition('stdClass');
  113. $def->setScope(ContainerInterface::SCOPE_PROTOTYPE);
  114. $this->assertFalse($def->isShared());
  115. $this->assertEquals(ContainerInterface::SCOPE_PROTOTYPE, $def->getScope());
  116. }
  117. /**
  118. * @group legacy
  119. */
  120. public function testSetGetScope()
  121. {
  122. $def = new Definition('stdClass');
  123. $this->assertEquals('container', $def->getScope());
  124. $this->assertSame($def, $def->setScope('foo'));
  125. $this->assertEquals('foo', $def->getScope());
  126. }
  127. public function testSetIsPublic()
  128. {
  129. $def = new Definition('stdClass');
  130. $this->assertTrue($def->isPublic(), '->isPublic() returns true by default');
  131. $this->assertSame($def, $def->setPublic(false), '->setPublic() implements a fluent interface');
  132. $this->assertFalse($def->isPublic(), '->isPublic() returns false if the instance must not be public.');
  133. }
  134. public function testSetIsSynthetic()
  135. {
  136. $def = new Definition('stdClass');
  137. $this->assertFalse($def->isSynthetic(), '->isSynthetic() returns false by default');
  138. $this->assertSame($def, $def->setSynthetic(true), '->setSynthetic() implements a fluent interface');
  139. $this->assertTrue($def->isSynthetic(), '->isSynthetic() returns true if the service is synthetic.');
  140. }
  141. /**
  142. * @group legacy
  143. */
  144. public function testLegacySetIsSynchronized()
  145. {
  146. $def = new Definition('stdClass');
  147. $this->assertFalse($def->isSynchronized(), '->isSynchronized() returns false by default');
  148. $this->assertSame($def, $def->setSynchronized(true), '->setSynchronized() implements a fluent interface');
  149. $this->assertTrue($def->isSynchronized(), '->isSynchronized() returns true if the service is synchronized.');
  150. }
  151. public function testSetIsLazy()
  152. {
  153. $def = new Definition('stdClass');
  154. $this->assertFalse($def->isLazy(), '->isLazy() returns false by default');
  155. $this->assertSame($def, $def->setLazy(true), '->setLazy() implements a fluent interface');
  156. $this->assertTrue($def->isLazy(), '->isLazy() returns true if the service is lazy.');
  157. }
  158. public function testSetIsAbstract()
  159. {
  160. $def = new Definition('stdClass');
  161. $this->assertFalse($def->isAbstract(), '->isAbstract() returns false by default');
  162. $this->assertSame($def, $def->setAbstract(true), '->setAbstract() implements a fluent interface');
  163. $this->assertTrue($def->isAbstract(), '->isAbstract() returns true if the instance must not be public.');
  164. }
  165. public function testSetIsDeprecated()
  166. {
  167. $def = new Definition('stdClass');
  168. $this->assertFalse($def->isDeprecated(), '->isDeprecated() returns false by default');
  169. $this->assertSame($def, $def->setDeprecated(true), '->setDeprecated() implements a fluent interface');
  170. $this->assertTrue($def->isDeprecated(), '->isDeprecated() returns true if the instance should not be used anymore.');
  171. $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');
  172. }
  173. /**
  174. * @dataProvider invalidDeprecationMessageProvider
  175. * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  176. */
  177. public function testSetDeprecatedWithInvalidDeprecationTemplate($message)
  178. {
  179. $def = new Definition('stdClass');
  180. $def->setDeprecated(false, $message);
  181. }
  182. public function invalidDeprecationMessageProvider()
  183. {
  184. return array(
  185. "With \rs" => array("invalid \r message %service_id%"),
  186. "With \ns" => array("invalid \n message %service_id%"),
  187. 'With */s' => array('invalid */ message %service_id%'),
  188. 'message not containing require %service_id% variable' => array('this is deprecated'),
  189. );
  190. }
  191. public function testSetGetConfigurator()
  192. {
  193. $def = new Definition('stdClass');
  194. $this->assertSame($def, $def->setConfigurator('foo'), '->setConfigurator() implements a fluent interface');
  195. $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
  196. }
  197. public function testClearTags()
  198. {
  199. $def = new Definition('stdClass');
  200. $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
  201. $def->addTag('foo', array('foo' => 'bar'));
  202. $def->clearTags();
  203. $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
  204. }
  205. public function testClearTag()
  206. {
  207. $def = new Definition('stdClass');
  208. $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
  209. $def->addTag('1foo1', array('foo1' => 'bar1'));
  210. $def->addTag('2foo2', array('foo2' => 'bar2'));
  211. $def->addTag('3foo3', array('foo3' => 'bar3'));
  212. $def->clearTag('2foo2');
  213. $this->assertTrue($def->hasTag('1foo1'));
  214. $this->assertFalse($def->hasTag('2foo2'));
  215. $this->assertTrue($def->hasTag('3foo3'));
  216. $def->clearTag('1foo1');
  217. $this->assertFalse($def->hasTag('1foo1'));
  218. $this->assertTrue($def->hasTag('3foo3'));
  219. }
  220. public function testTags()
  221. {
  222. $def = new Definition('stdClass');
  223. $this->assertEquals(array(), $def->getTag('foo'), '->getTag() returns an empty array if the tag is not defined');
  224. $this->assertFalse($def->hasTag('foo'));
  225. $this->assertSame($def, $def->addTag('foo'), '->addTag() implements a fluent interface');
  226. $this->assertTrue($def->hasTag('foo'));
  227. $this->assertEquals(array(array()), $def->getTag('foo'), '->getTag() returns attributes for a tag name');
  228. $def->addTag('foo', array('foo' => 'bar'));
  229. $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getTag('foo'), '->addTag() can adds the same tag several times');
  230. $def->addTag('bar', array('bar' => 'bar'));
  231. $this->assertEquals($def->getTags(), array(
  232. 'foo' => array(array(), array('foo' => 'bar')),
  233. 'bar' => array(array('bar' => 'bar')),
  234. ), '->getTags() returns all tags');
  235. }
  236. public function testSetArgument()
  237. {
  238. $def = new Definition('stdClass');
  239. $def->addArgument('foo');
  240. $this->assertSame(array('foo'), $def->getArguments());
  241. $this->assertSame($def, $def->replaceArgument(0, 'moo'));
  242. $this->assertSame(array('moo'), $def->getArguments());
  243. $def->addArgument('moo');
  244. $def
  245. ->replaceArgument(0, 'foo')
  246. ->replaceArgument(1, 'bar')
  247. ;
  248. $this->assertSame(array('foo', 'bar'), $def->getArguments());
  249. }
  250. /**
  251. * @expectedException \OutOfBoundsException
  252. */
  253. public function testGetArgumentShouldCheckBounds()
  254. {
  255. $def = new Definition('stdClass');
  256. $def->addArgument('foo');
  257. $def->getArgument(1);
  258. }
  259. /**
  260. * @expectedException \OutOfBoundsException
  261. * @expectedExceptionMessage The index "1" is not in the range [0, 0].
  262. */
  263. public function testReplaceArgumentShouldCheckBounds()
  264. {
  265. $def = new Definition('stdClass');
  266. $def->addArgument('foo');
  267. $def->replaceArgument(1, 'bar');
  268. }
  269. /**
  270. * @expectedException \OutOfBoundsException
  271. * @expectedExceptionMessage Cannot replace arguments if none have been configured yet.
  272. */
  273. public function testReplaceArgumentWithoutExistingArgumentsShouldCheckBounds()
  274. {
  275. $def = new Definition('stdClass');
  276. $def->replaceArgument(0, 'bar');
  277. }
  278. public function testSetGetProperties()
  279. {
  280. $def = new Definition('stdClass');
  281. $this->assertEquals(array(), $def->getProperties());
  282. $this->assertSame($def, $def->setProperties(array('foo' => 'bar')));
  283. $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
  284. }
  285. public function testSetProperty()
  286. {
  287. $def = new Definition('stdClass');
  288. $this->assertEquals(array(), $def->getProperties());
  289. $this->assertSame($def, $def->setProperty('foo', 'bar'));
  290. $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
  291. }
  292. public function testAutowired()
  293. {
  294. $def = new Definition('stdClass');
  295. $this->assertFalse($def->isAutowired());
  296. $def->setAutowired(true);
  297. $this->assertTrue($def->isAutowired());
  298. }
  299. public function testTypes()
  300. {
  301. $def = new Definition('stdClass');
  302. $this->assertEquals(array(), $def->getAutowiringTypes());
  303. $this->assertSame($def, $def->setAutowiringTypes(array('Foo')));
  304. $this->assertEquals(array('Foo'), $def->getAutowiringTypes());
  305. $this->assertSame($def, $def->addAutowiringType('Bar'));
  306. $this->assertTrue($def->hasAutowiringType('Bar'));
  307. $this->assertSame($def, $def->removeAutowiringType('Foo'));
  308. $this->assertEquals(array('Bar'), $def->getAutowiringTypes());
  309. }
  310. }