4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Tests\Definition;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Definition\NodeInterface;
16 use Symfony\Component\Config\Definition\Builder\TreeBuilder;
18 class NormalizationTest extends TestCase
21 * @dataProvider getEncoderTests
23 public function testNormalizeEncoders($denormalized)
25 $tb = new TreeBuilder();
27 ->root('root_name', 'array')
28 ->fixXmlConfig('encoder')
30 ->node('encoders', 'array')
31 ->useAttributeAsKey('class')
33 ->beforeNormalization()->ifString()->then(function ($v) { return array('algorithm' => $v); })->end()
35 ->node('algorithm', 'scalar')->end()
46 'foo' => array('algorithm' => 'plaintext'),
50 $this->assertNormalized($tree, $denormalized, $normalized);
53 public function getEncoderTests()
60 array('class' => 'foo', 'algorithm' => 'plaintext'),
64 // XML when only one element of this type
66 'encoder' => array('class' => 'foo', 'algorithm' => 'plaintext'),
72 array('class' => 'foo', 'algorithm' => 'plaintext'),
86 'foo' => array('algorithm' => 'plaintext'),
90 return array_map(function ($v) {
96 * @dataProvider getAnonymousKeysTests
98 public function testAnonymousKeysArray($denormalized)
100 $tb = new TreeBuilder();
102 ->root('root', 'array')
104 ->node('logout', 'array')
105 ->fixXmlConfig('handler')
107 ->node('handlers', 'array')
108 ->prototype('scalar')->end()
117 $normalized = array('logout' => array('handlers' => array('a', 'b', 'c')));
119 $this->assertNormalized($tree, $denormalized, $normalized);
122 public function getAnonymousKeysTests()
128 'handlers' => array('a', 'b', 'c'),
134 'handler' => array('a', 'b', 'c'),
138 return array_map(function ($v) { return array($v); }, $configs);
142 * @dataProvider getNumericKeysTests
144 public function testNumericKeysAsAttributes($denormalized)
147 'thing' => array(42 => array('foo', 'bar'), 1337 => array('baz', 'qux')),
150 $this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, $normalized);
153 public function getNumericKeysTests()
159 42 => array('foo', 'bar'), 1337 => array('baz', 'qux'),
165 array('foo', 'bar', 'id' => 42), array('baz', 'qux', 'id' => 1337),
169 return array_map(function ($v) { return array($v); }, $configs);
173 * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
174 * @expectedExceptionMessage The attribute "id" must be set for path "root.thing".
176 public function testNonAssociativeArrayThrowsExceptionIfAttributeNotSet()
178 $denormalized = array(
180 array('foo', 'bar'), array('baz', 'qux'),
184 $this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, array());
187 public function testAssociativeArrayPreserveKeys()
189 $tb = new TreeBuilder();
191 ->root('root', 'array')
194 ->node('foo', 'scalar')->end()
201 $data = array('first' => array('foo' => 'bar'));
203 $this->assertNormalized($tree, $data, $data);
206 public static function assertNormalized(NodeInterface $tree, $denormalized, $normalized)
208 self::assertSame($normalized, $tree->normalize($denormalized));
211 private function getNumericKeysTestTree()
213 $tb = new TreeBuilder();
215 ->root('root', 'array')
217 ->node('thing', 'array')
218 ->useAttributeAsKey('id')
220 ->prototype('scalar')->end()