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\DefinitionDecorator;
17 class DefinitionDecoratorTest extends TestCase
19 public function testConstructor()
21 $def = new DefinitionDecorator('foo');
23 $this->assertEquals('foo', $def->getParent());
24 $this->assertEquals(array(), $def->getChanges());
28 * @dataProvider getPropertyTests
30 public function testSetProperty($property, $changeKey)
32 $def = new DefinitionDecorator('foo');
34 $getter = 'get'.ucfirst($property);
35 $setter = 'set'.ucfirst($property);
37 $this->assertNull($def->$getter());
38 $this->assertSame($def, $def->$setter('foo'));
39 $this->assertEquals('foo', $def->$getter());
40 $this->assertEquals(array($changeKey => true), $def->getChanges());
43 public function getPropertyTests()
46 array('class', 'class'),
47 array('factory', 'factory'),
48 array('configurator', 'configurator'),
49 array('file', 'file'),
54 * @dataProvider provideLegacyPropertyTests
57 public function testLegacySetProperty($property, $changeKey)
59 $def = new DefinitionDecorator('foo');
61 $getter = 'get'.ucfirst($property);
62 $setter = 'set'.ucfirst($property);
64 $this->assertNull($def->$getter());
65 $this->assertSame($def, $def->$setter('foo'));
66 $this->assertEquals('foo', $def->$getter());
67 $this->assertEquals(array($changeKey => true), $def->getChanges());
70 public function provideLegacyPropertyTests()
73 array('factoryClass', 'factory_class'),
74 array('factoryMethod', 'factory_method'),
75 array('factoryService', 'factory_service'),
79 public function testSetPublic()
81 $def = new DefinitionDecorator('foo');
83 $this->assertTrue($def->isPublic());
84 $this->assertSame($def, $def->setPublic(false));
85 $this->assertFalse($def->isPublic());
86 $this->assertEquals(array('public' => true), $def->getChanges());
89 public function testSetLazy()
91 $def = new DefinitionDecorator('foo');
93 $this->assertFalse($def->isLazy());
94 $this->assertSame($def, $def->setLazy(false));
95 $this->assertFalse($def->isLazy());
96 $this->assertEquals(array('lazy' => true), $def->getChanges());
99 public function testSetAutowired()
101 $def = new DefinitionDecorator('foo');
103 $this->assertFalse($def->isAutowired());
104 $this->assertSame($def, $def->setAutowired(false));
105 $this->assertFalse($def->isAutowired());
106 $this->assertEquals(array('autowire' => true), $def->getChanges());
109 public function testSetArgument()
111 $def = new DefinitionDecorator('foo');
113 $this->assertEquals(array(), $def->getArguments());
114 $this->assertSame($def, $def->replaceArgument(0, 'foo'));
115 $this->assertEquals(array('index_0' => 'foo'), $def->getArguments());
119 * @expectedException \InvalidArgumentException
121 public function testReplaceArgumentShouldRequireIntegerIndex()
123 $def = new DefinitionDecorator('foo');
125 $def->replaceArgument('0', 'foo');
128 public function testReplaceArgument()
130 $def = new DefinitionDecorator('foo');
132 $def->setArguments(array(0 => 'foo', 1 => 'bar'));
133 $this->assertEquals('foo', $def->getArgument(0));
134 $this->assertEquals('bar', $def->getArgument(1));
136 $this->assertSame($def, $def->replaceArgument(1, 'baz'));
137 $this->assertEquals('foo', $def->getArgument(0));
138 $this->assertEquals('baz', $def->getArgument(1));
140 $this->assertEquals(array(0 => 'foo', 1 => 'bar', 'index_1' => 'baz'), $def->getArguments());
144 * @expectedException \OutOfBoundsException
146 public function testGetArgumentShouldCheckBounds()
148 $def = new DefinitionDecorator('foo');
150 $def->setArguments(array(0 => 'foo'));
151 $def->replaceArgument(0, 'foo');
153 $def->getArgument(1);