IsIpTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * @internal
  5. */
  6. class IsIpTest extends BaseTestCase
  7. {
  8. // 合法断言
  9. public function testValidCase()
  10. {
  11. // 合法的IPv4
  12. $this->freeValidate();
  13. $this->validate->addColumn('address')->isIp();
  14. $validateResult = $this->validate->validate(['address' => '192.168.1.1']);
  15. $this->assertTrue($validateResult);
  16. // 合法的IPv6
  17. $this->freeValidate();
  18. $this->validate->addColumn('address')->isIp();
  19. $validateResult = $this->validate->validate(['address' => '2001:0db8:85a3:08d3:1319:8a2e:0370:7334']);
  20. $this->assertTrue($validateResult);
  21. }
  22. // 默认错误信息断言
  23. public function testDefaultErrorMsgCase()
  24. {
  25. // 不是IP
  26. $this->freeValidate();
  27. $this->validate->addColumn('address', '回调入口')->isIp();
  28. $validateResult = $this->validate->validate(['address' => 'http://baidu.com']);
  29. $this->assertFalse($validateResult);
  30. $this->assertEquals('回调入口不是有效的IP地址', $this->validate->getError()->__toString());
  31. }
  32. // 自定义错误信息断言
  33. public function testCustomErrorMsgCase()
  34. {
  35. // 范围不合法
  36. $this->freeValidate();
  37. $this->validate->addColumn('address')->isIp('请输入合法的IP地址');
  38. $validateResult = $this->validate->validate(['address' => '256.256.256.256']);
  39. $this->assertFalse($validateResult);
  40. $this->assertEquals('请输入合法的IP地址', $this->validate->getError()->__toString());
  41. }
  42. }