NormalizationTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Config\Tests\Definition;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  13. use Symfony\Component\Config\Definition\NodeInterface;
  14. class NormalizationTest extends TestCase
  15. {
  16. /**
  17. * @dataProvider getEncoderTests
  18. */
  19. public function testNormalizeEncoders($denormalized)
  20. {
  21. $tb = new TreeBuilder('root_name', 'array');
  22. $tree = $tb
  23. ->getRootNode()
  24. ->fixXmlConfig('encoder')
  25. ->children()
  26. ->node('encoders', 'array')
  27. ->useAttributeAsKey('class')
  28. ->prototype('array')
  29. ->beforeNormalization()->ifString()->then(function ($v) { return ['algorithm' => $v]; })->end()
  30. ->children()
  31. ->node('algorithm', 'scalar')->end()
  32. ->end()
  33. ->end()
  34. ->end()
  35. ->end()
  36. ->end()
  37. ->buildTree()
  38. ;
  39. $normalized = [
  40. 'encoders' => [
  41. 'foo' => ['algorithm' => 'plaintext'],
  42. ],
  43. ];
  44. $this->assertNormalized($tree, $denormalized, $normalized);
  45. }
  46. public function getEncoderTests()
  47. {
  48. $configs = [];
  49. // XML
  50. $configs[] = [
  51. 'encoder' => [
  52. ['class' => 'foo', 'algorithm' => 'plaintext'],
  53. ],
  54. ];
  55. // XML when only one element of this type
  56. $configs[] = [
  57. 'encoder' => ['class' => 'foo', 'algorithm' => 'plaintext'],
  58. ];
  59. // YAML/PHP
  60. $configs[] = [
  61. 'encoders' => [
  62. ['class' => 'foo', 'algorithm' => 'plaintext'],
  63. ],
  64. ];
  65. // YAML/PHP
  66. $configs[] = [
  67. 'encoders' => [
  68. 'foo' => 'plaintext',
  69. ],
  70. ];
  71. // YAML/PHP
  72. $configs[] = [
  73. 'encoders' => [
  74. 'foo' => ['algorithm' => 'plaintext'],
  75. ],
  76. ];
  77. return array_map(function ($v) {
  78. return [$v];
  79. }, $configs);
  80. }
  81. /**
  82. * @dataProvider getAnonymousKeysTests
  83. */
  84. public function testAnonymousKeysArray($denormalized)
  85. {
  86. $tb = new TreeBuilder('root', 'array');
  87. $tree = $tb
  88. ->getRootNode()
  89. ->children()
  90. ->node('logout', 'array')
  91. ->fixXmlConfig('handler')
  92. ->children()
  93. ->node('handlers', 'array')
  94. ->prototype('scalar')->end()
  95. ->end()
  96. ->end()
  97. ->end()
  98. ->end()
  99. ->end()
  100. ->buildTree()
  101. ;
  102. $normalized = ['logout' => ['handlers' => ['a', 'b', 'c']]];
  103. $this->assertNormalized($tree, $denormalized, $normalized);
  104. }
  105. public function getAnonymousKeysTests()
  106. {
  107. $configs = [];
  108. $configs[] = [
  109. 'logout' => [
  110. 'handlers' => ['a', 'b', 'c'],
  111. ],
  112. ];
  113. $configs[] = [
  114. 'logout' => [
  115. 'handler' => ['a', 'b', 'c'],
  116. ],
  117. ];
  118. return array_map(function ($v) { return [$v]; }, $configs);
  119. }
  120. /**
  121. * @dataProvider getNumericKeysTests
  122. */
  123. public function testNumericKeysAsAttributes($denormalized)
  124. {
  125. $normalized = [
  126. 'thing' => [42 => ['foo', 'bar'], 1337 => ['baz', 'qux']],
  127. ];
  128. $this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, $normalized);
  129. }
  130. public function getNumericKeysTests()
  131. {
  132. $configs = [];
  133. $configs[] = [
  134. 'thing' => [
  135. 42 => ['foo', 'bar'], 1337 => ['baz', 'qux'],
  136. ],
  137. ];
  138. $configs[] = [
  139. 'thing' => [
  140. ['foo', 'bar', 'id' => 42], ['baz', 'qux', 'id' => 1337],
  141. ],
  142. ];
  143. return array_map(function ($v) { return [$v]; }, $configs);
  144. }
  145. /**
  146. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  147. * @expectedExceptionMessage The attribute "id" must be set for path "root.thing".
  148. */
  149. public function testNonAssociativeArrayThrowsExceptionIfAttributeNotSet()
  150. {
  151. $denormalized = [
  152. 'thing' => [
  153. ['foo', 'bar'], ['baz', 'qux'],
  154. ],
  155. ];
  156. $this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, []);
  157. }
  158. public function testAssociativeArrayPreserveKeys()
  159. {
  160. $tb = new TreeBuilder('root', 'array');
  161. $tree = $tb
  162. ->getRootNode()
  163. ->prototype('array')
  164. ->children()
  165. ->node('foo', 'scalar')->end()
  166. ->end()
  167. ->end()
  168. ->end()
  169. ->buildTree()
  170. ;
  171. $data = ['first' => ['foo' => 'bar']];
  172. $this->assertNormalized($tree, $data, $data);
  173. }
  174. public static function assertNormalized(NodeInterface $tree, $denormalized, $normalized)
  175. {
  176. self::assertSame($normalized, $tree->normalize($denormalized));
  177. }
  178. private function getNumericKeysTestTree()
  179. {
  180. $tb = new TreeBuilder('root', 'array');
  181. $tree = $tb
  182. ->getRootNode()
  183. ->children()
  184. ->node('thing', 'array')
  185. ->useAttributeAsKey('id')
  186. ->prototype('array')
  187. ->prototype('scalar')->end()
  188. ->end()
  189. ->end()
  190. ->end()
  191. ->end()
  192. ->buildTree()
  193. ;
  194. return $tree;
  195. }
  196. }