Skip to content

Commit 73d3d5a

Browse files
committed
feature #20361 [Config] Enable cannotBeEmpty along with requiresAtLeastOneElement (ro0NL)
This PR was squashed before being merged into the 3.4 branch (closes #20361). Discussion ---------- [Config] Enable cannotBeEmpty along with requiresAtLeastOneElement | Q | A | | --- | --- | | Branch? | "master" | | Bug fix? | no | | New feature? | yes | | BC breaks? | no | | Deprecations? | no | | Tests pass? | yes | Fixed tickets | #20356 | | License | MIT | | Doc PR | reference to the documentation PR, if any | As @vudaltsov mentioned, we ignore any calls to `ArrayNodeDefinition::cannotBeEmpty`, which can lead to unexpected behavior. Imo. all subclasses should follow the base API. Commits ------- d40e7e4 [Config] Enable cannotBeEmpty along with requiresAtLeastOneElement
2 parents 3b54ce8 + d40e7e4 commit 73d3d5a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ protected function createNode()
428428
$node->setKeyAttribute($this->key, $this->removeKeyItem);
429429
}
430430

431-
if (true === $this->atLeastOne) {
431+
if (true === $this->atLeastOne || false === $this->allowEmptyValue) {
432432
$node->setMinNumberOfElements(1);
433433
}
434434

@@ -490,6 +490,12 @@ protected function validateConcreteNode(ArrayNode $node)
490490
);
491491
}
492492

493+
if (false === $this->allowEmptyValue) {
494+
throw new InvalidDefinitionException(
495+
sprintf('->cannotBeEmpty() is not applicable to concrete nodes at path "%s"', $path)
496+
);
497+
}
498+
493499
if (true === $this->atLeastOne) {
494500
throw new InvalidDefinitionException(
495501
sprintf('->requiresAtLeastOneElement() is not applicable to concrete nodes at path "%s"', $path)

src/Symfony/Component/Config/Tests/Definition/Builder/ArrayNodeDefinitionTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function providePrototypeNodeSpecificCalls()
5454
array('defaultValue', array(array())),
5555
array('addDefaultChildrenIfNoneSet', array()),
5656
array('requiresAtLeastOneElement', array()),
57+
array('cannotBeEmpty', array()),
5758
array('useAttributeAsKey', array('foo')),
5859
);
5960
}
@@ -285,6 +286,32 @@ public function getEnableableNodeFixtures()
285286
);
286287
}
287288

289+
public function testRequiresAtLeastOneElement()
290+
{
291+
$node = new ArrayNodeDefinition('root');
292+
$node
293+
->requiresAtLeastOneElement()
294+
->integerPrototype();
295+
296+
$node->getNode()->finalize(array(1));
297+
298+
$this->addToAssertionCount(1);
299+
}
300+
301+
/**
302+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
303+
* @expectedExceptionMessage The path "root" should have at least 1 element(s) defined.
304+
*/
305+
public function testCannotBeEmpty()
306+
{
307+
$node = new ArrayNodeDefinition('root');
308+
$node
309+
->cannotBeEmpty()
310+
->integerPrototype();
311+
312+
$node->getNode()->finalize(array());
313+
}
314+
288315
protected function getField($object, $field)
289316
{
290317
$reflection = new \ReflectionProperty($object, $field);

0 commit comments

Comments
 (0)