Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / TypeValidatorTest.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\Validator\Tests\Constraints;
13
14 use Symfony\Component\Validator\Constraints\Type;
15 use Symfony\Component\Validator\Constraints\TypeValidator;
16 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
17
18 class TypeValidatorTest extends ConstraintValidatorTestCase
19 {
20     protected static $file;
21
22     protected function createValidator()
23     {
24         return new TypeValidator();
25     }
26
27     public function testNullIsValid()
28     {
29         $constraint = new Type(array('type' => 'integer'));
30
31         $this->validator->validate(null, $constraint);
32
33         $this->assertNoViolation();
34     }
35
36     public function testEmptyIsValidIfString()
37     {
38         $constraint = new Type(array('type' => 'string'));
39
40         $this->validator->validate('', $constraint);
41
42         $this->assertNoViolation();
43     }
44
45     public function testEmptyIsInvalidIfNoString()
46     {
47         $constraint = new Type(array(
48             'type' => 'integer',
49             'message' => 'myMessage',
50         ));
51
52         $this->validator->validate('', $constraint);
53
54         $this->buildViolation('myMessage')
55             ->setParameter('{{ value }}', '""')
56             ->setParameter('{{ type }}', 'integer')
57             ->setCode(Type::INVALID_TYPE_ERROR)
58             ->assertRaised();
59     }
60
61     /**
62      * @dataProvider getValidValues
63      */
64     public function testValidValues($value, $type)
65     {
66         $constraint = new Type(array('type' => $type));
67
68         $this->validator->validate($value, $constraint);
69
70         $this->assertNoViolation();
71     }
72
73     public function getValidValues()
74     {
75         $object = new \stdClass();
76         $file = $this->createFile();
77
78         return array(
79             array(true, 'Boolean'),
80             array(false, 'Boolean'),
81             array(true, 'boolean'),
82             array(false, 'boolean'),
83             array(true, 'bool'),
84             array(false, 'bool'),
85             array(0, 'numeric'),
86             array('0', 'numeric'),
87             array(1.5, 'numeric'),
88             array('1.5', 'numeric'),
89             array(0, 'integer'),
90             array(1.5, 'float'),
91             array('12345', 'string'),
92             array(array(), 'array'),
93             array($object, 'object'),
94             array($object, 'stdClass'),
95             array($file, 'resource'),
96             array('12345', 'digit'),
97             array('12a34', 'alnum'),
98             array('abcde', 'alpha'),
99             array("\n\r\t", 'cntrl'),
100             array('arf12', 'graph'),
101             array('abcde', 'lower'),
102             array('ABCDE', 'upper'),
103             array('arf12', 'print'),
104             array('*&$()', 'punct'),
105             array("\n\r\t", 'space'),
106             array('AB10BC99', 'xdigit'),
107         );
108     }
109
110     /**
111      * @dataProvider getInvalidValues
112      */
113     public function testInvalidValues($value, $type, $valueAsString)
114     {
115         $constraint = new Type(array(
116             'type' => $type,
117             'message' => 'myMessage',
118         ));
119
120         $this->validator->validate($value, $constraint);
121
122         $this->buildViolation('myMessage')
123             ->setParameter('{{ value }}', $valueAsString)
124             ->setParameter('{{ type }}', $type)
125             ->setCode(Type::INVALID_TYPE_ERROR)
126             ->assertRaised();
127     }
128
129     public function getInvalidValues()
130     {
131         $object = new \stdClass();
132         $file = $this->createFile();
133
134         return array(
135             array('foobar', 'numeric', '"foobar"'),
136             array('foobar', 'boolean', '"foobar"'),
137             array('0', 'integer', '"0"'),
138             array('1.5', 'float', '"1.5"'),
139             array(12345, 'string', '12345'),
140             array($object, 'boolean', 'object'),
141             array($object, 'numeric', 'object'),
142             array($object, 'integer', 'object'),
143             array($object, 'float', 'object'),
144             array($object, 'string', 'object'),
145             array($object, 'resource', 'object'),
146             array($file, 'boolean', 'resource'),
147             array($file, 'numeric', 'resource'),
148             array($file, 'integer', 'resource'),
149             array($file, 'float', 'resource'),
150             array($file, 'string', 'resource'),
151             array($file, 'object', 'resource'),
152             array('12a34', 'digit', '"12a34"'),
153             array('1a#23', 'alnum', '"1a#23"'),
154             array('abcd1', 'alpha', '"abcd1"'),
155             array("\nabc", 'cntrl', "\"\nabc\""),
156             array("abc\n", 'graph', "\"abc\n\""),
157             array('abCDE', 'lower', '"abCDE"'),
158             array('ABcde', 'upper', '"ABcde"'),
159             array("\nabc", 'print', "\"\nabc\""),
160             array('abc&$!', 'punct', '"abc&$!"'),
161             array("\nabc", 'space', "\"\nabc\""),
162             array('AR1012', 'xdigit', '"AR1012"'),
163         );
164     }
165
166     protected function createFile()
167     {
168         if (!static::$file) {
169             static::$file = fopen(__FILE__, 'r');
170         }
171
172         return static::$file;
173     }
174
175     public static function tearDownAfterClass()
176     {
177         if (static::$file) {
178             fclose(static::$file);
179             static::$file = null;
180         }
181     }
182 }