MergeTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace Symfony\Tests\Component\Config\Definition;
  3. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  4. class MergeTest extends \PHPUnit_Framework_TestCase
  5. {
  6. /**
  7. * @expectedException Symfony\Component\Config\Definition\Exception\ForbiddenOverwriteException
  8. */
  9. public function testForbiddenOverwrite()
  10. {
  11. $tb = new TreeBuilder();
  12. $tree = $tb
  13. ->root('root', 'array')
  14. ->children()
  15. ->node('foo', 'scalar')
  16. ->cannotBeOverwritten()
  17. ->end()
  18. ->end()
  19. ->end()
  20. ->buildTree()
  21. ;
  22. $a = array(
  23. 'foo' => 'bar',
  24. );
  25. $b = array(
  26. 'foo' => 'moo',
  27. );
  28. $tree->merge($a, $b);
  29. }
  30. public function testUnsetKey()
  31. {
  32. $tb = new TreeBuilder();
  33. $tree = $tb
  34. ->root('root', 'array')
  35. ->children()
  36. ->node('foo', 'scalar')->end()
  37. ->node('bar', 'scalar')->end()
  38. ->node('unsettable', 'array')
  39. ->canBeUnset()
  40. ->children()
  41. ->node('foo', 'scalar')->end()
  42. ->node('bar', 'scalar')->end()
  43. ->end()
  44. ->end()
  45. ->node('unsetted', 'array')
  46. ->canBeUnset()
  47. ->prototype('scalar')->end()
  48. ->end()
  49. ->end()
  50. ->end()
  51. ->buildTree()
  52. ;
  53. $a = array(
  54. 'foo' => 'bar',
  55. 'unsettable' => array(
  56. 'foo' => 'a',
  57. 'bar' => 'b',
  58. ),
  59. 'unsetted' => false,
  60. );
  61. $b = array(
  62. 'foo' => 'moo',
  63. 'bar' => 'b',
  64. 'unsettable' => false,
  65. 'unsetted' => array('a', 'b'),
  66. );
  67. $this->assertEquals(array(
  68. 'foo' => 'moo',
  69. 'bar' => 'b',
  70. 'unsettable' => false,
  71. 'unsetted' => array('a', 'b'),
  72. ), $tree->merge($a, $b));
  73. }
  74. /**
  75. * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  76. */
  77. public function testDoesNotAllowNewKeysInSubsequentConfigs()
  78. {
  79. $tb = new TreeBuilder();
  80. $tree = $tb
  81. ->root('config', 'array')
  82. ->children()
  83. ->node('test', 'array')
  84. ->disallowNewKeysInSubsequentConfigs()
  85. ->useAttributeAsKey('key')
  86. ->prototype('array')
  87. ->children()
  88. ->node('value', 'scalar')->end()
  89. ->end()
  90. ->end()
  91. ->end()
  92. ->end()
  93. ->end()
  94. ->buildTree();
  95. $a = array(
  96. 'test' => array(
  97. 'a' => array('value' => 'foo')
  98. )
  99. );
  100. $b = array(
  101. 'test' => array(
  102. 'b' => array('value' => 'foo')
  103. )
  104. );
  105. $tree->merge($a, $b);
  106. }
  107. public function testPerformsNoDeepMerging()
  108. {
  109. $tb = new TreeBuilder();
  110. $tree = $tb
  111. ->root('config', 'array')
  112. ->children()
  113. ->node('no_deep_merging', 'array')
  114. ->performNoDeepMerging()
  115. ->children()
  116. ->node('foo', 'scalar')->end()
  117. ->node('bar', 'scalar')->end()
  118. ->end()
  119. ->end()
  120. ->end()
  121. ->end()
  122. ->buildTree()
  123. ;
  124. $a = array(
  125. 'no_deep_merging' => array(
  126. 'foo' => 'a',
  127. 'bar' => 'b',
  128. ),
  129. );
  130. $b = array(
  131. 'no_deep_merging' => array(
  132. 'c' => 'd',
  133. )
  134. );
  135. $this->assertEquals(array(
  136. 'no_deep_merging' => array(
  137. 'c' => 'd',
  138. )
  139. ), $tree->merge($a, $b));
  140. }
  141. public function testPrototypeWithoutAKeyAttribute()
  142. {
  143. $tb = new TreeBuilder();
  144. $tree = $tb
  145. ->root('config', 'array')
  146. ->children()
  147. ->arrayNode('append_elements')
  148. ->prototype('scalar')->end()
  149. ->end()
  150. ->end()
  151. ->end()
  152. ->buildTree()
  153. ;
  154. $a = array(
  155. 'append_elements' => array('a', 'b'),
  156. );
  157. $b = array(
  158. 'append_elements' => array('c', 'd'),
  159. );
  160. $this->assertEquals(array('append_elements' => array('a', 'b', 'c', 'd')), $tree->merge($a, $b));
  161. }
  162. }