94857c17841734663c92758cec013adc21281f96
[yaffs-website] / vendor / symfony / validator / Tests / Constraints / EmailValidatorTest.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\Bridge\PhpUnit\DnsMock;
15 use Symfony\Component\Validator\Constraints\Email;
16 use Symfony\Component\Validator\Constraints\EmailValidator;
17 use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
18
19 /**
20  * @group dns-sensitive
21  */
22 class EmailValidatorTest extends ConstraintValidatorTestCase
23 {
24     protected function createValidator()
25     {
26         return new EmailValidator(false);
27     }
28
29     public function testNullIsValid()
30     {
31         $this->validator->validate(null, new Email());
32
33         $this->assertNoViolation();
34     }
35
36     public function testEmptyStringIsValid()
37     {
38         $this->validator->validate('', new Email());
39
40         $this->assertNoViolation();
41     }
42
43     /**
44      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
45      */
46     public function testExpectsStringCompatibleType()
47     {
48         $this->validator->validate(new \stdClass(), new Email());
49     }
50
51     /**
52      * @dataProvider getValidEmails
53      */
54     public function testValidEmails($email)
55     {
56         $this->validator->validate($email, new Email());
57
58         $this->assertNoViolation();
59     }
60
61     public function getValidEmails()
62     {
63         return array(
64             array('fabien@symfony.com'),
65             array('example@example.co.uk'),
66             array('fabien_potencier@example.fr'),
67         );
68     }
69
70     /**
71      * @dataProvider getInvalidEmails
72      */
73     public function testInvalidEmails($email)
74     {
75         $constraint = new Email(array(
76             'message' => 'myMessage',
77         ));
78
79         $this->validator->validate($email, $constraint);
80
81         $this->buildViolation('myMessage')
82             ->setParameter('{{ value }}', '"'.$email.'"')
83             ->setCode(Email::INVALID_FORMAT_ERROR)
84             ->assertRaised();
85     }
86
87     public function getInvalidEmails()
88     {
89         return array(
90             array('example'),
91             array('example@'),
92             array('example@localhost'),
93             array('foo@example.com bar'),
94         );
95     }
96
97     public function testStrict()
98     {
99         $constraint = new Email(array('strict' => true));
100
101         $this->validator->validate('example@localhost', $constraint);
102
103         $this->assertNoViolation();
104     }
105
106     /**
107      * @dataProvider getInvalidEmailsForStrictChecks
108      */
109     public function testStrictWithInvalidEmails($email)
110     {
111         $constraint = new Email(array(
112             'message' => 'myMessage',
113             'strict' => true,
114         ));
115
116         $this->validator->validate($email, $constraint);
117
118         $this
119             ->buildViolation('myMessage')
120             ->setParameter('{{ value }}', '"'.$email.'"')
121             ->setCode(Email::INVALID_FORMAT_ERROR)
122             ->assertRaised();
123     }
124
125     /**
126      * @see https://github.com/egulias/EmailValidator/blob/1.2.8/tests/egulias/Tests/EmailValidator/EmailValidatorTest.php
127      */
128     public function getInvalidEmailsForStrictChecks()
129     {
130         return array(
131             array('test@example.com test'),
132             array('user  name@example.com'),
133             array('user   name@example.com'),
134             array('example.@example.co.uk'),
135             array('example@example@example.co.uk'),
136             array('(test_exampel@example.fr)'),
137             array('example(example)example@example.co.uk'),
138             array('.example@localhost'),
139             array('ex\ample@localhost'),
140             array('example@local\host'),
141             array('example@localhost.'),
142             array('user name@example.com'),
143             array('username@ example . com'),
144             array('example@(fake).com'),
145             array('example@(fake.com'),
146             array('username@example,com'),
147             array('usern,ame@example.com'),
148             array('user[na]me@example.com'),
149             array('"""@iana.org'),
150             array('"\"@iana.org'),
151             array('"test"test@iana.org'),
152             array('"test""test"@iana.org'),
153             array('"test"."test"@iana.org'),
154             array('"test".test@iana.org'),
155             array('"test"'.chr(0).'@iana.org'),
156             array('"test\"@iana.org'),
157             array(chr(226).'@iana.org'),
158             array('test@'.chr(226).'.org'),
159             array('\r\ntest@iana.org'),
160             array('\r\n test@iana.org'),
161             array('\r\n \r\ntest@iana.org'),
162             array('\r\n \r\ntest@iana.org'),
163             array('\r\n \r\n test@iana.org'),
164             array('test@iana.org \r\n'),
165             array('test@iana.org \r\n '),
166             array('test@iana.org \r\n \r\n'),
167             array('test@iana.org \r\n\r\n'),
168             array('test@iana.org  \r\n\r\n '),
169             array('test@iana/icann.org'),
170             array('test@foo;bar.com'),
171             array('test;123@foobar.com'),
172             array('test@example..com'),
173             array('email.email@email."'),
174             array('test@email>'),
175             array('test@email<'),
176             array('test@email{'),
177             array(str_repeat('x', 254).'@example.com'), //email with warnings
178         );
179     }
180
181     /**
182      * @dataProvider getDnsChecks
183      * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
184      */
185     public function testDnsChecks($type, $violation)
186     {
187         DnsMock::withMockedHosts(array('example.com' => array(array('type' => $violation ? false : $type))));
188
189         $constraint = new Email(array(
190             'message' => 'myMessage',
191             'MX' === $type ? 'checkMX' : 'checkHost' => true,
192         ));
193
194         $this->validator->validate('foo@example.com', $constraint);
195
196         if (!$violation) {
197             $this->assertNoViolation();
198         } else {
199             $this->buildViolation('myMessage')
200                 ->setParameter('{{ value }}', '"foo@example.com"')
201                 ->setCode($violation)
202                 ->assertRaised();
203         }
204     }
205
206     public function getDnsChecks()
207     {
208         return array(
209             array('MX', false),
210             array('MX', Email::MX_CHECK_FAILED_ERROR),
211             array('A', false),
212             array('A', Email::HOST_CHECK_FAILED_ERROR),
213             array('AAAA', false),
214             array('AAAA', Email::HOST_CHECK_FAILED_ERROR),
215         );
216     }
217
218     /**
219      * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
220      */
221     public function testHostnameIsProperlyParsed()
222     {
223         DnsMock::withMockedHosts(array('baz.com' => array(array('type' => 'MX'))));
224
225         $this->validator->validate(
226             '"foo@bar"@baz.com',
227             new Email(array('checkMX' => true))
228         );
229
230         $this->assertNoViolation();
231     }
232
233     /**
234      * @dataProvider provideCheckTypes
235      */
236     public function testEmptyHostIsNotValid($checkType, $violation)
237     {
238         $this->validator->validate(
239             'foo@bar.fr@',
240             new Email(array(
241                 'message' => 'myMessage',
242                 $checkType => true,
243             ))
244         );
245
246         $this
247             ->buildViolation('myMessage')
248             ->setParameter('{{ value }}', '"foo@bar.fr@"')
249             ->setCode($violation)
250             ->assertRaised();
251     }
252
253     public function provideCheckTypes()
254     {
255         return array(
256             array('checkMX', Email::MX_CHECK_FAILED_ERROR),
257             array('checkHost', Email::HOST_CHECK_FAILED_ERROR),
258         );
259     }
260 }