ChildDefinitionTest.php 4.6 KB

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