EmailValidatorTest.php 2.5 KB

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