This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/Config/Definition/NormalizationTest.php

145 lines
3.9 KiB
PHP
Raw Normal View History

<?php
2011-05-31 09:57:06 +01:00
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Symfony\Tests\Component\Config\Definition;
use Symfony\Component\Config\Definition\NodeInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
class NormalizerTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider getEncoderTests
*/
public function testNormalizeEncoders($denormalized)
{
$tb = new TreeBuilder();
$tree = $tb
->root('root_name', 'array')
[Security/DependencyInjection] adds support for merging security configurations The merging is done in three steps: 1. Normalization: ================= All passed config arrays will be transformed into the same structure regardless of what format they come from. 2. Merging: =========== This is the step when the actual merging is performed. Starting at the root the configs will be passed along the tree until a node has no children, or the merging of sub-paths of the current node has been specifically disabled. Left-Side Right-Side Merge Result ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -nothing- array Right-Side will be taken. scalar scalar Right-Side will be taken. array false Right-Side will be taken if ->canBeUnset() was called on the array node. false array Right-Side will be taken. array array Each value in the array will be passed to the specific child node, or the prototype node (whatever is present). 3. Finalization: ================ The normalized, and merged config will be passed through the config tree to perform final validation on the submitted values, and set default values where this has been requested. You can influence this process in various ways, here is a list with some examples. All of these methods must be called on the node on which they should be applied. * isRequired(): Node must be present in at least one config file. * requiresAtLeastOneElement(): PrototypeNode must have at least one element. * treatNullLike($value): Replaces null with $value during normalization. * treatTrueLike($value): Same as above just for true * treatFalseLike($value): Same as above just for false * defaultValue($value): Sets a default value for this node (only for scalars) * addDefaultsIfNotSet(): Whether to add default values of an array which has not been defined in any configuration file. * disallowNewKeysInSubsequentConfigs(): All keys for this array must be defined in one configuration file, subsequent configurations may only overwrite these. * fixXmlConfig($key, $plural = null): Transforms XML config into same structure as YAML, and PHP configurations. * useAttributeAsKey($name): Defines which XML attribute to use as array key. * cannotBeOverwritten(): Declares a certain sub-path as non-overwritable. All configuration for this path must be defined in the same configuration file. * cannotBeEmpty(): If value is set, it must be non-empty. * canBeUnset(): If array values should be unset if false is specified. Architecture: ============= The configuration consists basically out of two different sets of classes. 1. Builder classes: These classes provide the fluent interface and are used to construct the config tree. 2. Node classes: These classes contain the actual logic for normalization, merging, and finalizing configurations. After you have added all the metadata to your builders, the call to ->buildTree() will convert this metadata to actual node classes. Most of the time, you will not have to interact with the config nodes directly, but will delegate this to the Processor class which will call the respective methods on the config node classes.
2011-02-04 13:37:01 +00:00
->fixXmlConfig('encoder')
[Config] Component refactoring The Config component API have changed and the extension configuration files must be updated accordingly: 1. Array nodes must enclosed their children definition in ->children() ... ->end() calls: Before: $treeBuilder->root('zend', 'array') ->arrayNode('logger') ->scalarNode('priority')->defaultValue('INFO')->end() ->booleanNode('log_errors')->defaultFalse()->end() ->end(); After: $treeBuilder->root('zend', 'array') ->children() ->arrayNode('logger') ->children() ->scalarNode('priority')->defaultValue('INFO')->end() ->booleanNode('log_errors')->defaultFalse()->end() ->end() ->end() ->end(); 2. The 'builder' method (in NodeBuilder) has been dropped in favor of an 'append' method (in ArrayNodeDefinition) Before: $treeBuilder->root('doctrine', 'array') ->arrayNode('dbal') ->builder($this->getDbalConnectionsNode()) ->end(); After: $treeBuilder->root('doctrine', 'array') ->children() ->arrayNode('dbal') ->append($this->getDbalConnectionsNode()) ->end() ->end(); 3. The root of a TreeBuilder is now an NodeDefinition (and most probably an ArrayNodeDefinition): Before: $root = $treeBuilder->root('doctrine', 'array'); $this->addDbalSection($root); public function addDbalSection(NodeBuilder $node) { ... } After: $root = $treeBuilder->root('doctrine', 'array'); $this->addDbalSection($root); public function addDbalSection(ArrayNodeDefinition $node) { ... } 4. The NodeBuilder API has changed (this is seldom used): Before: $node = new NodeBuilder('connections', 'array'); After: The recommended way is to use a tree builder: $treeBuilder = new TreeBuilder(); $node = $treeBuilder->root('connections', 'array'); An other way would be: $builder = new NodeBuilder(); $node = $builder->node('connections', 'array'); Some notes: - Tree root nodes should most always be array nodes, so this as been made the default: $treeBuilder->root('doctrine', 'array') is equivalent to $treeBuilder->root('doctrine') - There could be more than one ->children() ... ->end() sections. This could help with the readability: $treeBuilder->root('doctrine') ->children() ->scalarNode('default_connection')->end() ->end() ->fixXmlConfig('type') ->children() ->arrayNode('types') .... ->end() ->end()
2011-03-14 17:29:56 +00:00
->children()
->node('encoders', 'array')
->useAttributeAsKey('class')
->prototype('array')
->beforeNormalization()->ifString()->then(function($v) { return array('algorithm' => $v); })->end()
->children()
->node('algorithm', 'scalar')->end()
->end()
->end()
->end()
->end()
->end()
->buildTree()
;
$normalized = array(
'encoders' => array(
'foo' => array('algorithm' => 'plaintext'),
),
);
$this->assertNormalized($tree, $denormalized, $normalized);
}
public function getEncoderTests()
{
$configs = array();
// XML
$configs[] = array(
'encoder' => array(
array('class' => 'foo', 'algorithm' => 'plaintext'),
),
);
// XML when only one element of this type
$configs[] = array(
'encoder' => array('class' => 'foo', 'algorithm' => 'plaintext'),
);
// YAML/PHP
$configs[] = array(
'encoders' => array(
array('class' => 'foo', 'algorithm' => 'plaintext'),
),
);
// YAML/PHP
$configs[] = array(
'encoders' => array(
'foo' => 'plaintext',
),
);
// YAML/PHP
$configs[] = array(
'encoders' => array(
'foo' => array('algorithm' => 'plaintext'),
),
);
return array_map(function($v) {
return array($v);
}, $configs);
}
/**
* @dataProvider getAnonymousKeysTests
*/
public function testAnonymousKeysArray($denormalized)
{
$tb = new TreeBuilder();
$tree = $tb
->root('root', 'array')
[Config] Component refactoring The Config component API have changed and the extension configuration files must be updated accordingly: 1. Array nodes must enclosed their children definition in ->children() ... ->end() calls: Before: $treeBuilder->root('zend', 'array') ->arrayNode('logger') ->scalarNode('priority')->defaultValue('INFO')->end() ->booleanNode('log_errors')->defaultFalse()->end() ->end(); After: $treeBuilder->root('zend', 'array') ->children() ->arrayNode('logger') ->children() ->scalarNode('priority')->defaultValue('INFO')->end() ->booleanNode('log_errors')->defaultFalse()->end() ->end() ->end() ->end(); 2. The 'builder' method (in NodeBuilder) has been dropped in favor of an 'append' method (in ArrayNodeDefinition) Before: $treeBuilder->root('doctrine', 'array') ->arrayNode('dbal') ->builder($this->getDbalConnectionsNode()) ->end(); After: $treeBuilder->root('doctrine', 'array') ->children() ->arrayNode('dbal') ->append($this->getDbalConnectionsNode()) ->end() ->end(); 3. The root of a TreeBuilder is now an NodeDefinition (and most probably an ArrayNodeDefinition): Before: $root = $treeBuilder->root('doctrine', 'array'); $this->addDbalSection($root); public function addDbalSection(NodeBuilder $node) { ... } After: $root = $treeBuilder->root('doctrine', 'array'); $this->addDbalSection($root); public function addDbalSection(ArrayNodeDefinition $node) { ... } 4. The NodeBuilder API has changed (this is seldom used): Before: $node = new NodeBuilder('connections', 'array'); After: The recommended way is to use a tree builder: $treeBuilder = new TreeBuilder(); $node = $treeBuilder->root('connections', 'array'); An other way would be: $builder = new NodeBuilder(); $node = $builder->node('connections', 'array'); Some notes: - Tree root nodes should most always be array nodes, so this as been made the default: $treeBuilder->root('doctrine', 'array') is equivalent to $treeBuilder->root('doctrine') - There could be more than one ->children() ... ->end() sections. This could help with the readability: $treeBuilder->root('doctrine') ->children() ->scalarNode('default_connection')->end() ->end() ->fixXmlConfig('type') ->children() ->arrayNode('types') .... ->end() ->end()
2011-03-14 17:29:56 +00:00
->children()
->node('logout', 'array')
->fixXmlConfig('handler')
->children()
->node('handlers', 'array')
->prototype('scalar')->end()
->end()
->end()
->end()
->end()
->end()
->buildTree()
;
$normalized = array('logout' => array('handlers' => array('a', 'b', 'c')));
$this->assertNormalized($tree, $denormalized, $normalized);
}
public function getAnonymousKeysTests()
{
$configs = array();
$configs[] = array(
'logout' => array(
'handlers' => array('a', 'b', 'c'),
),
);
$configs[] = array(
'logout' => array(
'handler' => array('a', 'b', 'c'),
),
);
return array_map(function($v) { return array($v); }, $configs);
}
public static function assertNormalized(NodeInterface $tree, $denormalized, $normalized)
{
self::assertSame($normalized, $tree->normalize($denormalized));
}
2011-06-08 18:56:59 +01:00
}