UrlValidatorTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. public 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 testExpectsStringCompatibleType()
  17. {
  18. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  19. $this->validator->isValid(new \stdClass(), new Url());
  20. }
  21. /**
  22. * @dataProvider getValidUrls
  23. */
  24. public function testValidUrls($url)
  25. {
  26. $this->assertTrue($this->validator->isValid($url, new Url()));
  27. }
  28. public function getValidUrls()
  29. {
  30. return array(
  31. array('http://www.google.com'),
  32. array('https://google.com/'),
  33. array('https://google.com:80/'),
  34. array('http://www.symfony-project.com/'),
  35. array('http://127.0.0.1/'),
  36. array('http://127.0.0.1:80/'),
  37. );
  38. }
  39. /**
  40. * @dataProvider getInvalidUrls
  41. */
  42. public function testInvalidUrls($url)
  43. {
  44. $this->assertFalse($this->validator->isValid($url, new Url()));
  45. }
  46. public function getInvalidUrls()
  47. {
  48. return array(
  49. array('google.com'),
  50. array('http:/google.com'),
  51. array('http://google.com::aa'),
  52. );
  53. }
  54. public function testMessageIsSet()
  55. {
  56. $constraint = new Url(array(
  57. 'message' => 'myMessage'
  58. ));
  59. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  60. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  61. $this->assertEquals($this->validator->getMessageParameters(), array(
  62. '{{ value }}' => 'foobar',
  63. ));
  64. }
  65. }