4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\ChildDefinition;
16 use Symfony\Component\DependencyInjection\DefinitionDecorator;
18 class ChildDefinitionTest extends TestCase
20 public function testConstructor()
22 $def = new ChildDefinition('foo');
24 $this->assertSame('foo', $def->getParent());
25 $this->assertSame(array(), $def->getChanges());
29 * @dataProvider getPropertyTests
31 public function testSetProperty($property, $changeKey)
33 $def = new ChildDefinition('foo');
35 $getter = 'get'.ucfirst($property);
36 $setter = 'set'.ucfirst($property);
38 $this->assertNull($def->$getter());
39 $this->assertSame($def, $def->$setter('foo'));
40 $this->assertSame('foo', $def->$getter());
41 $this->assertSame(array($changeKey => true), $def->getChanges());
44 public function getPropertyTests()
47 array('class', 'class'),
48 array('factory', 'factory'),
49 array('configurator', 'configurator'),
50 array('file', 'file'),
54 public function testSetPublic()
56 $def = new ChildDefinition('foo');
58 $this->assertTrue($def->isPublic());
59 $this->assertSame($def, $def->setPublic(false));
60 $this->assertFalse($def->isPublic());
61 $this->assertSame(array('public' => true), $def->getChanges());
64 public function testSetLazy()
66 $def = new ChildDefinition('foo');
68 $this->assertFalse($def->isLazy());
69 $this->assertSame($def, $def->setLazy(false));
70 $this->assertFalse($def->isLazy());
71 $this->assertSame(array('lazy' => true), $def->getChanges());
74 public function testSetAutowired()
76 $def = new ChildDefinition('foo');
78 $this->assertFalse($def->isAutowired());
79 $this->assertSame($def, $def->setAutowired(true));
80 $this->assertTrue($def->isAutowired());
81 $this->assertSame(array('autowired' => true), $def->getChanges());
84 public function testSetArgument()
86 $def = new ChildDefinition('foo');
88 $this->assertSame(array(), $def->getArguments());
89 $this->assertSame($def, $def->replaceArgument(0, 'foo'));
90 $this->assertSame(array('index_0' => 'foo'), $def->getArguments());
94 * @expectedException \InvalidArgumentException
96 public function testReplaceArgumentShouldRequireIntegerIndex()
98 $def = new ChildDefinition('foo');
100 $def->replaceArgument('0', 'foo');
103 public function testReplaceArgument()
105 $def = new ChildDefinition('foo');
107 $def->setArguments(array(0 => 'foo', 1 => 'bar'));
108 $this->assertSame('foo', $def->getArgument(0));
109 $this->assertSame('bar', $def->getArgument(1));
111 $this->assertSame($def, $def->replaceArgument(1, 'baz'));
112 $this->assertSame('foo', $def->getArgument(0));
113 $this->assertSame('baz', $def->getArgument(1));
115 $this->assertSame(array(0 => 'foo', 1 => 'bar', 'index_1' => 'baz'), $def->getArguments());
117 $this->assertSame($def, $def->replaceArgument('$bar', 'val'));
118 $this->assertSame('val', $def->getArgument('$bar'));
119 $this->assertSame(array(0 => 'foo', 1 => 'bar', 'index_1' => 'baz', '$bar' => 'val'), $def->getArguments());
123 * @expectedException \OutOfBoundsException
125 public function testGetArgumentShouldCheckBounds()
127 $def = new ChildDefinition('foo');
129 $def->setArguments(array(0 => 'foo'));
130 $def->replaceArgument(0, 'foo');
132 $def->getArgument(1);
135 public function testDefinitionDecoratorAliasExistsForBackwardsCompatibility()
137 $this->assertInstanceOf(ChildDefinition::class, new DefinitionDecorator('foo'));
141 * @expectedException \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
143 public function testCannotCallSetAutoconfigured()
145 $def = new ChildDefinition('foo');
146 $def->setAutoconfigured(true);
150 * @expectedException \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
152 public function testCannotCallSetInstanceofConditionals()
154 $def = new ChildDefinition('foo');
155 $def->setInstanceofConditionals(array('Foo' => new ChildDefinition('')));