IpValidatorTest.php 4.9 KB

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