From 428814b25ddd7b0bfbf56d0f4cc3879c3e9bc793 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Thu, 26 Jan 2017 11:48:39 +0100 Subject: [PATCH] clarify exception when no args are configured --- .../Component/DependencyInjection/Definition.php | 4 ++++ .../DependencyInjection/Tests/DefinitionTest.php | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/Definition.php b/src/Symfony/Component/DependencyInjection/Definition.php index 4f2ee304adad..49e4e1b0e29c 100644 --- a/src/Symfony/Component/DependencyInjection/Definition.php +++ b/src/Symfony/Component/DependencyInjection/Definition.php @@ -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)); } diff --git a/src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php b/src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php index 3439a5912384..0d3cb8a000bf 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/DefinitionTest.php @@ -232,6 +232,7 @@ public function testGetArgumentShouldCheckBounds() /** * @expectedException \OutOfBoundsException + * @expectedExceptionMessage The index "1" is not in the range [0, 0]. */ public function testReplaceArgumentShouldCheckBounds() { @@ -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');