From abd663ee59a5e91130ccfd64aa82c08e14f4d3aa Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sun, 2 Aug 2015 13:28:24 +0200 Subject: [PATCH 1/3] Allow to define Enum nodes with 1 single element --- src/Symfony/Component/Config/Definition/EnumNode.php | 7 +------ .../Config/Tests/Definition/EnumNodeTest.php | 12 ++++++++---- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/Config/Definition/EnumNode.php b/src/Symfony/Component/Config/Definition/EnumNode.php index 224871ab6fe02..3730f0fdadfab 100644 --- a/src/Symfony/Component/Config/Definition/EnumNode.php +++ b/src/Symfony/Component/Config/Definition/EnumNode.php @@ -24,13 +24,8 @@ class EnumNode extends ScalarNode public function __construct($name, NodeInterface $parent = null, array $values = array()) { - $values = array_unique($values); - if (count($values) <= 1) { - throw new \InvalidArgumentException('$values must contain at least two distinct elements.'); - } - parent::__construct($name, $parent); - $this->values = $values; + $this->values = array_unique($values); } public function getValues() diff --git a/src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php b/src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php index 2b84de6b098f7..34a3b727fcd83 100644 --- a/src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php +++ b/src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php @@ -21,12 +21,16 @@ public function testFinalizeValue() $this->assertSame('foo', $node->finalize('foo')); } - /** - * @expectedException \InvalidArgumentException - */ public function testConstructionWithOneValue() { - new EnumNode('foo', null, array('foo', 'foo')); + $node = new EnumNode('foo', null, array('foo')); + $this->assertSame('foo', $node->finalize('foo')); + } + + public function testConstructionWithOneDistinctValue() + { + $node = new EnumNode('foo', null, array('foo', 'foo')); + $this->assertSame('foo', $node->finalize('foo')); } /** From e75a983c42178caf11a99556bf1063fb5e605db8 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sun, 2 Aug 2015 16:35:20 +0200 Subject: [PATCH 2/3] Prevent empty arrays for Enum nodes --- src/Symfony/Component/Config/Definition/EnumNode.php | 7 ++++++- .../Component/Config/Tests/Definition/EnumNodeTest.php | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Config/Definition/EnumNode.php b/src/Symfony/Component/Config/Definition/EnumNode.php index 3730f0fdadfab..9b4c4156e311e 100644 --- a/src/Symfony/Component/Config/Definition/EnumNode.php +++ b/src/Symfony/Component/Config/Definition/EnumNode.php @@ -24,8 +24,13 @@ class EnumNode extends ScalarNode public function __construct($name, NodeInterface $parent = null, array $values = array()) { + $values = array_unique($values); + if (empty($values)) { + throw new \InvalidArgumentException('$values must contain at least one element.'); + } + parent::__construct($name, $parent); - $this->values = array_unique($values); + $this->values = $values; } public function getValues() diff --git a/src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php b/src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php index 34a3b727fcd83..654d5050dd9c9 100644 --- a/src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php +++ b/src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php @@ -21,6 +21,15 @@ public function testFinalizeValue() $this->assertSame('foo', $node->finalize('foo')); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage $values must contain at least one element. + */ + public function testConstructionWithNoValues() + { + new EnumNode('foo', null, array()); + } + public function testConstructionWithOneValue() { $node = new EnumNode('foo', null, array('foo')); From c12fbba15082231fd5c98d9855f685ad28cc9597 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 3 Aug 2015 10:23:17 +0200 Subject: [PATCH 3/3] Added a note about the BC break in Enum config nodes --- UPGRADE-2.8.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/UPGRADE-2.8.md b/UPGRADE-2.8.md index 2c18e58f50a5e..655acee0de431 100644 --- a/UPGRADE-2.8.md +++ b/UPGRADE-2.8.md @@ -399,3 +399,23 @@ FrameworkBundle session: cookie_httponly: false ``` + +Config +------ + +The edge case of defining just one value for nodes of type Enum is now allowed: + +```php +$rootNode + ->children() + ->enumNode('variable') + ->values(array('value')) + ->end() + ->end() +; +``` + +Before: `InvalidArgumentException` (variable must contain at least two +distinct elements). +After: the code will work as expected and it will restrict the values of the +`variable` option to just `value`.