/
EmailValidatorTest.php
81 lines (67 loc) · 2.06 KB
/
EmailValidatorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Symfony\Tests\Component\Validator;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\EmailValidator;
class EmailValidatorTest extends \PHPUnit_Framework_TestCase
{
protected $validator;
protected function setUp()
{
$this->validator = new EmailValidator();
}
public function testNullIsValid()
{
$this->assertTrue($this->validator->isValid(null, new Email()));
}
public function testEmptyStringIsValid()
{
$this->assertTrue($this->validator->isValid('', new Email()));
}
public function testExpectsStringCompatibleType()
{
$this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
$this->validator->isValid(new \stdClass(), new Email());
}
/**
* @dataProvider getValidEmails
*/
public function testValidEmails($email)
{
$this->assertTrue($this->validator->isValid($email, new Email()));
}
public function getValidEmails()
{
return array(
array('fabien.potencier@symfony-project.com'),
array('example@example.co.uk'),
array('fabien_potencier@example.fr'),
);
}
/**
* @dataProvider getInvalidEmails
*/
public function testInvalidEmails($email)
{
$this->assertFalse($this->validator->isValid($email, new Email()));
}
public function getInvalidEmails()
{
return array(
array('example'),
array('example@'),
array('example@localhost'),
array('example@example.com@example.com'),
);
}
public function testMessageIsSet()
{
$constraint = new Email(array(
'message' => 'myMessage'
));
$this->assertFalse($this->validator->isValid('foobar', $constraint));
$this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
$this->assertEquals($this->validator->getMessageParameters(), array(
'{{ value }}' => 'foobar',
));
}
}