UrlValidatorTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. array('http://sãopaulo.com/'),
  65. array('http://sãopaulo.com.br/'),
  66. array('http://пример.испытание/'),
  67. array('http://مثال.إختبار/'),
  68. array('http://例子.测试/'),
  69. array('http://例子.測試/'),
  70. array('http://例え.テスト/'),
  71. array('http://مثال.آزمایشی/'),
  72. array('http://실례.테스트/'),
  73. array('http://العربية.idn.icann.org/'),
  74. array('http://☎.com/'),
  75. );
  76. }
  77. /**
  78. * @dataProvider getInvalidUrls
  79. */
  80. public function testInvalidUrls($url)
  81. {
  82. $this->assertFalse($this->validator->isValid($url, new Url()));
  83. }
  84. public function getInvalidUrls()
  85. {
  86. return array(
  87. array('google.com'),
  88. array('://google.com'),
  89. array('http ://google.com'),
  90. array('http:/google.com'),
  91. array('http://goog_le.com'),
  92. array('http://google.com::aa'),
  93. array('http://google.com:aa'),
  94. array('ftp://google.fr'),
  95. array('faked://google.fr'),
  96. array('http://127.0.0.1:aa/'),
  97. array('ftp://[::1]/'),
  98. array('http://[::1'),
  99. );
  100. }
  101. /**
  102. * @dataProvider getValidCustomUrls
  103. */
  104. public function testCustomProtocolIsValid($url)
  105. {
  106. $constraint = new Url(array(
  107. 'protocols' => array('ftp', 'file', 'git')
  108. ));
  109. $this->assertTrue($this->validator->isValid($url, $constraint));
  110. }
  111. public function getValidCustomUrls()
  112. {
  113. return array(
  114. array('ftp://google.com'),
  115. array('file://127.0.0.1'),
  116. array('git://[::1]/'),
  117. );
  118. }
  119. public function testMessageIsSet()
  120. {
  121. $constraint = new Url(array(
  122. 'message' => 'myMessage'
  123. ));
  124. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  125. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  126. $this->assertEquals($this->validator->getMessageParameters(), array(
  127. '{{ value }}' => 'foobar',
  128. ));
  129. }
  130. }