Skip to content

Commit 3c3f565

Browse files
Merge branch '3.4'
* 3.4: [FrameworkBundle] Commands as a service [Config] Enable cannotBeEmpty along with requiresAtLeastOneElement Remove leading 0 in ms of date caster [Workflow] feature: add getter in workflow [Workflow] do not emit not needed guard events add groups support to the Valid constraint
2 parents f5d896a + d668d8c commit 3c3f565

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Definition/Builder/ArrayNodeDefinition.php

Lines changed: 7 additions & 1 deletion
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)

Tests/Definition/Builder/ArrayNodeDefinitionTest.php

Lines changed: 27 additions & 0 deletions
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)