MergeTest.php 4.2 KB

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