UrlValidatorTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. array('ftp://google.com/foo.tgz'),
  38. array('ftps://google.com/foo.tgz'),
  39. );
  40. }
  41. /**
  42. * @dataProvider getInvalidUrls
  43. */
  44. public function testInvalidUrls($url)
  45. {
  46. $this->assertFalse($this->validator->isValid($url, new Url()));
  47. }
  48. public function getInvalidUrls()
  49. {
  50. return array(
  51. array('google.com'),
  52. array('http:/google.com'),
  53. array('http://google.com::aa'),
  54. );
  55. }
  56. public function testMessageIsSet()
  57. {
  58. $constraint = new Url(array(
  59. 'message' => 'myMessage'
  60. ));
  61. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  62. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  63. $this->assertEquals($this->validator->getMessageParameters(), array(
  64. '{{ value }}' => 'foobar',
  65. ));
  66. }
  67. }