TreeBuilderTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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();
  19. $root = $builder->root('custom', 'array', new CustomNodeBuilder());
  20. $nodeBuilder = $root->children();
  21. $this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\Builder\NodeBuilder', $nodeBuilder);
  22. $nodeBuilder = $nodeBuilder->arrayNode('deeper')->children();
  23. $this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\Builder\NodeBuilder', $nodeBuilder);
  24. }
  25. public function testOverrideABuiltInNodeType()
  26. {
  27. $builder = new TreeBuilder();
  28. $root = $builder->root('override', 'array', new CustomNodeBuilder());
  29. $definition = $root->children()->variableNode('variable');
  30. $this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\Builder\VariableNodeDefinition', $definition);
  31. }
  32. public function testAddANodeType()
  33. {
  34. $builder = new TreeBuilder();
  35. $root = $builder->root('override', 'array', new CustomNodeBuilder());
  36. $definition = $root->children()->barNode('variable');
  37. $this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\Builder\BarNodeDefinition', $definition);
  38. }
  39. public function testCreateABuiltInNodeTypeWithACustomNodeBuilder()
  40. {
  41. $builder = new TreeBuilder();
  42. $root = $builder->root('builtin', 'array', new CustomNodeBuilder());
  43. $definition = $root->children()->booleanNode('boolean');
  44. $this->assertInstanceOf('Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition', $definition);
  45. }
  46. public function testPrototypedArrayNodeUseTheCustomNodeBuilder()
  47. {
  48. $builder = new TreeBuilder();
  49. $root = $builder->root('override', 'array', new CustomNodeBuilder());
  50. $root->prototype('bar')->end();
  51. $this->assertInstanceOf('Symfony\Component\Config\Tests\Fixtures\BarNode', $root->getNode(true)->getPrototype());
  52. }
  53. public function testAnExtendedNodeBuilderGetsPropagatedToTheChildren()
  54. {
  55. $builder = new TreeBuilder();
  56. $builder->root('propagation')
  57. ->children()
  58. ->setNodeClass('extended', 'Symfony\Component\Config\Definition\Builder\BooleanNodeDefinition')
  59. ->node('foo', 'extended')->end()
  60. ->arrayNode('child')
  61. ->children()
  62. ->node('foo', 'extended')
  63. ->end()
  64. ->end()
  65. ->end()
  66. ->end();
  67. $node = $builder->buildTree();
  68. $children = $node->getChildren();
  69. $this->assertInstanceOf('Symfony\Component\Config\Definition\BooleanNode', $children['foo']);
  70. $childChildren = $children['child']->getChildren();
  71. $this->assertInstanceOf('Symfony\Component\Config\Definition\BooleanNode', $childChildren['foo']);
  72. }
  73. public function testDefinitionInfoGetsTransferredToNode()
  74. {
  75. $builder = new TreeBuilder();
  76. $builder->root('test')->info('root info')
  77. ->children()
  78. ->node('child', 'variable')->info('child info')->defaultValue('default')
  79. ->end()
  80. ->end();
  81. $tree = $builder->buildTree();
  82. $children = $tree->getChildren();
  83. $this->assertEquals('root info', $tree->getInfo());
  84. $this->assertEquals('child info', $children['child']->getInfo());
  85. }
  86. public function testDefinitionExampleGetsTransferredToNode()
  87. {
  88. $builder = new TreeBuilder();
  89. $builder->root('test')
  90. ->example(array('key' => 'value'))
  91. ->children()
  92. ->node('child', 'variable')->info('child info')->defaultValue('default')->example('example')
  93. ->end()
  94. ->end();
  95. $tree = $builder->buildTree();
  96. $children = $tree->getChildren();
  97. $this->assertInternalType('array', $tree->getExample());
  98. $this->assertEquals('example', $children['child']->getExample());
  99. }
  100. }