Skip to content

Commit

Permalink
minor #21416 [DependencyInjection] clarify exception when no args are…
Browse files Browse the repository at this point in the history
… configured (xabbuh)

This PR was merged into the 2.7 branch.

Discussion
----------

[DependencyInjection] clarify exception when no args are configured

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #21412
| License       | MIT
| Doc PR        |

Commits
-------

428814b clarify exception when no args are configured
  • Loading branch information
fabpot committed Jan 26, 2017
2 parents a6d2420 + 428814b commit a35986f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Symfony/Component/DependencyInjection/Definition.php
Expand Up @@ -302,6 +302,10 @@ public function addArgument($argument)
*/
public function replaceArgument($index, $argument)
{
if (0 === count($this->arguments)) {
throw new OutOfBoundsException('Cannot replace arguments if none have been configured yet.');
}

if ($index < 0 || $index > count($this->arguments) - 1) {
throw new OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
}
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php
Expand Up @@ -232,6 +232,7 @@ public function testGetArgumentShouldCheckBounds()

/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage The index "1" is not in the range [0, 0].
*/
public function testReplaceArgumentShouldCheckBounds()
{
Expand All @@ -241,6 +242,16 @@ public function testReplaceArgumentShouldCheckBounds()
$def->replaceArgument(1, 'bar');
}

/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage Cannot replace arguments if none have been configured yet.
*/
public function testReplaceArgumentWithoutExistingArgumentsShouldCheckBounds()
{
$def = new Definition('stdClass');
$def->replaceArgument(0, 'bar');
}

public function testSetGetProperties()
{
$def = new Definition('stdClass');
Expand Down

0 comments on commit a35986f

Please sign in to comment.