EmailValidatorTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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;
  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. public function getValidEmails()
  41. {
  42. return array(
  43. array('fabien@symfony.com'),
  44. array('example@example.co.uk'),
  45. array('fabien_potencier@example.fr'),
  46. );
  47. }
  48. /**
  49. * @dataProvider getInvalidEmails
  50. */
  51. public function testInvalidEmails($email)
  52. {
  53. $this->assertFalse($this->validator->isValid($email, new Email()));
  54. }
  55. public function getInvalidEmails()
  56. {
  57. return array(
  58. array('example'),
  59. array('example@'),
  60. array('example@localhost'),
  61. array('example@example.com@example.com'),
  62. );
  63. }
  64. public function testMessageIsSet()
  65. {
  66. $constraint = new Email(array(
  67. 'message' => 'myMessage'
  68. ));
  69. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  70. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  71. $this->assertEquals($this->validator->getMessageParameters(), array(
  72. '{{ value }}' => 'foobar',
  73. ));
  74. }
  75. }