DefinitionTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 Symfony\Component\DependencyInjection\Definition;
  12. class DefinitionTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @covers Symfony\Component\DependencyInjection\Definition::__construct
  16. */
  17. public function testConstructor()
  18. {
  19. $def = new Definition('stdClass');
  20. $this->assertEquals('stdClass', $def->getClass(), '__construct() takes the class name as its first argument');
  21. $def = new Definition('stdClass', array('foo'));
  22. $this->assertEquals(array('foo'), $def->getArguments(), '__construct() takes an optional array of arguments as its second argument');
  23. }
  24. public function testSetGetFactoryClass()
  25. {
  26. $def = new Definition('stdClass');
  27. $this->assertNull($def->getFactoryClass());
  28. $this->assertSame($def, $def->setFactoryClass('stdClass2'), "->setFactoryClass() implements a fluent interface.");
  29. $this->assertEquals('stdClass2', $def->getFactoryClass(), "->getFactoryClass() returns current class to construct this service.");
  30. }
  31. public function testSetGetFactoryMethod()
  32. {
  33. $def = new Definition('stdClass');
  34. $this->assertNull($def->getFactoryMethod());
  35. $this->assertSame($def, $def->setFactoryMethod('foo'), '->setFactoryMethod() implements a fluent interface');
  36. $this->assertEquals('foo', $def->getFactoryMethod(), '->getFactoryMethod() returns the factory method name');
  37. }
  38. public function testSetGetFactoryService()
  39. {
  40. $def = new Definition('stdClass');
  41. $this->assertNull($def->getFactoryService());
  42. $this->assertSame($def, $def->setFactoryService('foo.bar'), "->setFactoryService() implements a fluent interface.");
  43. $this->assertEquals('foo.bar', $def->getFactoryService(), "->getFactoryService() returns current service to construct this service.");
  44. }
  45. /**
  46. * @covers Symfony\Component\DependencyInjection\Definition::setClass
  47. * @covers Symfony\Component\DependencyInjection\Definition::getClass
  48. */
  49. public function testSetGetClass()
  50. {
  51. $def = new Definition('stdClass');
  52. $this->assertSame($def, $def->setClass('foo'), '->setClass() implements a fluent interface');
  53. $this->assertEquals('foo', $def->getClass(), '->getClass() returns the class name');
  54. }
  55. /**
  56. * @covers Symfony\Component\DependencyInjection\Definition::setArguments
  57. * @covers Symfony\Component\DependencyInjection\Definition::getArguments
  58. * @covers Symfony\Component\DependencyInjection\Definition::addArgument
  59. */
  60. public function testArguments()
  61. {
  62. $def = new Definition('stdClass');
  63. $this->assertSame($def, $def->setArguments(array('foo')), '->setArguments() implements a fluent interface');
  64. $this->assertEquals(array('foo'), $def->getArguments(), '->getArguments() returns the arguments');
  65. $this->assertSame($def, $def->addArgument('bar'), '->addArgument() implements a fluent interface');
  66. $this->assertEquals(array('foo', 'bar'), $def->getArguments(), '->addArgument() adds an argument');
  67. }
  68. /**
  69. * @covers Symfony\Component\DependencyInjection\Definition::setMethodCalls
  70. * @covers Symfony\Component\DependencyInjection\Definition::addMethodCall
  71. * @covers Symfony\Component\DependencyInjection\Definition::hasMethodCall
  72. * @covers Symfony\Component\DependencyInjection\Definition::removeMethodCall
  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. /**
  96. * @covers Symfony\Component\DependencyInjection\Definition::setFile
  97. * @covers Symfony\Component\DependencyInjection\Definition::getFile
  98. */
  99. public function testSetGetFile()
  100. {
  101. $def = new Definition('stdClass');
  102. $this->assertSame($def, $def->setFile('foo'), '->setFile() implements a fluent interface');
  103. $this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
  104. }
  105. /**
  106. * @covers Symfony\Component\DependencyInjection\Definition::setScope
  107. * @covers Symfony\Component\DependencyInjection\Definition::getScope
  108. */
  109. public function testSetGetScope()
  110. {
  111. $def = new Definition('stdClass');
  112. $this->assertEquals('container', $def->getScope());
  113. $this->assertSame($def, $def->setScope('foo'));
  114. $this->assertEquals('foo', $def->getScope());
  115. }
  116. /**
  117. * @covers Symfony\Component\DependencyInjection\Definition::setPublic
  118. * @covers Symfony\Component\DependencyInjection\Definition::isPublic
  119. */
  120. public function testSetIsPublic()
  121. {
  122. $def = new Definition('stdClass');
  123. $this->assertTrue($def->isPublic(), '->isPublic() returns true by default');
  124. $this->assertSame($def, $def->setPublic(false), '->setPublic() implements a fluent interface');
  125. $this->assertFalse($def->isPublic(), '->isPublic() returns false if the instance must not be public.');
  126. }
  127. /**
  128. * @covers Symfony\Component\DependencyInjection\Definition::setSynthetic
  129. * @covers Symfony\Component\DependencyInjection\Definition::isSynthetic
  130. */
  131. public function testSetIsSynthetic()
  132. {
  133. $def = new Definition('stdClass');
  134. $this->assertFalse($def->isSynthetic(), '->isSynthetic() returns false by default');
  135. $this->assertSame($def, $def->setSynthetic(true), '->setSynthetic() implements a fluent interface');
  136. $this->assertTrue($def->isSynthetic(), '->isSynthetic() returns true if the service is synthetic.');
  137. }
  138. /**
  139. * @covers Symfony\Component\DependencyInjection\Definition::setSynchronized
  140. * @covers Symfony\Component\DependencyInjection\Definition::isSynchronized
  141. */
  142. public function testSetIsSynchronized()
  143. {
  144. $def = new Definition('stdClass');
  145. $this->assertFalse($def->isSynchronized(), '->isSynchronized() returns false by default');
  146. $this->assertSame($def, $def->setSynchronized(true), '->setSynchronized() implements a fluent interface');
  147. $this->assertTrue($def->isSynchronized(), '->isSynchronized() returns true if the service is synchronized.');
  148. }
  149. /**
  150. * @covers Symfony\Component\DependencyInjection\Definition::setLazy
  151. * @covers Symfony\Component\DependencyInjection\Definition::isLazy
  152. */
  153. public function testSetIsLazy()
  154. {
  155. $def = new Definition('stdClass');
  156. $this->assertFalse($def->isLazy(), '->isLazy() returns false by default');
  157. $this->assertSame($def, $def->setLazy(true), '->setLazy() implements a fluent interface');
  158. $this->assertTrue($def->isLazy(), '->isLazy() returns true if the service is lazy.');
  159. }
  160. /**
  161. * @covers Symfony\Component\DependencyInjection\Definition::setAbstract
  162. * @covers Symfony\Component\DependencyInjection\Definition::isAbstract
  163. */
  164. public function testSetIsAbstract()
  165. {
  166. $def = new Definition('stdClass');
  167. $this->assertFalse($def->isAbstract(), '->isAbstract() returns false by default');
  168. $this->assertSame($def, $def->setAbstract(true), '->setAbstract() implements a fluent interface');
  169. $this->assertTrue($def->isAbstract(), '->isAbstract() returns true if the instance must not be public.');
  170. }
  171. /**
  172. * @covers Symfony\Component\DependencyInjection\Definition::setConfigurator
  173. * @covers Symfony\Component\DependencyInjection\Definition::getConfigurator
  174. */
  175. public function testSetGetConfigurator()
  176. {
  177. $def = new Definition('stdClass');
  178. $this->assertSame($def, $def->setConfigurator('foo'), '->setConfigurator() implements a fluent interface');
  179. $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
  180. }
  181. /**
  182. * @covers Symfony\Component\DependencyInjection\Definition::clearTags
  183. */
  184. public function testClearTags()
  185. {
  186. $def = new Definition('stdClass');
  187. $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
  188. $def->addTag('foo', array('foo' => 'bar'));
  189. $def->clearTags();
  190. $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
  191. }
  192. /**
  193. * @covers Symfony\Component\DependencyInjection\Definition::clearTags
  194. */
  195. public function testClearTag()
  196. {
  197. $def = new Definition('stdClass');
  198. $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
  199. $def->addTag('1foo1', array('foo1' => 'bar1'));
  200. $def->addTag('2foo2', array('foo2' => 'bar2'));
  201. $def->addTag('3foo3', array('foo3' => 'bar3'));
  202. $def->clearTag('2foo2');
  203. $this->assertTrue($def->hasTag('1foo1'));
  204. $this->assertFalse($def->hasTag('2foo2'));
  205. $this->assertTrue($def->hasTag('3foo3'));
  206. $def->clearTag('1foo1');
  207. $this->assertFalse($def->hasTag('1foo1'));
  208. $this->assertTrue($def->hasTag('3foo3'));
  209. }
  210. /**
  211. * @covers Symfony\Component\DependencyInjection\Definition::addTag
  212. * @covers Symfony\Component\DependencyInjection\Definition::getTag
  213. * @covers Symfony\Component\DependencyInjection\Definition::getTags
  214. * @covers Symfony\Component\DependencyInjection\Definition::hasTag
  215. */
  216. public function testTags()
  217. {
  218. $def = new Definition('stdClass');
  219. $this->assertEquals(array(), $def->getTag('foo'), '->getTag() returns an empty array if the tag is not defined');
  220. $this->assertFalse($def->hasTag('foo'));
  221. $this->assertSame($def, $def->addTag('foo'), '->addTag() implements a fluent interface');
  222. $this->assertTrue($def->hasTag('foo'));
  223. $this->assertEquals(array(array()), $def->getTag('foo'), '->getTag() returns attributes for a tag name');
  224. $def->addTag('foo', array('foo' => 'bar'));
  225. $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getTag('foo'), '->addTag() can adds the same tag several times');
  226. $def->addTag('bar', array('bar' => 'bar'));
  227. $this->assertEquals($def->getTags(), array(
  228. 'foo' => array(array(), array('foo' => 'bar')),
  229. 'bar' => array(array('bar' => 'bar')),
  230. ), '->getTags() returns all tags');
  231. }
  232. /**
  233. * @covers Symfony\Component\DependencyInjection\Definition::replaceArgument
  234. */
  235. public function testSetArgument()
  236. {
  237. $def = new Definition('stdClass');
  238. $def->addArgument('foo');
  239. $this->assertSame(array('foo'), $def->getArguments());
  240. $this->assertSame($def, $def->replaceArgument(0, 'moo'));
  241. $this->assertSame(array('moo'), $def->getArguments());
  242. $def->addArgument('moo');
  243. $def
  244. ->replaceArgument(0, 'foo')
  245. ->replaceArgument(1, 'bar')
  246. ;
  247. $this->assertSame(array('foo', 'bar'), $def->getArguments());
  248. }
  249. /**
  250. * @expectedException OutOfBoundsException
  251. */
  252. public function testGetArgumentShouldCheckBounds()
  253. {
  254. $def = new Definition('stdClass');
  255. $def->addArgument('foo');
  256. $def->getArgument(1);
  257. }
  258. /**
  259. * @expectedException OutOfBoundsException
  260. */
  261. public function testReplaceArgumentShouldCheckBounds()
  262. {
  263. $def = new Definition('stdClass');
  264. $def->addArgument('foo');
  265. $def->replaceArgument(1, 'bar');
  266. }
  267. public function testSetGetProperties()
  268. {
  269. $def = new Definition('stdClass');
  270. $this->assertEquals(array(), $def->getProperties());
  271. $this->assertSame($def, $def->setProperties(array('foo' => 'bar')));
  272. $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
  273. }
  274. public function testSetProperty()
  275. {
  276. $def = new Definition('stdClass');
  277. $this->assertEquals(array(), $def->getProperties());
  278. $this->assertSame($def, $def->setProperty('foo', 'bar'));
  279. $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
  280. }
  281. }