EmailValidatorTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Validator\Tests\Constraints;
  11. use Symfony\Bridge\PhpUnit\DnsMock;
  12. use Symfony\Component\Validator\Constraints\Email;
  13. use Symfony\Component\Validator\Constraints\EmailValidator;
  14. use Symfony\Component\Validator\Validation;
  15. /**
  16. * @group dns-sensitive
  17. */
  18. class EmailValidatorTest extends AbstractConstraintValidatorTest
  19. {
  20. protected function getApiVersion()
  21. {
  22. return Validation::API_VERSION_2_5;
  23. }
  24. protected function createValidator()
  25. {
  26. return new EmailValidator(false);
  27. }
  28. public function testNullIsValid()
  29. {
  30. $this->validator->validate(null, new Email());
  31. $this->assertNoViolation();
  32. }
  33. public function testEmptyStringIsValid()
  34. {
  35. $this->validator->validate('', new Email());
  36. $this->assertNoViolation();
  37. }
  38. /**
  39. * @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
  40. */
  41. public function testExpectsStringCompatibleType()
  42. {
  43. $this->validator->validate(new \stdClass(), new Email());
  44. }
  45. /**
  46. * @dataProvider getValidEmails
  47. */
  48. public function testValidEmails($email)
  49. {
  50. $this->validator->validate($email, new Email());
  51. $this->assertNoViolation();
  52. }
  53. public function getValidEmails()
  54. {
  55. return array(
  56. array('fabien@symfony.com'),
  57. array('example@example.co.uk'),
  58. array('fabien_potencier@example.fr'),
  59. );
  60. }
  61. /**
  62. * @dataProvider getInvalidEmails
  63. */
  64. public function testInvalidEmails($email)
  65. {
  66. $constraint = new Email(array(
  67. 'message' => 'myMessage',
  68. ));
  69. $this->validator->validate($email, $constraint);
  70. $this->buildViolation('myMessage')
  71. ->setParameter('{{ value }}', '"'.$email.'"')
  72. ->setCode(Email::INVALID_FORMAT_ERROR)
  73. ->assertRaised();
  74. }
  75. public function getInvalidEmails()
  76. {
  77. return array(
  78. array('example'),
  79. array('example@'),
  80. array('example@localhost'),
  81. array('foo@example.com bar'),
  82. );
  83. }
  84. public function testStrict()
  85. {
  86. $constraint = new Email(array('strict' => true));
  87. $this->validator->validate('example@localhost', $constraint);
  88. $this->assertNoViolation();
  89. }
  90. /**
  91. * @dataProvider getDnsChecks
  92. * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
  93. */
  94. public function testDnsChecks($type, $violation)
  95. {
  96. DnsMock::withMockedHosts(array('example.com' => array(array('type' => $violation ? false : $type))));
  97. $constraint = new Email(array(
  98. 'message' => 'myMessage',
  99. 'MX' === $type ? 'checkMX' : 'checkHost' => true,
  100. ));
  101. $this->validator->validate('foo@example.com', $constraint);
  102. if (!$violation) {
  103. $this->assertNoViolation();
  104. } else {
  105. $this->buildViolation('myMessage')
  106. ->setParameter('{{ value }}', '"foo@example.com"')
  107. ->setCode($violation)
  108. ->assertRaised();
  109. }
  110. }
  111. public function getDnsChecks()
  112. {
  113. return array(
  114. array('MX', false),
  115. array('MX', Email::MX_CHECK_FAILED_ERROR),
  116. array('A', false),
  117. array('A', Email::HOST_CHECK_FAILED_ERROR),
  118. array('AAAA', false),
  119. array('AAAA', Email::HOST_CHECK_FAILED_ERROR),
  120. );
  121. }
  122. /**
  123. * @requires function Symfony\Bridge\PhpUnit\DnsMock::withMockedHosts
  124. */
  125. public function testHostnameIsProperlyParsed()
  126. {
  127. DnsMock::withMockedHosts(array('baz.com' => array(array('type' => 'MX'))));
  128. $this->validator->validate(
  129. '"foo@bar"@baz.com',
  130. new Email(array('checkMX' => true))
  131. );
  132. $this->assertNoViolation();
  133. }
  134. /**
  135. * @dataProvider provideCheckTypes
  136. */
  137. public function testEmptyHostIsNotValid($checkType, $violation)
  138. {
  139. $this->validator->validate(
  140. 'foo@bar.fr@',
  141. new Email(array(
  142. 'message' => 'myMessage',
  143. $checkType => true,
  144. ))
  145. );
  146. $this
  147. ->buildViolation('myMessage')
  148. ->setParameter('{{ value }}', '"foo@bar.fr@"')
  149. ->setCode($violation)
  150. ->assertRaised();
  151. }
  152. public function provideCheckTypes()
  153. {
  154. return array(
  155. array('checkMX', Email::MX_CHECK_FAILED_ERROR),
  156. array('checkHost', Email::HOST_CHECK_FAILED_ERROR),
  157. );
  158. }
  159. }