NodeBuilderTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\NodeBuilder as BaseNodeBuilder;
  13. use Symfony\Component\Config\Definition\Builder\VariableNodeDefinition as BaseVariableNodeDefinition;
  14. class NodeBuilderTest extends TestCase
  15. {
  16. /**
  17. * @expectedException \RuntimeException
  18. */
  19. public function testThrowsAnExceptionWhenTryingToCreateANonRegisteredNodeType()
  20. {
  21. $builder = new BaseNodeBuilder();
  22. $builder->node('', 'foobar');
  23. }
  24. /**
  25. * @expectedException \RuntimeException
  26. */
  27. public function testThrowsAnExceptionWhenTheNodeClassIsNotFound()
  28. {
  29. $builder = new BaseNodeBuilder();
  30. $builder
  31. ->setNodeClass('noclasstype', '\\foo\\bar\\noclass')
  32. ->node('', 'noclasstype');
  33. }
  34. public function testAddingANewNodeType()
  35. {
  36. $class = __NAMESPACE__.'\\SomeNodeDefinition';
  37. $builder = new BaseNodeBuilder();
  38. $node = $builder
  39. ->setNodeClass('newtype', $class)
  40. ->node('', 'newtype');
  41. $this->assertInstanceOf($class, $node);
  42. }
  43. public function testOverridingAnExistingNodeType()
  44. {
  45. $class = __NAMESPACE__.'\\SomeNodeDefinition';
  46. $builder = new BaseNodeBuilder();
  47. $node = $builder
  48. ->setNodeClass('variable', $class)
  49. ->node('', 'variable');
  50. $this->assertInstanceOf($class, $node);
  51. }
  52. public function testNodeTypesAreNotCaseSensitive()
  53. {
  54. $builder = new BaseNodeBuilder();
  55. $node1 = $builder->node('', 'VaRiAbLe');
  56. $node2 = $builder->node('', 'variable');
  57. $this->assertInstanceOf(\get_class($node1), $node2);
  58. $builder->setNodeClass('CuStOm', __NAMESPACE__.'\\SomeNodeDefinition');
  59. $node1 = $builder->node('', 'CUSTOM');
  60. $node2 = $builder->node('', 'custom');
  61. $this->assertInstanceOf(\get_class($node1), $node2);
  62. }
  63. public function testNumericNodeCreation()
  64. {
  65. $builder = new BaseNodeBuilder();
  66. $node = $builder->integerNode('foo')->min(3)->max(5);
  67. $this->assertInstanceOf('Symfony\Component\Config\Definition\Builder\IntegerNodeDefinition', $node);
  68. $node = $builder->floatNode('bar')->min(3.0)->max(5.0);
  69. $this->assertInstanceOf('Symfony\Component\Config\Definition\Builder\FloatNodeDefinition', $node);
  70. }
  71. }
  72. class SomeNodeDefinition extends BaseVariableNodeDefinition
  73. {
  74. }