DefinitionTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. /**
  25. * @covers Symfony\Component\DependencyInjection\Definition::setFactoryMethod
  26. * @covers Symfony\Component\DependencyInjection\Definition::getFactoryMethod
  27. */
  28. public function testSetGetConstructor()
  29. {
  30. $def = new Definition('stdClass');
  31. $this->assertSame($def, $def->setFactoryMethod('foo'), '->setFactoryMethod() implements a fluent interface');
  32. $this->assertEquals('foo', $def->getFactoryMethod(), '->getFactoryMethod() returns the factory method name');
  33. }
  34. public function testSetGetFactoryService()
  35. {
  36. $def = new Definition('stdClass');
  37. $this->assertNull($def->getFactoryService());
  38. $this->assertSame($def, $def->setFactoryService('stdClass2'), "->setFactoryService() implements a fluent interface.");
  39. $this->assertEquals('stdClass2', $def->getFactoryService(), "->getFactoryService() returns current service to construct this service.");
  40. }
  41. /**
  42. * @covers Symfony\Component\DependencyInjection\Definition::setClass
  43. * @covers Symfony\Component\DependencyInjection\Definition::getClass
  44. */
  45. public function testSetGetClass()
  46. {
  47. $def = new Definition('stdClass');
  48. $this->assertSame($def, $def->setClass('foo'), '->setClass() implements a fluent interface');
  49. $this->assertEquals('foo', $def->getClass(), '->getClass() returns the class name');
  50. }
  51. /**
  52. * @covers Symfony\Component\DependencyInjection\Definition::setArguments
  53. * @covers Symfony\Component\DependencyInjection\Definition::getArguments
  54. * @covers Symfony\Component\DependencyInjection\Definition::addArgument
  55. */
  56. public function testArguments()
  57. {
  58. $def = new Definition('stdClass');
  59. $this->assertSame($def, $def->setArguments(array('foo')), '->setArguments() implements a fluent interface');
  60. $this->assertEquals(array('foo'), $def->getArguments(), '->getArguments() returns the arguments');
  61. $this->assertSame($def, $def->addArgument('bar'), '->addArgument() implements a fluent interface');
  62. $this->assertEquals(array('foo', 'bar'), $def->getArguments(), '->addArgument() adds an argument');
  63. }
  64. /**
  65. * @covers Symfony\Component\DependencyInjection\Definition::setMethodCalls
  66. * @covers Symfony\Component\DependencyInjection\Definition::addMethodCall
  67. * @covers Symfony\Component\DependencyInjection\Definition::hasMethodCall
  68. * @covers Symfony\Component\DependencyInjection\Definition::removeMethodCall
  69. */
  70. public function testMethodCalls()
  71. {
  72. $def = new Definition('stdClass');
  73. $this->assertSame($def, $def->setMethodCalls(array(array('foo', array('foo')))), '->setMethodCalls() implements a fluent interface');
  74. $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->getMethodCalls() returns the methods to call');
  75. $this->assertSame($def, $def->addMethodCall('bar', array('bar')), '->addMethodCall() implements a fluent interface');
  76. $this->assertEquals(array(array('foo', array('foo')), array('bar', array('bar'))), $def->getMethodCalls(), '->addMethodCall() adds a method to call');
  77. $this->assertTrue($def->hasMethodCall('bar'), '->hasMethodCall() returns true if first argument is a method to call registered');
  78. $this->assertFalse($def->hasMethodCall('no_registered'), '->hasMethodCall() returns false if first argument is not a method to call registered');
  79. $this->assertSame($def, $def->removeMethodCall('bar'), '->removeMethodCall() implements a fluent interface');
  80. $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->removeMethodCall() removes a method to call');
  81. }
  82. /**
  83. * @covers Symfony\Component\DependencyInjection\Definition::setFile
  84. * @covers Symfony\Component\DependencyInjection\Definition::getFile
  85. */
  86. public function testSetGetFile()
  87. {
  88. $def = new Definition('stdClass');
  89. $this->assertSame($def, $def->setFile('foo'), '->setFile() implements a fluent interface');
  90. $this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
  91. }
  92. /**
  93. * @covers Symfony\Component\DependencyInjection\Definition::setShared
  94. * @covers Symfony\Component\DependencyInjection\Definition::isShared
  95. */
  96. public function testSetIsShared()
  97. {
  98. $def = new Definition('stdClass');
  99. $this->assertTrue($def->isShared(), '->isShared() returns true by default');
  100. $this->assertSame($def, $def->setShared(false), '->setShared() implements a fluent interface');
  101. $this->assertFalse($def->isShared(), '->isShared() returns false if the instance must not be shared');
  102. }
  103. /**
  104. * @covers Symfony\Component\DependencyInjection\Definition::setPublic
  105. * @covers Symfony\Component\DependencyInjection\Definition::isPublic
  106. */
  107. public function testSetIsPublic()
  108. {
  109. $def = new Definition('stdClass');
  110. $this->assertTrue($def->isPublic(), '->isPublic() returns true by default');
  111. $this->assertSame($def, $def->setPublic(false), '->setPublic() implements a fluent interface');
  112. $this->assertFalse($def->isPublic(), '->isPublic() returns false if the instance must not be public.');
  113. }
  114. /**
  115. * @covers Symfony\Component\DependencyInjection\Definition::setConfigurator
  116. * @covers Symfony\Component\DependencyInjection\Definition::getConfigurator
  117. */
  118. public function testSetGetConfigurator()
  119. {
  120. $def = new Definition('stdClass');
  121. $this->assertSame($def, $def->setConfigurator('foo'), '->setConfigurator() implements a fluent interface');
  122. $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
  123. }
  124. /**
  125. * @covers Symfony\Component\DependencyInjection\Definition::clearTags
  126. */
  127. public function testClearTags()
  128. {
  129. $def = new Definition('stdClass');
  130. $this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
  131. $def->addTag('foo', array('foo' => 'bar'));
  132. $def->clearTags();
  133. $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
  134. }
  135. /**
  136. * @covers Symfony\Component\DependencyInjection\Definition::addTag
  137. * @covers Symfony\Component\DependencyInjection\Definition::getTag
  138. * @covers Symfony\Component\DependencyInjection\Definition::getTags
  139. */
  140. public function testTags()
  141. {
  142. $def = new Definition('stdClass');
  143. $this->assertEquals(array(), $def->getTag('foo'), '->getTag() returns an empty array if the tag is not defined');
  144. $this->assertSame($def, $def->addTag('foo'), '->addTag() implements a fluent interface');
  145. $this->assertEquals(array(array()), $def->getTag('foo'), '->getTag() returns attributes for a tag name');
  146. $def->addTag('foo', array('foo' => 'bar'));
  147. $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getTag('foo'), '->addTag() can adds the same tag several times');
  148. $def->addTag('bar', array('bar' => 'bar'));
  149. $this->assertEquals($def->getTags(), array(
  150. 'foo' => array(array(), array('foo' => 'bar')),
  151. 'bar' => array(array('bar' => 'bar')),
  152. ), '->getTags() returns all tags');
  153. }
  154. /**
  155. * @covers Symfony\Component\DependencyInjection\Definition::setArgument
  156. */
  157. public function testSetArgument()
  158. {
  159. $def = new Definition('stdClass');
  160. $def->addArgument('foo');
  161. $this->assertSame(array('foo'), $def->getArguments());
  162. $this->assertSame($def, $def->setArgument(0, 'moo'));
  163. $this->assertSame(array('moo'), $def->getArguments());
  164. $def->addArgument('moo');
  165. $def
  166. ->setArgument(0, 'foo')
  167. ->setArgument(1, 'bar')
  168. ;
  169. $this->assertSame(array('foo', 'bar'), $def->getArguments());
  170. }
  171. }