Skip to content

[Config] Enable cannotBeEmpty along with requiresAtLeastOneElement #20361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ protected function createNode()
$node->setKeyAttribute($this->key, $this->removeKeyItem);
}

if (true === $this->atLeastOne) {
if (true === $this->atLeastOne || false === $this->allowEmptyValue) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug fix?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or given this bug, shouldn't we not fix it (to prevent any bc break), and deprecate cannotBeEmpty instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine either way... the numeric node does that actually.

Personally i'd favor Array::cannotBeEmpty over requiresAtLeastOneElement, but maybe that's just me :)

Maybe provide requiresAtLeastOneElement as an alias to cannotBeEmpty?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd favor simplicity her, taking history into account, thus stick to requiresAtLeastOneElement and make the PR as small as possible. It's not like requiresAtLeastOneElement is a bad name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. Deprecating cannotBeEmpty means it should throw in 4.0 right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or just be removed if we can

Copy link
Contributor Author

@ro0NL ro0NL Mar 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Removed the deprecation (worth it?) so both requiresAtLeastOneElement and cannotBeEmpty act the same. We cannot really deprecate/remove cannotBeEmpty as it comes from the base class (not sure this should be reversed in any way from a sub class).

So either deprecate requiresAtLeastOneElement in favor of cannotBeEmpty or support both like i did.

Or indeed throw from ArrayNode::cannotBeEmpty.. but im not sure it's worth the hassle.

$node->setMinNumberOfElements(1);
}

Expand Down Expand Up @@ -490,6 +490,12 @@ protected function validateConcreteNode(ArrayNode $node)
);
}

if (false === $this->allowEmptyValue) {
throw new InvalidDefinitionException(
sprintf('->cannotBeEmpty() is not applicable to concrete nodes at path "%s"', $path)
);
}

if (true === $this->atLeastOne) {
throw new InvalidDefinitionException(
sprintf('->requiresAtLeastOneElement() is not applicable to concrete nodes at path "%s"', $path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function providePrototypeNodeSpecificCalls()
array('defaultValue', array(array())),
array('addDefaultChildrenIfNoneSet', array()),
array('requiresAtLeastOneElement', array()),
array('cannotBeEmpty', array()),
array('useAttributeAsKey', array('foo')),
);
}
Expand Down Expand Up @@ -285,6 +286,32 @@ public function getEnableableNodeFixtures()
);
}

public function testRequiresAtLeastOneElement()
{
$node = new ArrayNodeDefinition('root');
$node
->requiresAtLeastOneElement()
->integerPrototype();

$node->getNode()->finalize(array(1));

$this->addToAssertionCount(1);
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage The path "root" should have at least 1 element(s) defined.
*/
public function testCannotBeEmpty()
{
$node = new ArrayNodeDefinition('root');
$node
->cannotBeEmpty()
->integerPrototype();

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

protected function getField($object, $field)
{
$reflection = new \ReflectionProperty($object, $field);
Expand Down