Skip to content

Commit 64d0507

Browse files
committed
feature #15433 Allow to define Enum nodes with 1 single element (javiereguiluz)
This PR was squashed before being merged into the 2.8 branch (closes #15433). Discussion ---------- Allow to define Enum nodes with 1 single element | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | #15402 | License | MIT | Doc PR | - Commits ------- 68f0818 Allow to define Enum nodes with 1 single element
2 parents 6888c1f + 68f0818 commit 64d0507

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

UPGRADE-2.8.md

+20
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,23 @@ FrameworkBundle
399399
session:
400400
cookie_httponly: false
401401
```
402+
403+
Config
404+
------
405+
406+
The edge case of defining just one value for nodes of type Enum is now allowed:
407+
408+
```php
409+
$rootNode
410+
->children()
411+
->enumNode('variable')
412+
->values(array('value'))
413+
->end()
414+
->end()
415+
;
416+
```
417+
418+
Before: `InvalidArgumentException` (variable must contain at least two
419+
distinct elements).
420+
After: the code will work as expected and it will restrict the values of the
421+
`variable` option to just `value`.

src/Symfony/Component/Config/Definition/EnumNode.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class EnumNode extends ScalarNode
2525
public function __construct($name, NodeInterface $parent = null, array $values = array())
2626
{
2727
$values = array_unique($values);
28-
if (count($values) <= 1) {
29-
throw new \InvalidArgumentException('$values must contain at least two distinct elements.');
28+
if (empty($values)) {
29+
throw new \InvalidArgumentException('$values must contain at least one element.');
3030
}
3131

3232
parent::__construct($name, $parent);

src/Symfony/Component/Config/Tests/Definition/EnumNodeTest.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,23 @@ public function testFinalizeValue()
2323

2424
/**
2525
* @expectedException \InvalidArgumentException
26+
* @expectedExceptionMessage $values must contain at least one element.
2627
*/
28+
public function testConstructionWithNoValues()
29+
{
30+
new EnumNode('foo', null, array());
31+
}
32+
2733
public function testConstructionWithOneValue()
2834
{
29-
new EnumNode('foo', null, array('foo', 'foo'));
35+
$node = new EnumNode('foo', null, array('foo'));
36+
$this->assertSame('foo', $node->finalize('foo'));
37+
}
38+
39+
public function testConstructionWithOneDistinctValue()
40+
{
41+
$node = new EnumNode('foo', null, array('foo', 'foo'));
42+
$this->assertSame('foo', $node->finalize('foo'));
3043
}
3144

3245
/**

0 commit comments

Comments
 (0)