DefinitionDecoratorTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\Component\DependencyInjection\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\DefinitionDecorator;
  13. class DefinitionDecoratorTest extends TestCase
  14. {
  15. public function testConstructor()
  16. {
  17. $def = new DefinitionDecorator('foo');
  18. $this->assertEquals('foo', $def->getParent());
  19. $this->assertEquals(array(), $def->getChanges());
  20. }
  21. /**
  22. * @dataProvider getPropertyTests
  23. */
  24. public function testSetProperty($property, $changeKey)
  25. {
  26. $def = new DefinitionDecorator('foo');
  27. $getter = 'get'.ucfirst($property);
  28. $setter = 'set'.ucfirst($property);
  29. $this->assertNull($def->$getter());
  30. $this->assertSame($def, $def->$setter('foo'));
  31. $this->assertEquals('foo', $def->$getter());
  32. $this->assertEquals(array($changeKey => true), $def->getChanges());
  33. }
  34. public function getPropertyTests()
  35. {
  36. return array(
  37. array('class', 'class'),
  38. array('factory', 'factory'),
  39. array('configurator', 'configurator'),
  40. array('file', 'file'),
  41. );
  42. }
  43. /**
  44. * @dataProvider provideLegacyPropertyTests
  45. * @group legacy
  46. */
  47. public function testLegacySetProperty($property, $changeKey)
  48. {
  49. $def = new DefinitionDecorator('foo');
  50. $getter = 'get'.ucfirst($property);
  51. $setter = 'set'.ucfirst($property);
  52. $this->assertNull($def->$getter());
  53. $this->assertSame($def, $def->$setter('foo'));
  54. $this->assertEquals('foo', $def->$getter());
  55. $this->assertEquals(array($changeKey => true), $def->getChanges());
  56. }
  57. public function provideLegacyPropertyTests()
  58. {
  59. return array(
  60. array('factoryClass', 'factory_class'),
  61. array('factoryMethod', 'factory_method'),
  62. array('factoryService', 'factory_service'),
  63. );
  64. }
  65. public function testSetPublic()
  66. {
  67. $def = new DefinitionDecorator('foo');
  68. $this->assertTrue($def->isPublic());
  69. $this->assertSame($def, $def->setPublic(false));
  70. $this->assertFalse($def->isPublic());
  71. $this->assertEquals(array('public' => true), $def->getChanges());
  72. }
  73. public function testSetLazy()
  74. {
  75. $def = new DefinitionDecorator('foo');
  76. $this->assertFalse($def->isLazy());
  77. $this->assertSame($def, $def->setLazy(false));
  78. $this->assertFalse($def->isLazy());
  79. $this->assertEquals(array('lazy' => true), $def->getChanges());
  80. }
  81. public function testSetAutowired()
  82. {
  83. $def = new DefinitionDecorator('foo');
  84. $this->assertFalse($def->isAutowired());
  85. $this->assertSame($def, $def->setAutowired(false));
  86. $this->assertFalse($def->isAutowired());
  87. $this->assertEquals(array('autowire' => true), $def->getChanges());
  88. }
  89. public function testSetArgument()
  90. {
  91. $def = new DefinitionDecorator('foo');
  92. $this->assertEquals(array(), $def->getArguments());
  93. $this->assertSame($def, $def->replaceArgument(0, 'foo'));
  94. $this->assertEquals(array('index_0' => 'foo'), $def->getArguments());
  95. }
  96. /**
  97. * @expectedException \InvalidArgumentException
  98. */
  99. public function testReplaceArgumentShouldRequireIntegerIndex()
  100. {
  101. $def = new DefinitionDecorator('foo');
  102. $def->replaceArgument('0', 'foo');
  103. }
  104. public function testReplaceArgument()
  105. {
  106. $def = new DefinitionDecorator('foo');
  107. $def->setArguments(array(0 => 'foo', 1 => 'bar'));
  108. $this->assertEquals('foo', $def->getArgument(0));
  109. $this->assertEquals('bar', $def->getArgument(1));
  110. $this->assertSame($def, $def->replaceArgument(1, 'baz'));
  111. $this->assertEquals('foo', $def->getArgument(0));
  112. $this->assertEquals('baz', $def->getArgument(1));
  113. $this->assertEquals(array(0 => 'foo', 1 => 'bar', 'index_1' => 'baz'), $def->getArguments());
  114. }
  115. /**
  116. * @expectedException \OutOfBoundsException
  117. */
  118. public function testGetArgumentShouldCheckBounds()
  119. {
  120. $def = new DefinitionDecorator('foo');
  121. $def->setArguments(array(0 => 'foo'));
  122. $def->replaceArgument(0, 'foo');
  123. $def->getArgument(1);
  124. }
  125. }