NormalizationTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. * This file is part of the Symfony framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\Tests\Component\Config\Definition;
  11. use Symfony\Component\Config\Definition\NodeInterface;
  12. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  13. class NormalizerTest extends \PHPUnit_Framework_TestCase
  14. {
  15. /**
  16. * @dataProvider getEncoderTests
  17. */
  18. public function testNormalizeEncoders($denormalized)
  19. {
  20. $tb = new TreeBuilder();
  21. $tree = $tb
  22. ->root('root_name', 'array')
  23. ->fixXmlConfig('encoder')
  24. ->children()
  25. ->node('encoders', 'array')
  26. ->useAttributeAsKey('class')
  27. ->prototype('array')
  28. ->beforeNormalization()->ifString()->then(function($v) { return array('algorithm' => $v); })->end()
  29. ->children()
  30. ->node('algorithm', 'scalar')->end()
  31. ->end()
  32. ->end()
  33. ->end()
  34. ->end()
  35. ->end()
  36. ->buildTree()
  37. ;
  38. $normalized = array(
  39. 'encoders' => array(
  40. 'foo' => array('algorithm' => 'plaintext'),
  41. ),
  42. );
  43. $this->assertNormalized($tree, $denormalized, $normalized);
  44. }
  45. public function getEncoderTests()
  46. {
  47. $configs = array();
  48. // XML
  49. $configs[] = array(
  50. 'encoder' => array(
  51. array('class' => 'foo', 'algorithm' => 'plaintext'),
  52. ),
  53. );
  54. // XML when only one element of this type
  55. $configs[] = array(
  56. 'encoder' => array('class' => 'foo', 'algorithm' => 'plaintext'),
  57. );
  58. // YAML/PHP
  59. $configs[] = array(
  60. 'encoders' => array(
  61. array('class' => 'foo', 'algorithm' => 'plaintext'),
  62. ),
  63. );
  64. // YAML/PHP
  65. $configs[] = array(
  66. 'encoders' => array(
  67. 'foo' => 'plaintext',
  68. ),
  69. );
  70. // YAML/PHP
  71. $configs[] = array(
  72. 'encoders' => array(
  73. 'foo' => array('algorithm' => 'plaintext'),
  74. ),
  75. );
  76. return array_map(function($v) {
  77. return array($v);
  78. }, $configs);
  79. }
  80. /**
  81. * @dataProvider getAnonymousKeysTests
  82. */
  83. public function testAnonymousKeysArray($denormalized)
  84. {
  85. $tb = new TreeBuilder();
  86. $tree = $tb
  87. ->root('root', 'array')
  88. ->children()
  89. ->node('logout', 'array')
  90. ->fixXmlConfig('handler')
  91. ->children()
  92. ->node('handlers', 'array')
  93. ->prototype('scalar')->end()
  94. ->end()
  95. ->end()
  96. ->end()
  97. ->end()
  98. ->end()
  99. ->buildTree()
  100. ;
  101. $normalized = array('logout' => array('handlers' => array('a', 'b', 'c')));
  102. $this->assertNormalized($tree, $denormalized, $normalized);
  103. }
  104. public function getAnonymousKeysTests()
  105. {
  106. $configs = array();
  107. $configs[] = array(
  108. 'logout' => array(
  109. 'handlers' => array('a', 'b', 'c'),
  110. ),
  111. );
  112. $configs[] = array(
  113. 'logout' => array(
  114. 'handler' => array('a', 'b', 'c'),
  115. ),
  116. );
  117. return array_map(function($v) { return array($v); }, $configs);
  118. }
  119. public static function assertNormalized(NodeInterface $tree, $denormalized, $normalized)
  120. {
  121. self::assertSame($normalized, $tree->normalize($denormalized));
  122. }
  123. }