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
This commit is contained in:
commit
64d050769b
@ -399,3 +399,23 @@ FrameworkBundle
|
|||||||
session:
|
session:
|
||||||
cookie_httponly: false
|
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`.
|
||||||
|
@ -25,8 +25,8 @@ class EnumNode extends ScalarNode
|
|||||||
public function __construct($name, NodeInterface $parent = null, array $values = array())
|
public function __construct($name, NodeInterface $parent = null, array $values = array())
|
||||||
{
|
{
|
||||||
$values = array_unique($values);
|
$values = array_unique($values);
|
||||||
if (count($values) <= 1) {
|
if (empty($values)) {
|
||||||
throw new \InvalidArgumentException('$values must contain at least two distinct elements.');
|
throw new \InvalidArgumentException('$values must contain at least one element.');
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::__construct($name, $parent);
|
parent::__construct($name, $parent);
|
||||||
|
@ -23,10 +23,23 @@ class EnumNodeTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException \InvalidArgumentException
|
||||||
|
* @expectedExceptionMessage $values must contain at least one element.
|
||||||
*/
|
*/
|
||||||
|
public function testConstructionWithNoValues()
|
||||||
|
{
|
||||||
|
new EnumNode('foo', null, array());
|
||||||
|
}
|
||||||
|
|
||||||
public function testConstructionWithOneValue()
|
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'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user