DefinitionTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Symfony\Tests\Component\DependencyInjection;
  10. use Symfony\Component\DependencyInjection\Definition;
  11. class DefinitionTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @covers Symfony\Component\DependencyInjection\Definition::__construct
  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. /**
  24. * @covers Symfony\Component\DependencyInjection\Definition::setFactoryMethod
  25. * @covers Symfony\Component\DependencyInjection\Definition::getFactoryMethod
  26. */
  27. public function testSetGetConstructor()
  28. {
  29. $def = new Definition('stdClass');
  30. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setFactoryMethod('foo')), '->setFactoryMethod() implements a fluent interface');
  31. $this->assertEquals('foo', $def->getFactoryMethod(), '->getFactoryMethod() returns the factory method name');
  32. }
  33. public function testSetGetFactoryService()
  34. {
  35. $def = new Definition('stdClass');
  36. $this->assertNull($def->getFactoryService());
  37. $this->assertSame($def, $def->setFactoryService('stdClass2'), "->setFactoryService() implements a fluent interface.");
  38. $this->assertEquals('stdClass2', $def->getFactoryService(), "->getFactoryService() returns current service to construct this service.");
  39. }
  40. /**
  41. * @covers Symfony\Component\DependencyInjection\Definition::setClass
  42. * @covers Symfony\Component\DependencyInjection\Definition::getClass
  43. */
  44. public function testSetGetClass()
  45. {
  46. $def = new Definition('stdClass');
  47. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setClass('foo')), '->setClass() implements a fluent interface');
  48. $this->assertEquals('foo', $def->getClass(), '->getClass() returns the class name');
  49. }
  50. /**
  51. * @covers Symfony\Component\DependencyInjection\Definition::setArguments
  52. * @covers Symfony\Component\DependencyInjection\Definition::getArguments
  53. * @covers Symfony\Component\DependencyInjection\Definition::addArgument
  54. */
  55. public function testArguments()
  56. {
  57. $def = new Definition('stdClass');
  58. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setArguments(array('foo'))), '->setArguments() implements a fluent interface');
  59. $this->assertEquals(array('foo'), $def->getArguments(), '->getArguments() returns the arguments');
  60. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->addArgument('bar')), '->addArgument() implements a fluent interface');
  61. $this->assertEquals(array('foo', 'bar'), $def->getArguments(), '->addArgument() adds an argument');
  62. }
  63. /**
  64. * @covers Symfony\Component\DependencyInjection\Definition::setMethodCalls
  65. * @covers Symfony\Component\DependencyInjection\Definition::addMethodCall
  66. * @covers Symfony\Component\DependencyInjection\Definition::hasMethodCall
  67. * @covers Symfony\Component\DependencyInjection\Definition::removeMethodCall
  68. */
  69. public function testMethodCalls()
  70. {
  71. $def = new Definition('stdClass');
  72. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setMethodCalls(array(array('foo', array('foo'))))), '->setMethodCalls() implements a fluent interface');
  73. $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->getMethodCalls() returns the methods to call');
  74. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->addMethodCall('bar', array('bar'))), '->addMethodCall() implements a fluent interface');
  75. $this->assertEquals(array(array('foo', array('foo')), array('bar', array('bar'))), $def->getMethodCalls(), '->addMethodCall() adds a method to call');
  76. $this->assertTrue($def->hasMethodCall('bar'), '->hasMethodCall() returns true if first argument is a method to call registered');
  77. $this->assertFalse($def->hasMethodCall('no_registered'), '->hasMethodCall() returns false if first argument is not a method to call registered');
  78. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->removeMethodCall('bar')), '->removeMethodCall() implements a fluent interface');
  79. $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->removeMethodCall() removes a method to call');
  80. }
  81. /**
  82. * @covers Symfony\Component\DependencyInjection\Definition::setFile
  83. * @covers Symfony\Component\DependencyInjection\Definition::getFile
  84. */
  85. public function testSetGetFile()
  86. {
  87. $def = new Definition('stdClass');
  88. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setFile('foo')), '->setFile() implements a fluent interface');
  89. $this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
  90. }
  91. /**
  92. * @covers Symfony\Component\DependencyInjection\Definition::setShared
  93. * @covers Symfony\Component\DependencyInjection\Definition::isShared
  94. */
  95. public function testSetIsShared()
  96. {
  97. $def = new Definition('stdClass');
  98. $this->assertTrue($def->isShared(), '->isShared() returns true by default');
  99. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setShared(false)), '->setShared() implements a fluent interface');
  100. $this->assertFalse($def->isShared(), '->isShared() returns false if the instance must not be shared');
  101. }
  102. /**
  103. * @covers Symfony\Component\DependencyInjection\Definition::setConfigurator
  104. * @covers Symfony\Component\DependencyInjection\Definition::getConfigurator
  105. */
  106. public function testSetGetConfigurator()
  107. {
  108. $def = new Definition('stdClass');
  109. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setConfigurator('foo')), '->setConfigurator() implements a fluent interface');
  110. $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
  111. }
  112. /**
  113. * @covers Symfony\Component\DependencyInjection\Definition::clearTags
  114. */
  115. public function testClearTags()
  116. {
  117. $def = new Definition('stdClass');
  118. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->clearTags()), '->clearTags() implements a fluent interface');
  119. $def->addTag('foo', array('foo' => 'bar'));
  120. $def->clearTags();
  121. $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
  122. }
  123. /**
  124. * @covers Symfony\Component\DependencyInjection\Definition::addTag
  125. * @covers Symfony\Component\DependencyInjection\Definition::getTag
  126. * @covers Symfony\Component\DependencyInjection\Definition::getTags
  127. */
  128. public function testTags()
  129. {
  130. $def = new Definition('stdClass');
  131. $this->assertEquals(array(), $def->getTag('foo'), '->getTag() returns an empty array if the tag is not defined');
  132. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->addTag('foo')), '->addTag() implements a fluent interface');
  133. $this->assertEquals(array(array()), $def->getTag('foo'), '->getTag() returns attributes for a tag name');
  134. $def->addTag('foo', array('foo' => 'bar'));
  135. $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getTag('foo'), '->addTag() can adds the same tag several times');
  136. $def->addTag('bar', array('bar' => 'bar'));
  137. $this->assertEquals($def->getTags(), array(
  138. 'foo' => array(array(), array('foo' => 'bar')),
  139. 'bar' => array(array('bar' => 'bar')),
  140. ), '->getTags() returns all tags');
  141. }
  142. }