DefinitionTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. * @covers Symfony\Component\DependencyInjection\Definition::setFile
  88. * @covers Symfony\Component\DependencyInjection\Definition::getFile
  89. */
  90. public function testSetGetFile()
  91. {
  92. $def = new Definition('stdClass');
  93. $this->assertSame($def, $def->setFile('foo'), '->setFile() implements a fluent interface');
  94. $this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
  95. }
  96. /**
  97. * @covers Symfony\Component\DependencyInjection\Definition::setScope
  98. * @covers Symfony\Component\DependencyInjection\Definition::getScope
  99. */
  100. public function testSetGetScope()
  101. {
  102. $def = new Definition('stdClass');
  103. $this->assertEquals('container', $def->getScope());
  104. $this->assertSame($def, $def->setScope('foo'));
  105. $this->assertEquals('foo', $def->getScope());
  106. }
  107. /**
  108. * @covers Symfony\Component\DependencyInjection\Definition::setPublic
  109. * @covers Symfony\Component\DependencyInjection\Definition::isPublic
  110. */
  111. public function testSetIsPublic()
  112. {
  113. $def = new Definition('stdClass');
  114. $this->assertTrue($def->isPublic(), '->isPublic() returns true by default');
  115. $this->assertSame($def, $def->setPublic(false), '->setPublic() implements a fluent interface');
  116. $this->assertFalse($def->isPublic(), '->isPublic() returns false if the instance must not be public.');
  117. }
  118. /**
  119. * @covers Symfony\Component\DependencyInjection\Definition::setSynthetic
  120. * @covers Symfony\Component\DependencyInjection\Definition::isSynthetic
  121. */
  122. public function testSetIsSynthetic()
  123. {
  124. $def = new Definition('stdClass');
  125. $this->assertFalse($def->isSynthetic(), '->isSynthetic() returns false by default');
  126. $this->assertSame($def, $def->setSynthetic(true), '->setSynthetic() implements a fluent interface');
  127. $this->assertTrue($def->isSynthetic(), '->isSynthetic() returns true if the instance must not be public.');
  128. }
  129. /**
  130. * @covers Symfony\Component\DependencyInjection\Definition::setAbstract
  131. * @covers Symfony\Component\DependencyInjection\Definition::isAbstract
  132. */
  133. public function testSetIsAbstract()
  134. {
  135. $def = new Definition('stdClass');
  136. $this->assertFalse($def->isAbstract(), '->isAbstract() returns false by default');
  137. $this->assertSame($def, $def->setAbstract(true), '->setAbstract() implements a fluent interface');
  138. $this->assertTrue($def->isAbstract(), '->isAbstract() returns true if the instance must not be public.');
  139. }
  140. /**
  141. * @covers Symfony\Component\DependencyInjection\Definition::setConfigurator
  142. * @covers Symfony\Component\DependencyInjection\Definition::getConfigurator
  143. */
  144. public function testSetGetConfigurator()
  145. {
  146. $def = new Definition('stdClass');
  147. $this->assertSame($def, $def->setConfigurator('foo'), '->setConfigurator() implements a fluent interface');
  148. $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
  149. }
  150. /**
  151. * @covers Symfony\Component\DependencyInjection\Definition::clearTags
  152. */
  153. public function testClearTags()
  154. {
  155. $def = new Definition('stdClass');
  156. $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
  157. $def->addTag('foo', array('foo' => 'bar'));
  158. $def->clearTags();
  159. $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
  160. }
  161. /**
  162. * @covers Symfony\Component\DependencyInjection\Definition::addTag
  163. * @covers Symfony\Component\DependencyInjection\Definition::getTag
  164. * @covers Symfony\Component\DependencyInjection\Definition::getTags
  165. * @covers Symfony\Component\DependencyInjection\Definition::hasTag
  166. */
  167. public function testTags()
  168. {
  169. $def = new Definition('stdClass');
  170. $this->assertEquals(array(), $def->getTag('foo'), '->getTag() returns an empty array if the tag is not defined');
  171. $this->assertFalse($def->hasTag('foo'));
  172. $this->assertSame($def, $def->addTag('foo'), '->addTag() implements a fluent interface');
  173. $this->assertTrue($def->hasTag('foo'));
  174. $this->assertEquals(array(array()), $def->getTag('foo'), '->getTag() returns attributes for a tag name');
  175. $def->addTag('foo', array('foo' => 'bar'));
  176. $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getTag('foo'), '->addTag() can adds the same tag several times');
  177. $def->addTag('bar', array('bar' => 'bar'));
  178. $this->assertEquals($def->getTags(), array(
  179. 'foo' => array(array(), array('foo' => 'bar')),
  180. 'bar' => array(array('bar' => 'bar')),
  181. ), '->getTags() returns all tags');
  182. }
  183. /**
  184. * @covers Symfony\Component\DependencyInjection\Definition::replaceArgument
  185. */
  186. public function testSetArgument()
  187. {
  188. $def = new Definition('stdClass');
  189. $def->addArgument('foo');
  190. $this->assertSame(array('foo'), $def->getArguments());
  191. $this->assertSame($def, $def->replaceArgument(0, 'moo'));
  192. $this->assertSame(array('moo'), $def->getArguments());
  193. $def->addArgument('moo');
  194. $def
  195. ->replaceArgument(0, 'foo')
  196. ->replaceArgument(1, 'bar')
  197. ;
  198. $this->assertSame(array('foo', 'bar'), $def->getArguments());
  199. }
  200. public function testSetGetProperties()
  201. {
  202. $def = new Definition('stdClass');
  203. $this->assertEquals(array(), $def->getProperties());
  204. $this->assertSame($def, $def->setProperties(array('foo' => 'bar')));
  205. $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
  206. }
  207. public function testSetProperty()
  208. {
  209. $def = new Definition('stdClass');
  210. $this->assertEquals(array(), $def->getProperties());
  211. $this->assertSame($def, $def->setProperty('foo', 'bar'));
  212. $this->assertEquals(array('foo' => 'bar'), $def->getProperties());
  213. }
  214. }