MergeTest.php 5.0 KB

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