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