DefinitionDecoratorTest.php 3.7 KB

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