Skip to content

Commit

Permalink
bug #24633 [Config] Fix cannotBeEmpty() (ro0NL)
Browse files Browse the repository at this point in the history
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
-------

2269f70 [Config] Fix cannotBeEmpty()
  • Loading branch information
fabpot committed Nov 10, 2017
2 parents cf4eb46 + 2269f70 commit b7928c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Expand Up @@ -424,7 +424,11 @@ protected function createNode()
$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);
}

Expand Down Expand Up @@ -486,9 +490,7 @@ protected function validateConcreteNode(ArrayNode $node)
}

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) {
Expand Down
Expand Up @@ -54,7 +54,6 @@ public function providePrototypeNodeSpecificCalls()
array('defaultValue', array(array())),
array('addDefaultChildrenIfNoneSet', array()),
array('requiresAtLeastOneElement', array()),
array('cannotBeEmpty', array()),
array('useAttributeAsKey', array('foo')),
);
}
Expand Down Expand Up @@ -298,6 +297,20 @@ public function testRequiresAtLeastOneElement()
$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');
Expand All @@ -313,15 +326,13 @@ public function testSetDeprecated()
}

/**
* @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());
}
Expand Down

0 comments on commit b7928c3

Please sign in to comment.