DefinitionTest.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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\Tests\Component\DependencyInjection;
  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 instance must not be public.');
  137. }
  138. /**
  139. * @covers Symfony\Component\DependencyInjection\Definition::setAbstract
  140. * @covers Symfony\Component\DependencyInjection\Definition::isAbstract
  141. */
  142. public function testSetIsAbstract()
  143. {
  144. $def = new Definition('stdClass');
  145. $this->assertFalse($def->isAbstract(), '->isAbstract() returns false by default');
  146. $this->assertSame($def, $def->setAbstract(true), '->setAbstract() implements a fluent interface');
  147. $this->assertTrue($def->isAbstract(), '->isAbstract() returns true if the instance must not be public.');
  148. }
  149. /**
  150. * @covers Symfony\Component\DependencyInjection\Definition::setConfigurator
  151. * @covers Symfony\Component\DependencyInjection\Definition::getConfigurator
  152. */
  153. public function testSetGetConfigurator()
  154. {
  155. $def = new Definition('stdClass');
  156. $this->assertSame($def, $def->setConfigurator('foo'), '->setConfigurator() implements a fluent interface');
  157. $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
  158. }
  159. /**
  160. * @covers Symfony\Component\DependencyInjection\Definition::clearTags
  161. */
  162. public function testClearTags()
  163. {
  164. $def = new Definition('stdClass');
  165. $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
  166. $def->addTag('foo', array('foo' => 'bar'));
  167. $def->clearTags();
  168. $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
  169. }
  170. /**
  171. * @covers Symfony\Component\DependencyInjection\Definition::addTag
  172. * @covers Symfony\Component\DependencyInjection\Definition::getTag
  173. * @covers Symfony\Component\DependencyInjection\Definition::getTags
  174. * @covers Symfony\Component\DependencyInjection\Definition::hasTag
  175. */
  176. public function testTags()
  177. {
  178. $def = new Definition('stdClass');
  179. $this->assertEquals(array(), $def->getTag('foo'), '->getTag() returns an empty array if the tag is not defined');
  180. $this->assertFalse($def->hasTag('foo'));
  181. $this->assertSame($def, $def->addTag('foo'), '->addTag() implements a fluent interface');
  182. $this->assertTrue($def->hasTag('foo'));
  183. $this->assertEquals(array(array()), $def->getTag('foo'), '->getTag() returns attributes for a tag name');
  184. $def->addTag('foo', array('foo' => 'bar'));
  185. $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getTag('foo'), '->addTag() can adds the same tag several times');
  186. $def->addTag('bar', array('bar' => 'bar'));
  187. $this->assertEquals($def->getTags(), array(
  188. 'foo' => array(array(), array('foo' => 'bar')),
  189. 'bar' => array(array('bar' => 'bar')),
  190. ), '->getTags() returns all tags');
  191. }
  192. /**
  193. * @covers Symfony\Component\DependencyInjection\Definition::replaceArgument
  194. */
  195. public function testSetArgument()
  196. {
  197. $def = new Definition('stdClass');
  198. $def->addArgument('foo');
  199. $this->assertSame(array('foo'), $def->getArguments());
  200. $this->assertSame($def, $def->replaceArgument(0, 'moo'));
  201. $this->assertSame(array('moo'), $def->getArguments());
  202. $def->addArgument('moo');
  203. $def
  204. ->replaceArgument(0, 'foo')
  205. ->replaceArgument(1, 'bar')
  206. ;
  207. $this->assertSame(array('foo', 'bar'), $def->getArguments());
  208. }
  209. /**
  210. * @expectedException OutOfBoundsException
  211. */
  212. public function testGetArgumentShouldCheckBounds()
  213. {
  214. $def = new Definition('stdClass');
  215. $def->addArgument('foo');
  216. $def->getArgument(1);
  217. }
  218. /**
  219. * @expectedException OutOfBoundsException
  220. */
  221. public function testReplaceArgumentShouldCheckBounds()
  222. {
  223. $def = new Definition('stdClass');
  224. $def->addArgument('foo');
  225. $def->replaceArgument(1, 'bar');
  226. }
  227. public function testSetGetProperties()
  228. {
  229. $def = new Definition('stdClass');
  230. $this->assertEquals(array(), $def->getProperties());
  231. $this->assertSame($def, $def->setProperties(array('foo' => 'bar')));
  232. $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
  233. }
  234. public function testSetProperty()
  235. {
  236. $def = new Definition('stdClass');
  237. $this->assertEquals(array(), $def->getProperties());
  238. $this->assertSame($def, $def->setProperty('foo', 'bar'));
  239. $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
  240. }
  241. }