UrlValidatorTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\Url;
  12. use Symfony\Component\Validator\Constraints\UrlValidator;
  13. class UrlValidatorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $validator;
  16. protected function setUp()
  17. {
  18. $this->validator = new UrlValidator();
  19. }
  20. public function testNullIsValid()
  21. {
  22. $this->assertTrue($this->validator->isValid(null, new Url()));
  23. }
  24. public function testEmptyStringIsValid()
  25. {
  26. $this->assertTrue($this->validator->isValid('', new Url()));
  27. }
  28. public function testExpectsStringCompatibleType()
  29. {
  30. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  31. $this->validator->isValid(new \stdClass(), new Url());
  32. }
  33. /**
  34. * @dataProvider getValidUrls
  35. */
  36. public function testValidUrls($url)
  37. {
  38. $this->assertTrue($this->validator->isValid($url, new Url()));
  39. }
  40. public function getValidUrls()
  41. {
  42. return array(
  43. array('http://www.google.com'),
  44. array('https://google.com/'),
  45. array('https://google.com:80/'),
  46. array('http://www.symfony.com/'),
  47. array('http://127.0.0.1/'),
  48. array('http://127.0.0.1:80/'),
  49. array('http://[::1]/'),
  50. array('http://[::1]:80/'),
  51. );
  52. }
  53. /**
  54. * @dataProvider getInvalidUrls
  55. */
  56. public function testInvalidUrls($url)
  57. {
  58. $this->assertFalse($this->validator->isValid($url, new Url()));
  59. }
  60. public function getInvalidUrls()
  61. {
  62. return array(
  63. array('google.com'),
  64. array('http:/google.com'),
  65. array('http://google.com::aa'),
  66. array('http://google.foobar'),
  67. array('ftp://google.fr'),
  68. );
  69. }
  70. public function testMessageIsSet()
  71. {
  72. $constraint = new Url(array(
  73. 'message' => 'myMessage'
  74. ));
  75. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  76. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  77. $this->assertEquals($this->validator->getMessageParameters(), array(
  78. '{{ value }}' => 'foobar',
  79. ));
  80. }
  81. }