NormalizationTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. ->node('encoders', 'array')
  17. ->useAttributeAsKey('class')
  18. ->prototype('array')
  19. ->beforeNormalization()->ifString()->then(function($v) { return array('algorithm' => $v); })->end()
  20. ->node('algorithm', 'scalar')->end()
  21. ->end()
  22. ->end()
  23. ->end()
  24. ->buildTree()
  25. ;
  26. $normalized = array(
  27. 'encoders' => array(
  28. 'foo' => array('algorithm' => 'plaintext'),
  29. ),
  30. );
  31. $this->assertNormalized($tree, $denormalized, $normalized);
  32. }
  33. public function getEncoderTests()
  34. {
  35. $configs = array();
  36. // XML
  37. $configs[] = array(
  38. 'encoder' => array(
  39. array('class' => 'foo', 'algorithm' => 'plaintext'),
  40. ),
  41. );
  42. // XML when only one element of this type
  43. $configs[] = array(
  44. 'encoder' => array('class' => 'foo', 'algorithm' => 'plaintext'),
  45. );
  46. // YAML/PHP
  47. $configs[] = array(
  48. 'encoders' => array(
  49. array('class' => 'foo', 'algorithm' => 'plaintext'),
  50. ),
  51. );
  52. // YAML/PHP
  53. $configs[] = array(
  54. 'encoders' => array(
  55. 'foo' => 'plaintext',
  56. ),
  57. );
  58. // YAML/PHP
  59. $configs[] = array(
  60. 'encoders' => array(
  61. 'foo' => array('algorithm' => 'plaintext'),
  62. ),
  63. );
  64. return array_map(function($v) {
  65. return array($v);
  66. }, $configs);
  67. }
  68. /**
  69. * @dataProvider getAnonymousKeysTests
  70. */
  71. public function testAnonymousKeysArray($denormalized)
  72. {
  73. $tb = new TreeBuilder();
  74. $tree = $tb
  75. ->root('root', 'array')
  76. ->node('logout', 'array')
  77. ->fixXmlConfig('handler')
  78. ->node('handlers', 'array')
  79. ->prototype('scalar')->end()
  80. ->end()
  81. ->end()
  82. ->end()
  83. ->buildTree()
  84. ;
  85. $normalized = array('logout' => array('handlers' => array('a', 'b', 'c')));
  86. $this->assertNormalized($tree, $denormalized, $normalized);
  87. }
  88. public function getAnonymousKeysTests()
  89. {
  90. $configs = array();
  91. $configs[] = array(
  92. 'logout' => array(
  93. 'handlers' => array('a', 'b', 'c'),
  94. ),
  95. );
  96. $configs[] = array(
  97. 'logout' => array(
  98. 'handler' => array('a', 'b', 'c'),
  99. ),
  100. );
  101. return array_map(function($v) { return array($v); }, $configs);
  102. }
  103. public static function assertNormalized(NodeInterface $tree, $denormalized, $normalized)
  104. {
  105. self::assertSame($normalized, $tree->normalize($denormalized));
  106. }
  107. }