UrlValidatorTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. );
  50. }
  51. /**
  52. * @dataProvider getInvalidUrls
  53. */
  54. public function testInvalidUrls($url)
  55. {
  56. $this->assertFalse($this->validator->isValid($url, new Url()));
  57. }
  58. public function getInvalidUrls()
  59. {
  60. return array(
  61. array('google.com'),
  62. array('http:/google.com'),
  63. array('http://google.com::aa'),
  64. );
  65. }
  66. public function testMessageIsSet()
  67. {
  68. $constraint = new Url(array(
  69. 'message' => 'myMessage'
  70. ));
  71. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  72. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  73. $this->assertEquals($this->validator->getMessageParameters(), array(
  74. '{{ value }}' => 'foobar',
  75. ));
  76. }
  77. }