UrlValidatorTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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://a.pl'),
  44. array('http://www.google.com'),
  45. array('http://www.google.museum'),
  46. array('http://google.বাংলা/'),
  47. array('http://google.ąęź/'),
  48. array('https://google.com/'),
  49. array('https://google.com:80/'),
  50. array('http://א-ת.com/'),
  51. array('http://żółw.żółw/'),
  52. array('http://àlàl.com:80/'),
  53. array('http://www.example.coop/'),
  54. array('http://www.test-example.com/'),
  55. array('http://www.symfony.com/'),
  56. array('http://symfony.fake/blog/'),
  57. array('http://symfony.com/search?type=&q=url+validator'),
  58. array('http://www.symfony.com/doc/current/book/validation.html#supported-constraints'),
  59. array('http://very.long.domain.name.com/'),
  60. array('http://127.0.0.1/'),
  61. array('http://127.0.0.1:80/'),
  62. array('http://[::1]/'),
  63. array('http://[::1]:80/'),
  64. array('http://[1:2:3::4:5:6:7]/'),
  65. );
  66. }
  67. /**
  68. * @dataProvider getInvalidUrls
  69. */
  70. public function testInvalidUrls($url)
  71. {
  72. $this->assertFalse($this->validator->isValid($url, new Url()));
  73. }
  74. public function getInvalidUrls()
  75. {
  76. return array(
  77. array('google.com'),
  78. array('://google.com'),
  79. array('http ://google.com'),
  80. array('http:/google.com'),
  81. array('http://goog_le.com'),
  82. array('http://google.com::aa'),
  83. array('http://google.com:aa'),
  84. array('http://google.foobar'),
  85. array('ftp://google.fr'),
  86. array('faked://google.fr'),
  87. array('http://127.0.0.1:aa/'),
  88. array('http://127.0.0.1:123456/'),
  89. array('ftp://[::1]/'),
  90. array('http://[::1'),
  91. );
  92. }
  93. /**
  94. * @dataProvider getValidCustomUrls
  95. */
  96. public function testCustomProtocolIsValid($url)
  97. {
  98. $constraint = new Url(array(
  99. 'protocols' => array('ftp', 'file', 'git')
  100. ));
  101. $this->assertTrue($this->validator->isValid($url, $constraint));
  102. }
  103. public function getValidCustomUrls()
  104. {
  105. return array(
  106. array('ftp://google.com'),
  107. array('file://127.0.0.1'),
  108. array('git://[::1]/'),
  109. );
  110. }
  111. public function testMessageIsSet()
  112. {
  113. $constraint = new Url(array(
  114. 'message' => 'myMessage'
  115. ));
  116. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  117. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  118. $this->assertEquals($this->validator->getMessageParameters(), array(
  119. '{{ value }}' => 'foobar',
  120. ));
  121. }
  122. }