DefinitionTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\Components\DependencyInjection;
  10. use Symfony\Components\DependencyInjection\Definition;
  11. class DefinitionTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @covers Symfony\Components\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\Components\DependencyInjection\Definition::setConstructor
  25. * @covers Symfony\Components\DependencyInjection\Definition::getConstructor
  26. */
  27. public function testSetGetConstructor()
  28. {
  29. $def = new Definition('stdClass');
  30. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setConstructor('foo')), '->setConstructor() implements a fluent interface');
  31. $this->assertEquals('foo', $def->getConstructor(), '->getConstructor() returns the constructor name');
  32. }
  33. /**
  34. * @covers Symfony\Components\DependencyInjection\Definition::setClass
  35. * @covers Symfony\Components\DependencyInjection\Definition::getClass
  36. */
  37. public function testSetGetClass()
  38. {
  39. $def = new Definition('stdClass');
  40. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setClass('foo')), '->setClass() implements a fluent interface');
  41. $this->assertEquals('foo', $def->getClass(), '->getClass() returns the class name');
  42. }
  43. /**
  44. * @covers Symfony\Components\DependencyInjection\Definition::setArguments
  45. * @covers Symfony\Components\DependencyInjection\Definition::getArguments
  46. * @covers Symfony\Components\DependencyInjection\Definition::addArgument
  47. */
  48. public function testArguments()
  49. {
  50. $def = new Definition('stdClass');
  51. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setArguments(array('foo'))), '->setArguments() implements a fluent interface');
  52. $this->assertEquals(array('foo'), $def->getArguments(), '->getArguments() returns the arguments');
  53. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->addArgument('bar')), '->addArgument() implements a fluent interface');
  54. $this->assertEquals(array('foo', 'bar'), $def->getArguments(), '->addArgument() adds an argument');
  55. }
  56. /**
  57. * @covers Symfony\Components\DependencyInjection\Definition::setMethodCalls
  58. * @covers Symfony\Components\DependencyInjection\Definition::addMethodCall
  59. */
  60. public function testMethodCalls()
  61. {
  62. $def = new Definition('stdClass');
  63. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setMethodCalls(array(array('foo', array('foo'))))), '->setMethodCalls() implements a fluent interface');
  64. $this->assertEquals(array(array('foo', array('foo'))), $def->getMethodCalls(), '->getMethodCalls() returns the methods to call');
  65. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->addMethodCall('bar', array('bar'))), '->addMethodCall() implements a fluent interface');
  66. $this->assertEquals(array(array('foo', array('foo')), array('bar', array('bar'))), $def->getMethodCalls(), '->addMethodCall() adds a method to call');
  67. }
  68. /**
  69. * @covers Symfony\Components\DependencyInjection\Definition::setFile
  70. * @covers Symfony\Components\DependencyInjection\Definition::getFile
  71. */
  72. public function testSetGetFile()
  73. {
  74. $def = new Definition('stdClass');
  75. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setFile('foo')), '->setFile() implements a fluent interface');
  76. $this->assertEquals('foo', $def->getFile(), '->getFile() returns the file to include');
  77. }
  78. /**
  79. * @covers Symfony\Components\DependencyInjection\Definition::setShared
  80. * @covers Symfony\Components\DependencyInjection\Definition::isShared
  81. */
  82. public function testSetIsShared()
  83. {
  84. $def = new Definition('stdClass');
  85. $this->assertTrue($def->isShared(), '->isShared() returns true by default');
  86. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setShared(false)), '->setShared() implements a fluent interface');
  87. $this->assertFalse($def->isShared(), '->isShared() returns false if the instance must not be shared');
  88. }
  89. /**
  90. * @covers Symfony\Components\DependencyInjection\Definition::setConfigurator
  91. * @covers Symfony\Components\DependencyInjection\Definition::getConfigurator
  92. */
  93. public function testSetGetConfigurator()
  94. {
  95. $def = new Definition('stdClass');
  96. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->setConfigurator('foo')), '->setConfigurator() implements a fluent interface');
  97. $this->assertEquals('foo', $def->getConfigurator(), '->getConfigurator() returns the configurator');
  98. }
  99. /**
  100. * @covers Symfony\Components\DependencyInjection\Definition::clearAnnotations
  101. */
  102. public function testClearAnnotations()
  103. {
  104. $def = new Definition('stdClass');
  105. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->clearAnnotations()), '->clearAnnotations() implements a fluent interface');
  106. $def->addAnnotation('foo', array('foo' => 'bar'));
  107. $def->clearAnnotations();
  108. $this->assertEquals(array(), $def->getAnnotations(), '->clearAnnotations() removes all current annotations');
  109. }
  110. /**
  111. * @covers Symfony\Components\DependencyInjection\Definition::addAnnotation
  112. * @covers Symfony\Components\DependencyInjection\Definition::getAnnotation
  113. * @covers Symfony\Components\DependencyInjection\Definition::getAnnotations
  114. */
  115. public function testAnnotations()
  116. {
  117. $def = new Definition('stdClass');
  118. $this->assertEquals(array(), $def->getAnnotation('foo'), '->getAnnotation() returns an empty array if the annotation is not defined');
  119. $this->assertEquals(spl_object_hash($def), spl_object_hash($def->addAnnotation('foo')), '->addAnnotation() implements a fluent interface');
  120. $this->assertEquals(array(array()), $def->getAnnotation('foo'), '->getAnnotation() returns attributes for an annotation name');
  121. $def->addAnnotation('foo', array('foo' => 'bar'));
  122. $this->assertEquals(array(array(), array('foo' => 'bar')), $def->getAnnotation('foo'), '->addAnnotation() can adds the same annotation several times');
  123. $def->addAnnotation('bar', array('bar' => 'bar'));
  124. $this->assertEquals($def->getAnnotations(), array(
  125. 'foo' => array(array(), array('foo' => 'bar')),
  126. 'bar' => array(array('bar' => 'bar')),
  127. ), '->getAnnotations() returns all annotations');
  128. }
  129. }