DefinitionTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. */
  67. public function testMethodCalls()
  68. {
  69. $def = new Definition('stdClass');
  70. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setMethodCalls(array(array('foo', array('foo'))))), '->setMethodCalls() implements a fluent interface');
  71. $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->getMethodCalls() returns the methods to call');
  72. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->addMethodCall('bar', array('bar'))), '->addMethodCall() implements a fluent interface');
  73. $this->assertEquals(array(array('foo', array('foo')), array('bar', array('bar'))), $def->getMethodCalls(), '->addMethodCall() adds a method to call');
  74. }
  75. /**
  76. * @covers Symfony\Component\DependencyInjection\Definition::setFile
  77. * @covers Symfony\Component\DependencyInjection\Definition::getFile
  78. */
  79. public function testSetGetFile()
  80. {
  81. $def = new Definition('stdClass');
  82. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setFile('foo')), '->setFile() implements a fluent interface');
  83. $this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
  84. }
  85. /**
  86. * @covers Symfony\Component\DependencyInjection\Definition::setShared
  87. * @covers Symfony\Component\DependencyInjection\Definition::isShared
  88. */
  89. public function testSetIsShared()
  90. {
  91. $def = new Definition('stdClass');
  92. $this->assertTrue($def->isShared(), '->isShared() returns true by default');
  93. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setShared(false)), '->setShared() implements a fluent interface');
  94. $this->assertFalse($def->isShared(), '->isShared() returns false if the instance must not be shared');
  95. }
  96. /**
  97. * @covers Symfony\Component\DependencyInjection\Definition::setConfigurator
  98. * @covers Symfony\Component\DependencyInjection\Definition::getConfigurator
  99. */
  100. public function testSetGetConfigurator()
  101. {
  102. $def = new Definition('stdClass');
  103. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setConfigurator('foo')), '->setConfigurator() implements a fluent interface');
  104. $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
  105. }
  106. /**
  107. * @covers Symfony\Component\DependencyInjection\Definition::clearTags
  108. */
  109. public function testClearTags()
  110. {
  111. $def = new Definition('stdClass');
  112. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->clearTags()), '->clearTags() implements a fluent interface');
  113. $def->addTag('foo', array('foo' => 'bar'));
  114. $def->clearTags();
  115. $this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
  116. }
  117. /**
  118. * @covers Symfony\Component\DependencyInjection\Definition::addTag
  119. * @covers Symfony\Component\DependencyInjection\Definition::getTag
  120. * @covers Symfony\Component\DependencyInjection\Definition::getTags
  121. */
  122. public function testTags()
  123. {
  124. $def = new Definition('stdClass');
  125. $this->assertEquals(array(), $def->getTag('foo'), '->getTag() returns an empty array if the tag is not defined');
  126. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->addTag('foo')), '->addTag() implements a fluent interface');
  127. $this->assertEquals(array(array()), $def->getTag('foo'), '->getTag() returns attributes for a tag name');
  128. $def->addTag('foo', array('foo' => 'bar'));
  129. $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getTag('foo'), '->addTag() can adds the same tag several times');
  130. $def->addTag('bar', array('bar' => 'bar'));
  131. $this->assertEquals($def->getTags(), array(
  132. 'foo' => array(array(), array('foo' => 'bar')),
  133. 'bar' => array(array('bar' => 'bar')),
  134. ), '->getTags() returns all tags');
  135. }
  136. }