8000 merged branch jmikola/2.0-DefinitionDecorator (PR #2502) · symfony/symfony@49ad487 · GitHub
[go: up one dir, main page]

Skip to content

Commit 49ad487

Browse files
committed
merged branch jmikola/2.0-DefinitionDecorator (PR #2502)
Commits ------- 80f0b98 [DependencyInjection] Fix DefinitionDecorator::getArgument() for replacements 4bbb685 [DependencyInjection] Test Definition and DefinitionDecorator exceptions Discussion ---------- [DependencyInjection] Fix getArgument() for replaced definition arguments I was implementing something that worked with DefinitionDecorators before the container was compiled and ran into some missing functionality. While I was tinkering, I also added some additional tests for exceptions in both Definition and DefinitionDecorator.
2 parents 323f80d + 80f0b98 commit 49ad487

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/Symfony/Component/DependencyInjection/DefinitionDecorator.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,33 @@ public function setPublic($boolean)
146146
return parent::setPublic($boolean);
147147
}
148148

149+
/**
150+
* Gets an argument to pass to the service constructor/factory method.
151+
*
152+
* If replaceArgument() has been used to replace an argument, this method
153+
* will return the replacement value.
154+
*
155+
* @param integer $index
156+
*
157+
* @return mixed The argument value
158+
*
159+
* @api
160+
*/
161+
public function getArgument($index)
162+
{
163+
if (array_key_exists('index_'.$index, $this->arguments)) {
164+
return $this->arguments['index_'.$index];
165+
}
166+
167+
$lastIndex = count(array_filter(array_keys($this->arguments), 'is_int')) - 1;
168+
169+
if ($index < 0 || $index > $lastIndex) {
170+
throw new \OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, $lastIndex));
171+
}
172+
173+
return $this->arguments[$index];
174+
}
175+
149176
/**
150177
* You should always use this method when overwriting existing arguments
151178
* of the parent definition.

tests/Symfony/Tests/Component/DependencyInjection/DefinitionDecoratorTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,42 @@ public function testSetArgument()
6969
$this->assertSame($def, $def->replaceArgument(0, 'foo'));
7070
$this->assertEquals(array('index_0' => 'foo'), $def->getArguments());
7171
}
72+
73+
/**
74+
* @expectedException InvalidArgumentException
75+
*/
76+
public function testReplaceArgumentShouldRequireIntegerIndex()
77+
{
78+
$def = new DefinitionDecorator('foo');
79+
80+
$def->replaceArgument('0', 'foo');
81+
}
82+
83+
public function testReplaceArgument()
84+
{
85+
$def = new DefinitionDecorator('foo');
86+
87+
$def->setArguments(array(0 => 'foo', 1 => 'bar'));
88+
$this->assertEquals('foo', $def->getArgument(0));
89+
$this->assertEquals('bar', $def->getArgument(1));
90+
91+
$this->assertSame($def, $def->replaceArgument(1, 'baz'));
92+
$this->assertEquals('foo', $def->getArgument(0));
93+
$this->assertEquals('baz', $def->getArgument(1));
94+
95+
$this->assertEquals(array(0 => 'foo', 1 => 'bar', 'index_1' => 'baz'), $def->getArguments());
96+
}
97+
98+
/**
99+
* @expectedException OutOfBoundsException
100+
*/
101+
public function testGetArgumentShouldCheckBounds()
102+
{
103+
$def = new DefinitionDecorator('foo');
104+
105+
$def->setArguments(array(0 => 'foo'));
106+
$def->replaceArgument(0, 'foo');
107+
108+
$def->getArgument(1);
109+
}
72110
}

tests/Symfony/Tests/Component/DependencyInjection/DefinitionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,28 @@ public function testSetArgument()
221221
$this->assertSame(array('foo', 'bar'), $def->getArguments());
222222
}
223223

224+
/**
225+
* @expectedException OutOfBoundsException
226+
*/
227+
public function testGetArgumentShouldCheckBounds()
228+
{
229+
$def = new Definition('stdClass');
230+
231+
$def->addArgument('foo');
232+
$def->getArgument(1);
233+
}
234+
235+
/**
236+
* @expectedException OutOfBoundsException
237+
*/
238+
public function testReplaceArgumentShouldCheckBounds()
239+
{
240+
$def = new Definition('stdClass');
241+
242+
$def->addArgument('foo');
243+
$def->replaceArgument(1, 'bar');
244+
}
245+
224246
public function testSetGetProperties()
225247
{
226248
$def = new Definition('stdClass');

0 commit comments

Comments
 (0)
0