minor #21416 [DependencyInjection] clarify exception when no args are 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
-------

428814b25d clarify exception when no args are configured
This commit is contained in:
Fabien Potencier 2017-01-26 07:56:09 -08:00
commit a35986f657
2 changed files with 15 additions and 0 deletions

View File

@ -302,6 +302,10 @@ class Definition
*/
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));
}

View File

@ -232,6 +232,7 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase
/**
* @expectedException \OutOfBoundsException
* @expectedExceptionMessage The index "1" is not in the range [0, 0].
*/
public function testReplaceArgumentShouldCheckBounds()
{
@ -241,6 +242,16 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase
$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');