Skip to content

Enableable ArrayNodeDefinition is disabled for empty configuration #25874

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 4 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 @@ -226,7 +226,9 @@ public function canBeEnabled()
->beforeNormalization()
->ifArray()
->then(function ($v) {
$v['enabled'] = isset($v['enabled']) ? $v['enabled'] : true;
if (!isset($v['enabled'])) {
$v['enabled'] = !empty($v);
}

return $v;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ public function testCanBeDisabled()
$this->assertTrue($this->getField($enabledNode, 'defaultValue'));
}

public function testEnableableNodeIsDisabledForEmptyConfigurationWhenNormalized()
{
$config = array();

$node = new ArrayNodeDefinition('root');
$node->canBeEnabled();

$this->assertEquals(
array('enabled' => false),
$node->getNode()->normalize($config),
'An enableable node is disabled by default'
);
}

public function testIgnoreExtraKeys()
{
$node = new ArrayNodeDefinition('root');
Expand Down Expand Up @@ -240,6 +254,7 @@ public function getEnableableNodeFixtures()
array(array('enabled' => true, 'foo' => 'baz'), array(array('foo' => 'baz')), 'any configuration enables an enableable node'),
array(array('enabled' => false, 'foo' => 'baz'), array(array('foo' => 'baz', 'enabled' => false)), 'An enableable node can be disabled'),
array(array('enabled' => false, 'foo' => 'bar'), array(false), 'false disables an enableable node'),
array(array('enabled' => false, 'foo' => 'bar'), array(), 'enableable node is disabled by default'),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Config\Tests\Definition\Builder;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\Tests\Fixtures\Builder\NodeBuilder as CustomNodeBuilder;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;

Expand Down Expand Up @@ -131,4 +132,22 @@ public function testDefinitionExampleGetsTransferredToNode()
$this->assertInternalType('array', $tree->getExample());
$this->assertEquals('example', $children['child']->getExample());
}

public function testRootNodeThatCanBeEnabledIsDisabledByDefault()
{
$builder = new TreeBuilder();

$builder->root('test')
->canBeEnabled();

$tree = $builder->buildTree();
$children = $tree->getChildren();

$this->assertFalse($children['enabled']->getDefaultValue());

$processor = new Processor();
$result = $processor->process($tree, array());

$this->assertEquals(array('enabled' => false), $result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ private function loadFromExtensions(array $content)
continue;
}

if (null === $values) {
$values = array('enabled' => null);
}

if (!is_array($values)) {
$values = array();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use Symfony\Component\Config\Definition\Builder\TreeBuilder;

final class FooConfiguration implements \Symfony\Component\Config\Definition\ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$builder = new TreeBuilder();
$rootNode = $builder->root('foo');
$rootNode->canBeEnabled();

return $builder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;

final class FooExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration(new \FooConfiguration(), $configs);

$container->setParameter('foo_extension_enabled', $config['enabled']);
}

public function getAlias()
{
return 'foo';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: ~
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public static function setUpBeforeClass()
self::$fixturesPath = realpath(__DIR__.'/../Fixtures/');
require_once self::$fixturesPath.'/includes/foo.php';
require_once self::$fixturesPath.'/includes/ProjectExtension.php';
require_once self::$fixturesPath.'/includes/FooExtension.php';
require_once self::$fixturesPath.'/includes/FooConfiguration.php';
}

/**
Expand Down Expand Up @@ -207,6 +209,19 @@ public function testExtensions()
}
}

public function testNullableExtension()
{
$container = new ContainerBuilder();
$container->registerExtension(new \FooExtension());
$loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml'));
$loader->load('extension_nullable.yml');
$container->compile();

$isEnabled = $container->getParameter('foo_extension_enabled');

$this->assertTrue($isEnabled);
}

public function testSupports()
{
$loader = new YamlFileLoader(new ContainerBuilder(), new FileLocator());
Expand Down