NormalizationTest.php 6.3 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();
  22. $tree = $tb
  23. ->root('root_name', 'array')
  24. ->fixXmlConfig('encoder')
  25. ->children()
  26. ->node('encoders', 'array')
  27. ->useAttributeAsKey('class')
  28. ->prototype('array')
  29. ->beforeNormalization()->ifString()->then(function ($v) { return array('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 = array(
  40. 'encoders' => array(
  41. 'foo' => array('algorithm' => 'plaintext'),
  42. ),
  43. );
  44. $this->assertNormalized($tree, $denormalized, $normalized);
  45. }
  46. public function getEncoderTests()
  47. {
  48. $configs = array();
  49. // XML
  50. $configs[] = array(
  51. 'encoder' => array(
  52. array('class' => 'foo', 'algorithm' => 'plaintext'),
  53. ),
  54. );
  55. // XML when only one element of this type
  56. $configs[] = array(
  57. 'encoder' => array('class' => 'foo', 'algorithm' => 'plaintext'),
  58. );
  59. // YAML/PHP
  60. $configs[] = array(
  61. 'encoders' => array(
  62. array('class' => 'foo', 'algorithm' => 'plaintext'),
  63. ),
  64. );
  65. // YAML/PHP
  66. $configs[] = array(
  67. 'encoders' => array(
  68. 'foo' => 'plaintext',
  69. ),
  70. );
  71. // YAML/PHP
  72. $configs[] = array(
  73. 'encoders' => array(
  74. 'foo' => array('algorithm' => 'plaintext'),
  75. ),
  76. );
  77. return array_map(function ($v) {
  78. return array($v);
  79. }, $configs);
  80. }
  81. /**
  82. * @dataProvider getAnonymousKeysTests
  83. */
  84. public function testAnonymousKeysArray($denormalized)
  85. {
  86. $tb = new TreeBuilder();
  87. $tree = $tb
  88. ->root('root', 'array')
  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 = array('logout' => array('handlers' => array('a', 'b', 'c')));
  103. $this->assertNormalized($tree, $denormalized, $normalized);
  104. }
  105. public function getAnonymousKeysTests()
  106. {
  107. $configs = array();
  108. $configs[] = array(
  109. 'logout' => array(
  110. 'handlers' => array('a', 'b', 'c'),
  111. ),
  112. );
  113. $configs[] = array(
  114. 'logout' => array(
  115. 'handler' => array('a', 'b', 'c'),
  116. ),
  117. );
  118. return array_map(function ($v) { return array($v); }, $configs);
  119. }
  120. /**
  121. * @dataProvider getNumericKeysTests
  122. */
  123. public function testNumericKeysAsAttributes($denormalized)
  124. {
  125. $normalized = array(
  126. 'thing' => array(42 => array('foo', 'bar'), 1337 => array('baz', 'qux')),
  127. );
  128. $this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, $normalized);
  129. }
  130. public function getNumericKeysTests()
  131. {
  132. $configs = array();
  133. $configs[] = array(
  134. 'thing' => array(
  135. 42 => array('foo', 'bar'), 1337 => array('baz', 'qux'),
  136. ),
  137. );
  138. $configs[] = array(
  139. 'thing' => array(
  140. array('foo', 'bar', 'id' => 42), array('baz', 'qux', 'id' => 1337),
  141. ),
  142. );
  143. return array_map(function ($v) { return array($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 = array(
  152. 'thing' => array(
  153. array('foo', 'bar'), array('baz', 'qux'),
  154. ),
  155. );
  156. $this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, array());
  157. }
  158. public function testAssociativeArrayPreserveKeys()
  159. {
  160. $tb = new TreeBuilder();
  161. $tree = $tb
  162. ->root('root', 'array')
  163. ->prototype('array')
  164. ->children()
  165. ->node('foo', 'scalar')->end()
  166. ->end()
  167. ->end()
  168. ->end()
  169. ->buildTree()
  170. ;
  171. $data = array('first' => array('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();
  181. $tree = $tb
  182. ->root('root', 'array')
  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. }