Skip to content

[Config] Allow using defaultNull() on BooleanNodeDefinition #58490

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

Merged
merged 1 commit into from
Oct 9, 2024
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Config/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add `#[WhenNot]` attribute to prevent service from being registered in a specific environment
* Generate a meta file in JSON format for resource tracking
* Add `SkippingResourceChecker`
* Add support for `defaultNull()` on `BooleanNode`

7.1
---
Expand Down
15 changes: 14 additions & 1 deletion src/Symfony/Component/Config/Definition/BooleanNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,23 @@
*/
class BooleanNode extends ScalarNode
{
public function __construct(
?string $name,
?NodeInterface $parent = null,
string $pathSeparator = self::DEFAULT_PATH_SEPARATOR,
private bool $nullable = false,
) {
parent::__construct($name, $parent, $pathSeparator);
}

protected function validateType(mixed $value): void
{
if (!\is_bool($value)) {
$ex = new InvalidTypeException(\sprintf('Invalid type for path "%s". Expected "bool", but got "%s".', $this->getPath(), get_debug_type($value)));
if (null === $value && $this->nullable) {
return;
}

$ex = new InvalidTypeException(\sprintf('Invalid type for path "%s". Expected "bool%s", but got "%s".', $this->getPath(), $this->nullable ? '" or "null' : '', get_debug_type($value)));
if ($hint = $this->getInfo()) {
$ex->addHint($hint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(?string $name, ?NodeParentInterface $parent = null)
*/
protected function instantiateNode(): BooleanNode
{
return new BooleanNode($this->name, $this->parent, $this->pathSeparator);
return new BooleanNode($this->name, $this->parent, $this->pathSeparator, null === $this->nullEquivalent);
}

/**
Expand All @@ -43,4 +43,20 @@ public function cannotBeEmpty(): static
{
throw new InvalidDefinitionException('->cannotBeEmpty() is not applicable to BooleanNodeDefinition.');
}

public function defaultNull(): static
{
$this->nullEquivalent = null;

return parent::defaultNull();
}

public function defaultValue(mixed $value): static
{
if (null === $value) {
$this->nullEquivalent = null;
}

return parent::defaultValue($value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,33 @@ public function testNormalize(bool $value)
$this->assertSame($value, $node->normalize($value));
}

public function testNullValueOnNullable()
{
$node = new BooleanNode('test', null, '.', true);

$this->assertNull($node->normalize(null));
}

public function testNullValueOnNotNullable()
{
$node = new BooleanNode('test', null, '.', false);

$this->expectException(InvalidTypeException::class);
$this->expectExceptionMessage('Invalid type for path "test". Expected "bool", but got "null".');

$this->assertNull($node->normalize(null));
}

public function testInvalidValueOnNullable()
{
$node = new BooleanNode('test', null, '.', true);

$this->expectException(InvalidTypeException::class);
$this->expectExceptionMessage('Invalid type for path "test". Expected "bool" or "null", but got "int".');

$node->normalize(123);
}

/**
* @dataProvider getValidValues
*/
Expand Down Expand Up @@ -60,7 +87,6 @@ public function testNormalizeThrowsExceptionOnInvalidValues($value)
public static function getInvalidValues(): array
{
return [
[null],
[''],
['foo'],
[0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ public function testCannotBeEmptyThrowsAnException()
$def->cannotBeEmpty();
}

public function testBooleanNodeWithDefaultNull()
{
$def = new BooleanNodeDefinition('foo');
$def->defaultNull();

$node = $def->getNode();
$this->assertTrue($node->hasDefaultValue());
$this->assertNull($node->getDefaultValue());

$this->assertNull($node->normalize(null));
}

public function testBooleanNodeWithDefaultValueAtNull()
{
$def = new BooleanNodeDefinition('foo');
$def->defaultValue(null);

$node = $def->getNode();
$this->assertTrue($node->hasDefaultValue());
$this->assertNull($node->getDefaultValue());

$this->assertNull($node->normalize(null));
}

public function testSetDeprecated()
{
$def = new BooleanNodeDefinition('foo');
Expand Down