NormalizationTest.php 3.7 KB

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