Yaffs site version 1.1
[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\Validation;
18
19 /**
20  * @group dns-sensitive
21  */
22 class EmailValidatorTest extends AbstractConstraintValidatorTest
23 {
24     protected function getApiVersion()
25     {
26         return Validation::API_VERSION_2_5;
27     }
28
29     protected function createValidator()
30     {
31         return new EmailValidator(false);
32     }
33
34     public function testNullIsValid()
35     {
36         $this->validator->validate(null, new Email());
37
38         $this->assertNoViolation();
39     }
40
41     public function testEmptyStringIsValid()
42     {
43         $this->validator->validate('', new Email());
44
45         $this->assertNoViolation();
46     }
47
48     /**
49      * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
50      */
51     public function testExpectsStringCompatibleType()
52     {
53         $this->validator->validate(new \stdClass(), new Email());
54     }
55
56     /**
57      * @dataProvider getValidEmails
58      */
59     public function testValidEmails($email)
60     {
61         $this->validator->validate($email, new Email());
62
63         $this->assertNoViolation();
64     }
65
66     public function getValidEmails()
67     {
68         return array(
69             array('fabien@symfony.com'),
70             array('example@example.co.uk'),
71             array('fabien_potencier@example.fr'),
72         );
73     }
74
75     /**
76      * @dataProvider getInvalidEmails
77      */
78     public function testInvalidEmails($email)
79     {
80         $constraint = new Email(array(
81             'message' => 'myMessage',
82         ));
83
84         $this->validator->validate($email, $constraint);
85
86         $this->buildViolation('myMessage')
87             ->setParameter('{{ value }}', '"'.$email.'"')
88             ->setCode(Email::INVALID_FORMAT_ERROR)
89             ->assertRaised();
90     }
91
92     public function getInvalidEmails()
93     {
94         return array(
95             array('example'),
96             array('example@'),
97             array('example@localhost'),
98             array('foo@example.com bar'),
99         );
100     }
101
102     public function testStrict()
103     {
104         $constraint = new Email(array('strict' => true));
105
106         $this->validator->validate('example@localhost', $constraint);
107
108         $this->assertNoViolation();
109     }
110
111     /**
112      * @dataProvider getDnsChecks
113      * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
114      */
115     public function testDnsChecks($type, $violation)
116     {
117         DnsMock::withMockedHosts(array('example.com' => array(array('type' => $violation ? false : $type))));
118
119         $constraint = new Email(array(
120             'message' => 'myMessage',
121             'MX' === $type ? 'checkMX' : 'checkHost' => true,
122         ));
123
124         $this->validator->validate('foo@example.com', $constraint);
125
126         if (!$violation) {
127             $this->assertNoViolation();
128         } else {
129             $this->buildViolation('myMessage')
130                 ->setParameter('{{ value }}', '"foo@example.com"')
131                 ->setCode($violation)
132                 ->assertRaised();
133         }
134     }
135
136     public function getDnsChecks()
137     {
138         return array(
139             array('MX', false),
140             array('MX', Email::MX_CHECK_FAILED_ERROR),
141             array('A', false),
142             array('A', Email::HOST_CHECK_FAILED_ERROR),
143             array('AAAA', false),
144             array('AAAA', Email::HOST_CHECK_FAILED_ERROR),
145         );
146     }
147
148     /**
149      * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
150      */
151     public function testHostnameIsProperlyParsed()
152     {
153         DnsMock::withMockedHosts(array('baz.com' => array(array('type' => 'MX'))));
154
155         $this->validator->validate(
156             '"foo@bar"@baz.com',
157             new Email(array('checkMX' => true))
158         );
159
160         $this->assertNoViolation();
161     }
162
163     /**
164      * @dataProvider provideCheckTypes
165      */
166     public function testEmptyHostIsNotValid($checkType, $violation)
167     {
168         $this->validator->validate(
169             'foo@bar.fr@',
170             new Email(array(
171                 'message' => 'myMessage',
172                 $checkType => true,
173             ))
174         );
175
176         $this
177             ->buildViolation('myMessage')
178             ->setParameter('{{ value }}', '"foo@bar.fr@"')
179             ->setCode($violation)
180             ->assertRaised();
181     }
182
183     public function provideCheckTypes()
184     {
185         return array(
186             array('checkMX', Email::MX_CHECK_FAILED_ERROR),
187             array('checkHost', Email::HOST_CHECK_FAILED_ERROR),
188         );
189     }
190 }