TreeBuilderTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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\Builder;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\Definition\Builder\TreeBuilder;
  13. use Symfony\Component\Config\Tests\Fixtures\Builder\NodeBuilder as CustomNodeBuilder;
  14. class TreeBuilderTest extends TestCase
  15. {
  16. public function testUsingACustomNodeBuilder()
  17. {
  18. $builder = new TreeBuilder('custom', 'array', new CustomNodeBuilder());
  19. $nodeBuilder = $builder->getRootNode()->children();
  20. $this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\Builder\NodeBuilder', $nodeBuilder);
  21. $nodeBuilder = $nodeBuilder->arrayNode('deeper')->children();
  22. $this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\Builder\NodeBuilder', $nodeBuilder);
  23. }
  24. public function testOverrideABuiltInNodeType()
  25. {
  26. $builder = new TreeBuilder('override', 'array', new CustomNodeBuilder());
  27. $definition = $builder->getRootNode()->children()->variableNode('variable');
  28. $this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\Builder\VariableNodeDefinition', $definition);
  29. }
  30. public function testAddANodeType()
  31. {
  32. $builder = new TreeBuilder('override', 'array', new CustomNodeBuilder());
  33. $definition = $builder->getRootNode()->children()->barNode('variable');
  34. $this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\Builder\BarNodeDefinition', $definition);
  35. }
  36. public function testCreateABuiltInNodeTypeWithACustomNodeBuilder()
  37. {
  38. $builder = new TreeBuilder('builtin', 'array', new CustomNodeBuilder());
  39. $definition = $builder->getRootNode()->children()->booleanNode('boolean');
  40. $this->assertInstanceOf('Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition', $definition);
  41. }
  42. public function testPrototypedArrayNodeUseTheCustomNodeBuilder()
  43. {
  44. $builder = new TreeBuilder('override', 'array', new CustomNodeBuilder());
  45. $root = $builder->getRootNode();
  46. $root->prototype('bar')->end();
  47. $this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\BarNode', $root->getNode(true)->getPrototype());
  48. }
  49. public function testAnExtendedNodeBuilderGetsPropagatedToTheChildren()
  50. {
  51. $builder = new TreeBuilder('propagation');
  52. $builder->getRootNode()
  53. ->children()
  54. ->setNodeClass('extended', 'Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition')
  55. ->node('foo', 'extended')->end()
  56. ->arrayNode('child')
  57. ->children()
  58. ->node('foo', 'extended')
  59. ->end()
  60. ->end()
  61. ->end()
  62. ->end();
  63. $node = $builder->buildTree();
  64. $children = $node->getChildren();
  65. $this->assertInstanceOf('Symfony\Component\Config\Definition\BooleanNode', $children['foo']);
  66. $childChildren = $children['child']->getChildren();
  67. $this->assertInstanceOf('Symfony\Component\Config\Definition\BooleanNode', $childChildren['foo']);
  68. }
  69. public function testDefinitionInfoGetsTransferredToNode()
  70. {
  71. $builder = new TreeBuilder('test');
  72. $builder->getRootNode()->info('root info')
  73. ->children()
  74. ->node('child', 'variable')->info('child info')->defaultValue('default')
  75. ->end()
  76. ->end();
  77. $tree = $builder->buildTree();
  78. $children = $tree->getChildren();
  79. $this->assertEquals('root info', $tree->getInfo());
  80. $this->assertEquals('child info', $children['child']->getInfo());
  81. }
  82. public function testDefinitionExampleGetsTransferredToNode()
  83. {
  84. $builder = new TreeBuilder('test');
  85. $builder->getRootNode()
  86. ->example(['key' => 'value'])
  87. ->children()
  88. ->node('child', 'variable')->info('child info')->defaultValue('default')->example('example')
  89. ->end()
  90. ->end();
  91. $tree = $builder->buildTree();
  92. $children = $tree->getChildren();
  93. $this->assertInternalType('array', $tree->getExample());
  94. $this->assertEquals('example', $children['child']->getExample());
  95. }
  96. public function testDefaultPathSeparatorIsDot()
  97. {
  98. $builder = new TreeBuilder('propagation');
  99. $builder->getRootNode()
  100. ->children()
  101. ->node('foo', 'variable')->end()
  102. ->arrayNode('child')
  103. ->children()
  104. ->node('foo', 'variable')
  105. ->end()
  106. ->end()
  107. ->end()
  108. ->end();
  109. $node = $builder->buildTree();
  110. $children = $node->getChildren();
  111. $this->assertArrayHasKey('foo', $children);
  112. $this->assertInstanceOf('Symfony\Component\Config\Definition\BaseNode', $children['foo']);
  113. $this->assertSame('propagation.foo', $children['foo']->getPath());
  114. $this->assertArrayHasKey('child', $children);
  115. $childChildren = $children['child']->getChildren();
  116. $this->assertArrayHasKey('foo', $childChildren);
  117. $this->assertInstanceOf('Symfony\Component\Config\Definition\BaseNode', $childChildren['foo']);
  118. $this->assertSame('propagation.child.foo', $childChildren['foo']->getPath());
  119. }
  120. public function testPathSeparatorIsPropagatedToChildren()
  121. {
  122. $builder = new TreeBuilder('propagation');
  123. $builder->getRootNode()
  124. ->children()
  125. ->node('foo', 'variable')->end()
  126. ->arrayNode('child')
  127. ->children()
  128. ->node('foo', 'variable')
  129. ->end()
  130. ->end()
  131. ->end()
  132. ->end();
  133. $builder->setPathSeparator('/');
  134. $node = $builder->buildTree();
  135. $children = $node->getChildren();
  136. $this->assertArrayHasKey('foo', $children);
  137. $this->assertInstanceOf('Symfony\Component\Config\Definition\BaseNode', $children['foo']);
  138. $this->assertSame('propagation/foo', $children['foo']->getPath());
  139. $this->assertArrayHasKey('child', $children);
  140. $childChildren = $children['child']->getChildren();
  141. $this->assertArrayHasKey('foo', $childChildren);
  142. $this->assertInstanceOf('Symfony\Component\Config\Definition\BaseNode', $childChildren['foo']);
  143. $this->assertSame('propagation/child/foo', $childChildren['foo']->getPath());
  144. }
  145. /**
  146. * @group legacy
  147. * @expectedDeprecation A tree builder without a root node is deprecated since Symfony 4.2 and will not be supported anymore in 5.0.
  148. */
  149. public function testInitializingTreeBuildersWithoutRootNode()
  150. {
  151. new TreeBuilder();
  152. }
  153. }