Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / config / Tests / Definition / ScalarNodeTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Config\Tests\Definition;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Definition\ArrayNode;
16 use Symfony\Component\Config\Definition\ScalarNode;
17
18 class ScalarNodeTest extends TestCase
19 {
20     /**
21      * @dataProvider getValidValues
22      */
23     public function testNormalize($value)
24     {
25         $node = new ScalarNode('test');
26         $this->assertSame($value, $node->normalize($value));
27     }
28
29     public function getValidValues()
30     {
31         return array(
32             array(false),
33             array(true),
34             array(null),
35             array(''),
36             array('foo'),
37             array(0),
38             array(1),
39             array(0.0),
40             array(0.1),
41         );
42     }
43
44     public function testSetDeprecated()
45     {
46         $childNode = new ScalarNode('foo');
47         $childNode->setDeprecated('"%node%" is deprecated');
48
49         $this->assertTrue($childNode->isDeprecated());
50         $this->assertSame('"foo" is deprecated', $childNode->getDeprecationMessage($childNode->getName(), $childNode->getPath()));
51
52         $node = new ArrayNode('root');
53         $node->addChild($childNode);
54
55         $deprecationTriggered = 0;
56         $deprecationHandler = function ($level, $message, $file, $line) use (&$prevErrorHandler, &$deprecationTriggered) {
57             if (E_USER_DEPRECATED === $level) {
58                 return ++$deprecationTriggered;
59             }
60
61             return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
62         };
63
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');
68
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');
73     }
74
75     /**
76      * @dataProvider getInvalidValues
77      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
78      */
79     public function testNormalizeThrowsExceptionOnInvalidValues($value)
80     {
81         $node = new ScalarNode('test');
82         $node->normalize($value);
83     }
84
85     public function getInvalidValues()
86     {
87         return array(
88             array(array()),
89             array(array('foo' => 'bar')),
90             array(new \stdClass()),
91         );
92     }
93
94     public function testNormalizeThrowsExceptionWithoutHint()
95     {
96         $node = new ScalarNode('test');
97
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.');
101         } else {
102             $this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', 'Invalid type for path "test". Expected scalar, but got array.');
103         }
104
105         $node->normalize(array());
106     }
107
108     public function testNormalizeThrowsExceptionWithErrorMessage()
109     {
110         $node = new ScalarNode('test');
111         $node->setInfo('"the test value"');
112
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\"");
116         } else {
117             $this->setExpectedException('Symfony\Component\Config\Definition\Exception\InvalidTypeException', "Invalid type for path \"test\". Expected scalar, but got array.\nHint: \"the test value\"");
118         }
119
120         $node->normalize(array());
121     }
122
123     /**
124      * @dataProvider getValidNonEmptyValues
125      *
126      * @param mixed $value
127      */
128     public function testValidNonEmptyValues($value)
129     {
130         $node = new ScalarNode('test');
131         $node->setAllowEmptyValue(false);
132
133         $this->assertSame($value, $node->finalize($value));
134     }
135
136     public function getValidNonEmptyValues()
137     {
138         return array(
139             array(false),
140             array(true),
141             array('foo'),
142             array(0),
143             array(1),
144             array(0.0),
145             array(0.1),
146         );
147     }
148
149     /**
150      * @dataProvider getEmptyValues
151      * @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
152      *
153      * @param mixed $value
154      */
155     public function testNotAllowedEmptyValuesThrowException($value)
156     {
157         $node = new ScalarNode('test');
158         $node->setAllowEmptyValue(false);
159         $node->finalize($value);
160     }
161
162     public function getEmptyValues()
163     {
164         return array(
165             array(null),
166             array(''),
167         );
168     }
169 }