ScalarNodeTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\ScalarNode;
  13. class ScalarNodeTest extends TestCase
  14. {
  15. /**
  16. * @dataProvider getValidValues
  17. */
  18. public function testNormalize($value)
  19. {
  20. $node = new ScalarNode('test');
  21. $this->assertSame($value, $node->normalize($value));
  22. }
  23. public function getValidValues()
  24. {
  25. return array(
  26. array(false),
  27. array(true),
  28. array(null),
  29. array(''),
  30. array('foo'),
  31. array(0),
  32. array(1),
  33. array(0.0),
  34. array(0.1),
  35. );
  36. }
  37. /**
  38. * @dataProvider getInvalidValues
  39. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
  40. */
  41. public function testNormalizeThrowsExceptionOnInvalidValues($value)
  42. {
  43. $node = new ScalarNode('test');
  44. $node->normalize($value);
  45. }
  46. public function getInvalidValues()
  47. {
  48. return array(
  49. array(array()),
  50. array(array('foo' => 'bar')),
  51. array(new \stdClass()),
  52. );
  53. }
  54. public function testNormalizeThrowsExceptionWithoutHint()
  55. {
  56. $node = new ScalarNode('test');
  57. if (method_exists($this, 'expectException')) {
  58. $this->expectException('Symfony\Component\Config\Definition\Exception\InvalidTypeException');
  59. $this->expectExceptionMessage('Invalid type for path "test". Expected scalar, but got array.');
  60. } else {
  61. $this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', 'Invalid type for path "test". Expected scalar, but got array.');
  62. }
  63. $node->normalize(array());
  64. }
  65. public function testNormalizeThrowsExceptionWithErrorMessage()
  66. {
  67. $node = new ScalarNode('test');
  68. $node->setInfo('"the test value"');
  69. if (method_exists($this, 'expectException')) {
  70. $this->expectException('Symfony\Component\Config\Definition\Exception\InvalidTypeException');
  71. $this->expectExceptionMessage("Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");
  72. } else {
  73. $this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', "Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");
  74. }
  75. $node->normalize(array());
  76. }
  77. /**
  78. * @dataProvider getValidNonEmptyValues
  79. *
  80. * @param mixed $value
  81. */
  82. public function testValidNonEmptyValues($value)
  83. {
  84. $node = new ScalarNode('test');
  85. $node->setAllowEmptyValue(false);
  86. $this->assertSame($value, $node->finalize($value));
  87. }
  88. public function getValidNonEmptyValues()
  89. {
  90. return array(
  91. array(false),
  92. array(true),
  93. array('foo'),
  94. array(0),
  95. array(1),
  96. array(0.0),
  97. array(0.1),
  98. );
  99. }
  100. /**
  101. * @dataProvider getEmptyValues
  102. * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
  103. *
  104. * @param mixed $value
  105. */
  106. public function testNotAllowedEmptyValuesThrowException($value)
  107. {
  108. $node = new ScalarNode('test');
  109. $node->setAllowEmptyValue(false);
  110. $node->finalize($value);
  111. }
  112. public function getEmptyValues()
  113. {
  114. return array(
  115. array(null),
  116. array(''),
  117. );
  118. }
  119. }