8000 [Config] Fix cannotBeEmpty() · symfony/symfony@2269f70 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2269f70

Browse files
committed
[Config] Fix cannotBeEmpty()
1 parent cf4eb46 commit 2269f70

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,11 @@ protected function createNode()
424424
$node->setKeyAttribute($this->key, $this->removeKeyItem);
425425
}
426426

427-
if (true === $this->atLeastOne || false === $this->allowEmptyValue) {
427+
if (false === $this->allowEmptyValue) {
428+
@trigger_error(sprintf('Using %s::cannotBeEmpty() at path "%s" has no effect, consider requiresAtLeastOneElement() instead. In 4.0 both methods will behave the same.', __CLASS__, $node->getPath()), E_USER_DEPRECATED);
429+
}
430+
431+
if (true === $this->atLeastOne) {
428432
$node->setMinNumberOfElements(1);
429433
}
430434

@@ -486,9 +490,7 @@ protected function validateConcreteNode(ArrayNode $node)
486490
}
487491

488492
if (false === $this->allowEmptyValue) {
489-
throw new InvalidDefinitionException(
490-
sprintf('->cannotBeEmpty() is not applicable to concrete nodes at path "%s"', $path)
491-
);
493+
@trigger_error(sprintf('->cannotBeEmpty() is not applicable to concrete nodes at path "%s". In 4.0 it will throw an exception.', $path), E_USER_DEPRECATED);
492494
}
493495

494496
if (true === $this->atLeastOne) {

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public function providePrototypeNodeSpecificCalls()
5454
array('defaultValue', array(array())),
5555
array('addDefaultChildrenIfNoneSet', array()),
5656
array('requiresAtLeastOneElement', array()),
57-
array('cannotBeEmpty', array()),
5857
array('useAttributeAsKey', array('foo')),
5958
);
6059
}
@@ -298,6 +297,20 @@ public function testRequiresAtLeastOneElement()
298297
$this->addToAssertionCount(1);
299298
}
300299

300+
/**
301+
* @group legacy
302+
* @expectedDeprecation Using Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition::cannotBeEmpty() at path "root" has no effect, consider requiresAtLeastOneElement() instead. In 4.0 both methods will behave the same.
303+
*/
304+
public function testCannotBeEmpty()
305+
{
306+
$node = new ArrayNodeDefinition('root');
307+
$node
308+
->cannotBeEmpty()
309+
->integerPrototype();
310+
311+
$node->getNode()->finalize(array());
312+
}
313+
301314
public function testSetDeprecated()
302315
{
303316
$node = new ArrayNodeDefinition('root');
@@ -313,15 +326,13 @@ public function testSetDeprecated()
313326
}
314327

315328
/**
316-
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
317-
* @expectedExceptionMessage The path "root" should have at least 1 element(s) defined.
329+
* @group legacy
330+
* @expectedDeprecation ->cannotBeEmpty() is not applicable to concrete nodes at path "root". In 4.0 it will throw an exception.
318331
*/
319-
public function testCannotBeEmpty()
332+
public function testCannotBeEmptyOnConcreteNode()
320333
{
321334
$node = new ArrayNodeDefinition('root');
322-
$node
323-
->cannotBeEmpty()
324-
->integerPrototype();
335+
$node->cannotBeEmpty();
325336

326337
$node->getNode()->finalize(array());
327338
}

0 commit comments

Comments
 (0)
0