MergeTest.php 5.1 KB

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