EmailValidatorTest.php 2.4 KB

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