IpValidatorTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace Symfony\Tests\Component\Validator;
  3. use Symfony\Component\Validator\Constraints\Ip;
  4. use Symfony\Component\Validator\Constraints\IpValidator;
  5. class IpValidatorTest extends \PHPUnit_Framework_TestCase
  6. {
  7. protected $validator;
  8. protected function setUp()
  9. {
  10. $this->validator = new IpValidator();
  11. }
  12. public function testNullIsValid()
  13. {
  14. $this->assertTrue($this->validator->isValid(null, new Ip()));
  15. }
  16. public function testEmptyStringIsValid()
  17. {
  18. $this->assertTrue($this->validator->isValid('', new Ip()));
  19. }
  20. public function testExpectsStringCompatibleType()
  21. {
  22. $this->setExpectedException('Symfony\Component\Validator\Exception\UnexpectedTypeException');
  23. $this->validator->isValid(new \stdClass(), new Ip());
  24. }
  25. /**
  26. * @dataProvider getValidIpsV4
  27. */
  28. public function testValidIpsV4($date)
  29. {
  30. $this->assertTrue($this->validator->isValid($date, new Ip(array(
  31. 'version' => Ip::V4,
  32. ))));
  33. }
  34. public function getValidIpsV4()
  35. {
  36. return array(
  37. array('0.0.0.0'),
  38. array('255.255.255.255'),
  39. array('123.45.67.178'),
  40. );
  41. }
  42. /**
  43. * @dataProvider getValidIpsV6
  44. */
  45. public function testValidIpsV6($date)
  46. {
  47. $this->assertTrue($this->validator->isValid($date, new Ip(array(
  48. 'version' => Ip::V6,
  49. ))));
  50. }
  51. public function getValidIpsV6()
  52. {
  53. return array(
  54. array('2001:0db8:85a3:0000:0000:8a2e:0370:7334'),
  55. array('2001:0DB8:85A3:0000:0000:8A2E:0370:7334'),
  56. array('2001:0Db8:85a3:0000:0000:8A2e:0370:7334'),
  57. array('fe80:0000:0000:0000:0202:b3ff:fe1e:8329'),
  58. array('fe80:0:0:0:202:b3ff:fe1e:8329'),
  59. array('fe80::202:b3ff:fe1e:8329'),
  60. array('0:0:0:0:0:0:0:0'),
  61. array('::'),
  62. array('0::'),
  63. array('::0'),
  64. array('0::0'),
  65. // IPv4 mapped to IPv6
  66. array('2001:0db8:85a3:0000:0000:8a2e:0.0.0.0'),
  67. array('::0.0.0.0'),
  68. array('::255.255.255.255'),
  69. array('::123.45.67.178'),
  70. );
  71. }
  72. /**
  73. * @dataProvider getValidIpsAll
  74. */
  75. public function testValidIpsAll($date)
  76. {
  77. $this->assertTrue($this->validator->isValid($date, new Ip(array(
  78. 'version' => Ip::ALL,
  79. ))));
  80. }
  81. public function getValidIpsAll()
  82. {
  83. return array_merge($this->getValidIpsV4(), $this->getValidIpsV6());
  84. }
  85. /**
  86. * @dataProvider getInvalidIpsV4
  87. */
  88. public function testInvalidIpsV4($date)
  89. {
  90. $this->assertFalse($this->validator->isValid($date, new Ip(array(
  91. 'version' => Ip::V4,
  92. ))));
  93. }
  94. public function getInvalidIpsV4()
  95. {
  96. return array(
  97. array('0'),
  98. array('0.0'),
  99. array('0.0.0'),
  100. array('256.0.0.0'),
  101. array('0.256.0.0'),
  102. array('0.0.256.0'),
  103. array('0.0.0.256'),
  104. array('-1.0.0.0'),
  105. array('foobar'),
  106. );
  107. }
  108. /**
  109. * @dataProvider getInvalidIpsV6
  110. */
  111. public function testInvalidIpsV6($date)
  112. {
  113. $this->assertFalse($this->validator->isValid($date, new Ip(array(
  114. 'version' => Ip::V6,
  115. ))));
  116. }
  117. public function getInvalidIpsV6()
  118. {
  119. return array(
  120. array('z001:0db8:85a3:0000:0000:8a2e:0370:7334'),
  121. array('fe80'),
  122. array('fe80:8329'),
  123. array('fe80:::202:b3ff:fe1e:8329'),
  124. array('fe80::202:b3ff::fe1e:8329'),
  125. // IPv4 mapped to IPv6
  126. array('2001:0db8:85a3:0000:0000:8a2e:0370:0.0.0.0'),
  127. array('::0.0'),
  128. array('::0.0.0'),
  129. array('::256.0.0.0'),
  130. array('::0.256.0.0'),
  131. array('::0.0.256.0'),
  132. array('::0.0.0.256'),
  133. );
  134. }
  135. /**
  136. * @dataProvider getInvalidIpsAll
  137. */
  138. public function testInvalidIpsAll($date)
  139. {
  140. $this->assertFalse($this->validator->isValid($date, new Ip(array(
  141. 'version' => Ip::ALL,
  142. ))));
  143. }
  144. public function getInvalidIpsAll()
  145. {
  146. return array_merge($this->getInvalidIpsV4(), $this->getInvalidIpsV6());
  147. }
  148. public function testMessageIsSet()
  149. {
  150. $constraint = new Ip(array(
  151. 'message' => 'myMessage'
  152. ));
  153. $this->assertFalse($this->validator->isValid('foobar', $constraint));
  154. $this->assertEquals($this->validator->getMessageTemplate(), 'myMessage');
  155. $this->assertEquals($this->validator->getMessageParameters(), array(
  156. '{{ value }}' => 'foobar',
  157. ));
  158. }
  159. }