UrlValidatorTest.php 2.1 KB

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