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