ScalarNodeTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\Definition\ArrayNode;
  13. use Symfony\Component\Config\Definition\ScalarNode;
  14. class ScalarNodeTest extends TestCase
  15. {
  16. /**
  17. * @dataProvider getValidValues
  18. */
  19. public function testNormalize($value)
  20. {
  21. $node = new ScalarNode('test');
  22. $this->assertSame($value, $node->normalize($value));
  23. }
  24. public function getValidValues()
  25. {
  26. return [
  27. [false],
  28. [true],
  29. [null],
  30. [''],
  31. ['foo'],
  32. [0],
  33. [1],
  34. [0.0],
  35. [0.1],
  36. ];
  37. }
  38. public function testSetDeprecated()
  39. {
  40. $childNode = new ScalarNode('foo');
  41. $childNode->setDeprecated('"%node%" is deprecated');
  42. $this->assertTrue($childNode->isDeprecated());
  43. $this->assertSame('"foo" is deprecated', $childNode->getDeprecationMessage($childNode->getName(), $childNode->getPath()));
  44. $node = new ArrayNode('root');
  45. $node->addChild($childNode);
  46. $deprecationTriggered = 0;
  47. $deprecationHandler = function ($level, $message, $file, $line) use (&$prevErrorHandler, &$deprecationTriggered) {
  48. if (E_USER_DEPRECATED === $level) {
  49. return ++$deprecationTriggered;
  50. }
  51. return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
  52. };
  53. $prevErrorHandler = set_error_handler($deprecationHandler);
  54. $node->finalize([]);
  55. restore_error_handler();
  56. $this->assertSame(0, $deprecationTriggered, '->finalize() should not trigger if the deprecated node is not set');
  57. $prevErrorHandler = set_error_handler($deprecationHandler);
  58. $node->finalize(['foo' => '']);
  59. restore_error_handler();
  60. $this->assertSame(1, $deprecationTriggered, '->finalize() should trigger if the deprecated node is set');
  61. }
  62. /**
  63. * @dataProvider getInvalidValues
  64. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
  65. */
  66. public function testNormalizeThrowsExceptionOnInvalidValues($value)
  67. {
  68. $node = new ScalarNode('test');
  69. $node->normalize($value);
  70. }
  71. public function getInvalidValues()
  72. {
  73. return [
  74. [[]],
  75. [['foo' => 'bar']],
  76. [new \stdClass()],
  77. ];
  78. }
  79. public function testNormalizeThrowsExceptionWithoutHint()
  80. {
  81. $node = new ScalarNode('test');
  82. if (method_exists($this, 'expectException')) {
  83. $this->expectException('Symfony\Component\Config\Definition\Exception\InvalidTypeException');
  84. $this->expectExceptionMessage('Invalid type for path "test". Expected scalar, but got array.');
  85. } else {
  86. $this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', 'Invalid type for path "test". Expected scalar, but got array.');
  87. }
  88. $node->normalize([]);
  89. }
  90. public function testNormalizeThrowsExceptionWithErrorMessage()
  91. {
  92. $node = new ScalarNode('test');
  93. $node->setInfo('"the test value"');
  94. if (method_exists($this, 'expectException')) {
  95. $this->expectException('Symfony\Component\Config\Definition\Exception\InvalidTypeException');
  96. $this->expectExceptionMessage("Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");
  97. } else {
  98. $this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', "Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");
  99. }
  100. $node->normalize([]);
  101. }
  102. /**
  103. * @dataProvider getValidNonEmptyValues
  104. *
  105. * @param mixed $value
  106. */
  107. public function testValidNonEmptyValues($value)
  108. {
  109. $node = new ScalarNode('test');
  110. $node->setAllowEmptyValue(false);
  111. $this->assertSame($value, $node->finalize($value));
  112. }
  113. public function getValidNonEmptyValues()
  114. {
  115. return [
  116. [false],
  117. [true],
  118. ['foo'],
  119. [0],
  120. [1],
  121. [0.0],
  122. [0.1],
  123. ];
  124. }
  125. /**
  126. * @dataProvider getEmptyValues
  127. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  128. *
  129. * @param mixed $value
  130. */
  131. public function testNotAllowedEmptyValuesThrowException($value)
  132. {
  133. $node = new ScalarNode('test');
  134. $node->setAllowEmptyValue(false);
  135. $node->finalize($value);
  136. }
  137. public function getEmptyValues()
  138. {
  139. return [
  140. [null],
  141. [''],
  142. ];
  143. }
  144. }