UrlValidatorTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. protected function tearDown()
  21. {
  22. $this->validator = null;
  23. }
  24. public function testNullIsValid()
  25. {
  26. $this->assertTrue($this->validator->isValid(null, new Url()));
  27. }
  28. public function testEmptyStringIsValid()
  29. {
  30. $this->assertTrue($this->validator->isValid('', new Url()));
  31. }
  32. public function testExpectsStringCompatibleType()
  33. {
  34. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  35. $this->validator->isValid(new \stdClass(), new Url());
  36. }
  37. /**
  38. * @dataProvider getValidUrls
  39. */
  40. public function testValidUrls($url)
  41. {
  42. $this->assertTrue($this->validator->isValid($url, new Url()));
  43. }
  44. public function getValidUrls()
  45. {
  46. return array(
  47. array('http://a.pl'),
  48. array('http://www.google.com'),
  49. array('http://www.google.museum'),
  50. array('https://google.com/'),
  51. array('https://google.com:80/'),
  52. array('http://www.example.coop/'),
  53. array('http://www.test-example.com/'),
  54. array('http://www.symfony.com/'),
  55. array('http://symfony.fake/blog/'),
  56. array('http://symfony.com/search?type=&q=url+validator'),
  57. array('http://www.symfony.com/doc/current/book/validation.html#supported-constraints'),
  58. array('http://very.long.domain.name.com/'),
  59. array('http://127.0.0.1/'),
  60. array('http://127.0.0.1:80/'),
  61. array('http://[::1]/'),
  62. array('http://[::1]:80/'),
  63. array('http://[1:2:3::4:5:6:7]/'),
  64. );
  65. }
  66. /**
  67. * @dataProvider getInvalidUrls
  68. */
  69. public function testInvalidUrls($url)
  70. {
  71. $this->assertFalse($this->validator->isValid($url, new Url()));
  72. }
  73. public function getInvalidUrls()
  74. {
  75. return array(
  76. array('google.com'),
  77. array('://google.com'),
  78. array('http ://google.com'),
  79. array('http:/google.com'),
  80. array('http://goog_le.com'),
  81. array('http://google.com::aa'),
  82. array('http://google.com:aa'),
  83. array('ftp://google.fr'),
  84. array('faked://google.fr'),
  85. array('http://127.0.0.1:aa/'),
  86. array('ftp://[::1]/'),
  87. array('http://[::1'),
  88. );
  89. }
  90. /**
  91. * @dataProvider getValidCustomUrls
  92. */
  93. public function testCustomProtocolIsValid($url)
  94. {
  95. $constraint = new Url(array(
  96. 'protocols' => array('ftp', 'file', 'git')
  97. ));
  98. $this->assertTrue($this->validator->isValid($url, $constraint));
  99. }
  100. public function getValidCustomUrls()
  101. {
  102. return array(
  103. array('ftp://google.com'),
  104. array('file://127.0.0.1'),
  105. array('git://[::1]/'),
  106. );
  107. }
  108. public function testMessageIsSet()
  109. {
  110. $constraint = new Url(array(
  111. 'message' => 'myMessage'
  112. ));
  113. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  114. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  115. $this->assertEquals($this->validator->getMessageParameters(), array(
  116. '{{ value }}' => 'foobar',
  117. ));
  118. }
  119. }