UrlValidatorTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace Symfony\Tests\Component\Validator;
  3. use Symfony\Component\Validator\Constraints\Url;
  4. use Symfony\Component\Validator\Constraints\UrlValidator;
  5. class UrlValidatorTest extends \PHPUnit_Framework_TestCase
  6. {
  7. protected $validator;
  8. protected function setUp()
  9. {
  10. $this->validator = new UrlValidator();
  11. }
  12. public function testNullIsValid()
  13. {
  14. $this->assertTrue($this->validator->isValid(null, new Url()));
  15. }
  16. public function testEmptyStringIsValid()
  17. {
  18. $this->assertTrue($this->validator->isValid('', new Url()));
  19. }
  20. public function testExpectsStringCompatibleType()
  21. {
  22. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  23. $this->validator->isValid(new \stdClass(), new Url());
  24. }
  25. /**
  26. * @dataProvider getValidUrls
  27. */
  28. public function testValidUrls($url)
  29. {
  30. $this->assertTrue($this->validator->isValid($url, new Url()));
  31. }
  32. public function getValidUrls()
  33. {
  34. return array(
  35. array('http://www.google.com'),
  36. array('https://google.com/'),
  37. array('https://google.com:80/'),
  38. array('http://www.symfony-project.com/'),
  39. array('http://127.0.0.1/'),
  40. array('http://127.0.0.1:80/'),
  41. );
  42. }
  43. /**
  44. * @dataProvider getInvalidUrls
  45. */
  46. public function testInvalidUrls($url)
  47. {
  48. $this->assertFalse($this->validator->isValid($url, new Url()));
  49. }
  50. public function getInvalidUrls()
  51. {
  52. return array(
  53. array('google.com'),
  54. array('http:/google.com'),
  55. array('http://google.com::aa'),
  56. );
  57. }
  58. public function testMessageIsSet()
  59. {
  60. $constraint = new Url(array(
  61. 'message' => 'myMessage'
  62. ));
  63. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  64. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  65. $this->assertEquals($this->validator->getMessageParameters(), array(
  66. '{{ value }}' => 'foobar',
  67. ));
  68. }
  69. }