From d40e7e4c2de1c4468714de8006f84aee287aace8 Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Sun, 30 Oct 2016 08:36:08 +0000 Subject: [PATCH] [Config] Enable cannotBeEmpty along with requiresAtLeastOneElement --- .../Builder/ArrayNodeDefinition.php | 8 +++++- .../Builder/ArrayNodeDefinitionTest.php | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php b/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php index 9db0b3676763..cac53044a9a1 100644 --- a/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php +++ b/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php @@ -428,7 +428,7 @@ protected function createNode() $node->setKeyAttribute($this->key, $this->removeKeyItem); } - if (true === $this->atLeastOne) { + if (true === $this->atLeastOne || false === $this->allowEmptyValue) { $node->setMinNumberOfElements(1); } @@ -490,6 +490,12 @@ protected function validateConcreteNode(ArrayNode $node) ); } + if (false === $this->allowEmptyValue) { + throw new InvalidDefinitionException( + sprintf('->cannotBeEmpty() is not applicable to concrete nodes at path "%s"', $path) + ); + } + if (true === $this->atLeastOne) { throw new InvalidDefinitionException( sprintf('->requiresAtLeastOneElement() is not applicable to concrete nodes at path "%s"', $path) diff --git a/src/Symfony/Component/Config/Tests/Definition/Builder/ArrayNodeDefinitionTest.php b/src/Symfony/Component/Config/Tests/Definition/Builder/ArrayNodeDefinitionTest.php index f2a32351a84a..4c2a397cff6d 100644 --- a/src/Symfony/Component/Config/Tests/Definition/Builder/ArrayNodeDefinitionTest.php +++ b/src/Symfony/Component/Config/Tests/Definition/Builder/ArrayNodeDefinitionTest.php @@ -54,6 +54,7 @@ public function providePrototypeNodeSpecificCalls() array('defaultValue', array(array())), array('addDefaultChildrenIfNoneSet', array()), array('requiresAtLeastOneElement', array()), + array('cannotBeEmpty', array()), array('useAttributeAsKey', array('foo')), ); } @@ -285,6 +286,32 @@ public function getEnableableNodeFixtures() ); } + public function testRequiresAtLeastOneElement() + { + $node = new ArrayNodeDefinition('root'); + $node + ->requiresAtLeastOneElement() + ->integerPrototype(); + + $node->getNode()->finalize(array(1)); + + $this->addToAssertionCount(1); + } + + /** + * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException + * @expectedExceptionMessage The path "root" should have at least 1 element(s) defined. + */ + public function testCannotBeEmpty() + { + $node = new ArrayNodeDefinition('root'); + $node + ->cannotBeEmpty() + ->integerPrototype(); + + $node->getNode()->finalize(array()); + } + protected function getField($object, $field) { $reflection = new \ReflectionProperty($object, $field);