bug #24633 [Config] Fix cannotBeEmpty() (ro0NL)

This PR was merged into the 3.4 branch.

Discussion
----------

[Config] Fix cannotBeEmpty()

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #24614
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!--highly recommended for new features-->

Open for better deprecation message, but this should clarify.

cc @iltar

Commits
-------

2269f70180 [Config] Fix cannotBeEmpty()
This commit is contained in:
Fabien Potencier 2017-11-10 07:14:21 -08:00
commit b7928c3f27
2 changed files with 24 additions and 11 deletions

View File

@ -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) {

View File

@ -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());
}