4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Tests\Definition;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Definition\ArrayNode;
16 use Symfony\Component\Config\Definition\ScalarNode;
18 class ScalarNodeTest extends TestCase
21 * @dataProvider getValidValues
23 public function testNormalize($value)
25 $node = new ScalarNode('test');
26 $this->assertSame($value, $node->normalize($value));
29 public function getValidValues()
44 public function testSetDeprecated()
46 $childNode = new ScalarNode('foo');
47 $childNode->setDeprecated('"%node%" is deprecated');
49 $this->assertTrue($childNode->isDeprecated());
50 $this->assertSame('"foo" is deprecated', $childNode->getDeprecationMessage($childNode->getName(), $childNode->getPath()));
52 $node = new ArrayNode('root');
53 $node->addChild($childNode);
55 $deprecationTriggered = 0;
56 $deprecationHandler = function ($level, $message, $file, $line) use (&$prevErrorHandler, &$deprecationTriggered) {
57 if (E_USER_DEPRECATED === $level) {
58 return ++$deprecationTriggered;
61 return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
64 $prevErrorHandler = set_error_handler($deprecationHandler);
65 $node->finalize(array());
66 restore_error_handler();
67 $this->assertSame(0, $deprecationTriggered, '->finalize() should not trigger if the deprecated node is not set');
69 $prevErrorHandler = set_error_handler($deprecationHandler);
70 $node->finalize(array('foo' => ''));
71 restore_error_handler();
72 $this->assertSame(1, $deprecationTriggered, '->finalize() should trigger if the deprecated node is set');
76 * @dataProvider getInvalidValues
77 * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
79 public function testNormalizeThrowsExceptionOnInvalidValues($value)
81 $node = new ScalarNode('test');
82 $node->normalize($value);
85 public function getInvalidValues()
89 array(array('foo' => 'bar')),
90 array(new \stdClass()),
94 public function testNormalizeThrowsExceptionWithoutHint()
96 $node = new ScalarNode('test');
98 if (method_exists($this, 'expectException')) {
99 $this->expectException('Symfony\Component\Config\Definition\Exception\InvalidTypeException');
100 $this->expectExceptionMessage('Invalid type for path "test". Expected scalar, but got array.');
102 $this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', 'Invalid type for path "test". Expected scalar, but got array.');
105 $node->normalize(array());
108 public function testNormalizeThrowsExceptionWithErrorMessage()
110 $node = new ScalarNode('test');
111 $node->setInfo('"the test value"');
113 if (method_exists($this, 'expectException')) {
114 $this->expectException('Symfony\Component\Config\Definition\Exception\InvalidTypeException');
115 $this->expectExceptionMessage("Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");
117 $this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', "Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");
120 $node->normalize(array());
124 * @dataProvider getValidNonEmptyValues
126 * @param mixed $value
128 public function testValidNonEmptyValues($value)
130 $node = new ScalarNode('test');
131 $node->setAllowEmptyValue(false);
133 $this->assertSame($value, $node->finalize($value));
136 public function getValidNonEmptyValues()
150 * @dataProvider getEmptyValues
151 * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
153 * @param mixed $value
155 public function testNotAllowedEmptyValuesThrowException($value)
157 $node = new ScalarNode('test');
158 $node->setAllowEmptyValue(false);
159 $node->finalize($value);
162 public function getEmptyValues()