UrlValidatorTest.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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;
  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. );
  67. }
  68. public function testMessageIsSet()
  69. {
  70. $constraint = new Url(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. }