MergeTest.php 5.1 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();
  21. $tree = $tb
  22. ->root('root', 'array')
  23. ->children()
  24. ->node('foo', 'scalar')
  25. ->cannotBeOverwritten()
  26. ->end()
  27. ->end()
  28. ->end()
  29. ->buildTree()
  30. ;
  31. $a = array(
  32. 'foo' => 'bar',
  33. );
  34. $b = array(
  35. 'foo' => 'moo',
  36. );
  37. $tree->merge($a, $b);
  38. }
  39. public function testUnsetKey()
  40. {
  41. $tb = new TreeBuilder();
  42. $tree = $tb
  43. ->root('root', 'array')
  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 = array(
  63. 'foo' => 'bar',
  64. 'unsettable' => array(
  65. 'foo' => 'a',
  66. 'bar' => 'b',
  67. ),
  68. 'unsetted' => false,
  69. );
  70. $b = array(
  71. 'foo' => 'moo',
  72. 'bar' => 'b',
  73. 'unsettable' => false,
  74. 'unsetted' => array('a', 'b'),
  75. );
  76. $this->assertEquals(array(
  77. 'foo' => 'moo',
  78. 'bar' => 'b',
  79. 'unsettable' => false,
  80. 'unsetted' => array('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();
  89. $tree = $tb
  90. ->root('config', 'array')
  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 = array(
  105. 'test' => array(
  106. 'a' => array('value' => 'foo'),
  107. ),
  108. );
  109. $b = array(
  110. 'test' => array(
  111. 'b' => array('value' => 'foo'),
  112. ),
  113. );
  114. $tree->merge($a, $b);
  115. }
  116. public function testPerformsNoDeepMerging()
  117. {
  118. $tb = new TreeBuilder();
  119. $tree = $tb
  120. ->root('config', 'array')
  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 = array(
  134. 'no_deep_merging' => array(
  135. 'foo' => 'a',
  136. 'bar' => 'b',
  137. ),
  138. );
  139. $b = array(
  140. 'no_deep_merging' => array(
  141. 'c' => 'd',
  142. ),
  143. );
  144. $this->assertEquals(array(
  145. 'no_deep_merging' => array(
  146. 'c' => 'd',
  147. ),
  148. ), $tree->merge($a, $b));
  149. }
  150. public function testPrototypeWithoutAKeyAttribute()
  151. {
  152. $tb = new TreeBuilder();
  153. $tree = $tb
  154. ->root('config', 'array')
  155. ->children()
  156. ->arrayNode('append_elements')
  157. ->prototype('scalar')->end()
  158. ->end()
  159. ->end()
  160. ->end()
  161. ->buildTree()
  162. ;
  163. $a = array(
  164. 'append_elements' => array('a', 'b'),
  165. );
  166. $b = array(
  167. 'append_elements' => array('c', 'd'),
  168. );
  169. $this->assertEquals(array('append_elements' => array('a', 'b', 'c', 'd')), $tree->merge($a, $b));
  170. }
  171. }