IpTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(__FILE__) . '/../../TestHelper.php';
  26. /**
  27. * @see Zend_Validate_Ip
  28. */
  29. require_once 'Zend/Validate/Ip.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Validate
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Validate_IpTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * Zend_Validate_Ip object
  41. *
  42. * @var Zend_Validate_Ip
  43. */
  44. protected $_validator;
  45. /**
  46. * Creates a new Zend_Validate_Ip object for each test method
  47. *
  48. * @return void
  49. */
  50. public function setUp()
  51. {
  52. $this->_validator = new Zend_Validate_Ip();
  53. }
  54. /**
  55. * Ensures that the validator follows expected behavior
  56. *
  57. * @return void
  58. */
  59. public function testBasic()
  60. {
  61. $this->assertTrue($this->_validator->isValid('1.2.3.4'));
  62. $this->assertTrue($this->_validator->isValid('10.0.0.1'));
  63. $this->assertTrue($this->_validator->isValid('255.255.255.255'));
  64. $this->assertFalse($this->_validator->isValid('0.0.0.256'));
  65. $this->assertFalse($this->_validator->isValid('1.2.3.4.5'));
  66. }
  67. public function testZeroIpForZF2420()
  68. {
  69. $this->assertTrue($this->_validator->isValid('0.0.0.0'));
  70. }
  71. /**
  72. * Ensures that getMessages() returns expected default value
  73. *
  74. * @return void
  75. */
  76. public function testGetMessages()
  77. {
  78. $this->assertEquals(array(), $this->_validator->getMessages());
  79. }
  80. public function testInvalidIpForZF4809()
  81. {
  82. $this->assertFalse($this->_validator->isValid('1.2.333'));
  83. }
  84. public function testInvalidIpForZF3435()
  85. {
  86. $this->assertFalse($this->_validator->isValid('192.168.0.2 adfs'));
  87. }
  88. /**
  89. * @see ZF-2694
  90. */
  91. public function testIPv6addresses()
  92. {
  93. if (!function_exists('inet_pton')) {
  94. $this->markTestIncomplete('No IPv6 support within this PHP release');
  95. }
  96. $this->assertTrue($this->_validator->isValid('::127.0.0.1'));
  97. }
  98. /**
  99. * @ZF-4352
  100. */
  101. public function testNonStringValidation()
  102. {
  103. $this->assertFalse($this->_validator->isValid(array(1 => 1)));
  104. }
  105. }