4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Validator\Tests\Constraints;
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;
20 * @group dns-sensitive
22 class EmailValidatorTest extends ConstraintValidatorTestCase
24 protected function createValidator()
26 return new EmailValidator(false);
29 public function testNullIsValid()
31 $this->validator->validate(null, new Email());
33 $this->assertNoViolation();
36 public function testEmptyStringIsValid()
38 $this->validator->validate('', new Email());
40 $this->assertNoViolation();
44 * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
46 public function testExpectsStringCompatibleType()
48 $this->validator->validate(new \stdClass(), new Email());
52 * @dataProvider getValidEmails
54 public function testValidEmails($email)
56 $this->validator->validate($email, new Email());
58 $this->assertNoViolation();
61 public function getValidEmails()
64 array('fabien@symfony.com'),
65 array('example@example.co.uk'),
66 array('fabien_potencier@example.fr'),
71 * @dataProvider getInvalidEmails
73 public function testInvalidEmails($email)
75 $constraint = new Email(array(
76 'message' => 'myMessage',
79 $this->validator->validate($email, $constraint);
81 $this->buildViolation('myMessage')
82 ->setParameter('{{ value }}', '"'.$email.'"')
83 ->setCode(Email::INVALID_FORMAT_ERROR)
87 public function getInvalidEmails()
92 array('example@localhost'),
93 array('foo@example.com bar'),
97 public function testStrict()
99 $constraint = new Email(array('strict' => true));
101 $this->validator->validate('example@localhost', $constraint);
103 $this->assertNoViolation();
107 * @dataProvider getInvalidEmailsForStrictChecks
109 public function testStrictWithInvalidEmails($email)
111 $constraint = new Email(array(
112 'message' => 'myMessage',
116 $this->validator->validate($email, $constraint);
119 ->buildViolation('myMessage')
120 ->setParameter('{{ value }}', '"'.$email.'"')
121 ->setCode(Email::INVALID_FORMAT_ERROR)
126 * @see https://github.com/egulias/EmailValidator/blob/1.2.8/tests/egulias/Tests/EmailValidator/EmailValidatorTest.php
128 public function getInvalidEmailsForStrictChecks()
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
182 * @dataProvider getDnsChecks
183 * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
185 public function testDnsChecks($type, $violation)
187 DnsMock::withMockedHosts(array('example.com' => array(array('type' => $violation ? false : $type))));
189 $constraint = new Email(array(
190 'message' => 'myMessage',
191 'MX' === $type ? 'checkMX' : 'checkHost' => true,
194 $this->validator->validate('foo@example.com', $constraint);
197 $this->assertNoViolation();
199 $this->buildViolation('myMessage')
200 ->setParameter('{{ value }}', '"foo@example.com"')
201 ->setCode($violation)
206 public function getDnsChecks()
210 array('MX', Email::MX_CHECK_FAILED_ERROR),
212 array('A', Email::HOST_CHECK_FAILED_ERROR),
213 array('AAAA', false),
214 array('AAAA', Email::HOST_CHECK_FAILED_ERROR),
219 * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
221 public function testHostnameIsProperlyParsed()
223 DnsMock::withMockedHosts(array('baz.com' => array(array('type' => 'MX'))));
225 $this->validator->validate(
227 new Email(array('checkMX' => true))
230 $this->assertNoViolation();
234 * @dataProvider provideCheckTypes
236 public function testEmptyHostIsNotValid($checkType, $violation)
238 $this->validator->validate(
241 'message' => 'myMessage',
247 ->buildViolation('myMessage')
248 ->setParameter('{{ value }}', '"foo@bar.fr@"')
249 ->setCode($violation)
253 public function provideCheckTypes()
256 array('checkMX', Email::MX_CHECK_FAILED_ERROR),
257 array('checkHost', Email::HOST_CHECK_FAILED_ERROR),