From 68f0818a6cab387217caa699d14240bd8f41d709 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sun, 2 Aug 2015 13:28:24 +0200 Subject: [PATCH] Allow to define Enum nodes with 1 single element --- UPGRADE-2.8.md | 20 +++++++++++++++++++ .../Component/Config/Definition/EnumNode.php | 4 ++-- .../Config/Tests/Definition/EnumNodeTest.php | 15 +++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/UPGRADE-2.8.md b/UPGRADE-2.8.md index 2c18e58f50..655acee0de 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`. diff --git a/src/Symfony/Component/Config/Definition/EnumNode.php b/src/Symfony/Component/Config/Definition/EnumNode.php index 224871ab6f..9b4c4156e3 100644 --- a/src/Symfony/Component/Config/Definition/EnumNode.php +++ b/src/Symfony/Component/Config/Definition/EnumNode.php @@ -25,8 +25,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.'); + if (empty($values)) { + throw new \InvalidArgumentException('$values must contain at least one element.'); } parent::__construct($name, $parent); diff --git a/src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php b/src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php index 2b84de6b09..654d5050dd 100644 --- a/src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php +++ b/src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php @@ -23,10 +23,23 @@ class EnumNodeTest extends \PHPUnit_Framework_TestCase /** * @expectedException \InvalidArgumentException + * @expectedExceptionMessage $values must contain at least one element. */ + public function testConstructionWithNoValues() + { + new EnumNode('foo', null, array()); + } + 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')); } /**