diff --git a/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php b/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php index c8d929e951..170b466984 100644 --- a/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php +++ b/src/Symfony/Component/Config/Definition/Builder/ArrayNodeDefinition.php @@ -424,7 +424,11 @@ class ArrayNodeDefinition extends NodeDefinition implements ParentNodeDefinition $node->setKeyAttribute($this->key, $this->removeKeyItem); } - if (true === $this->atLeastOne || false === $this->allowEmptyValue) { + if (false === $this->allowEmptyValue) { + @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); + } + + if (true === $this->atLeastOne) { $node->setMinNumberOfElements(1); } @@ -486,9 +490,7 @@ class ArrayNodeDefinition extends NodeDefinition implements ParentNodeDefinition } if (false === $this->allowEmptyValue) { - throw new InvalidDefinitionException( - sprintf('->cannotBeEmpty() is not applicable to concrete nodes at path "%s"', $path) - ); + @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); } if (true === $this->atLeastOne) { diff --git a/src/Symfony/Component/Config/Tests/Definition/Builder/ArrayNodeDefinitionTest.php b/src/Symfony/Component/Config/Tests/Definition/Builder/ArrayNodeDefinitionTest.php index e32c093eeb..dd6a6317e6 100644 --- a/src/Symfony/Component/Config/Tests/Definition/Builder/ArrayNodeDefinitionTest.php +++ b/src/Symfony/Component/Config/Tests/Definition/Builder/ArrayNodeDefinitionTest.php @@ -54,7 +54,6 @@ class ArrayNodeDefinitionTest extends TestCase array('defaultValue', array(array())), array('addDefaultChildrenIfNoneSet', array()), array('requiresAtLeastOneElement', array()), - array('cannotBeEmpty', array()), array('useAttributeAsKey', array('foo')), ); } @@ -298,6 +297,20 @@ class ArrayNodeDefinitionTest extends TestCase $this->addToAssertionCount(1); } + /** + * @group legacy + * @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. + */ + public function testCannotBeEmpty() + { + $node = new ArrayNodeDefinition('root'); + $node + ->cannotBeEmpty() + ->integerPrototype(); + + $node->getNode()->finalize(array()); + } + public function testSetDeprecated() { $node = new ArrayNodeDefinition('root'); @@ -313,15 +326,13 @@ class ArrayNodeDefinitionTest extends TestCase } /** - * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException - * @expectedExceptionMessage The path "root" should have at least 1 element(s) defined. + * @group legacy + * @expectedDeprecation ->cannotBeEmpty() is not applicable to concrete nodes at path "root". In 4.0 it will throw an exception. */ - public function testCannotBeEmpty() + public function testCannotBeEmptyOnConcreteNode() { $node = new ArrayNodeDefinition('root'); - $node - ->cannotBeEmpty() - ->integerPrototype(); + $node->cannotBeEmpty(); $node->getNode()->finalize(array()); }