ActiveUrlTest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * 是否一个能连通的URL
  5. * Class ActiveUrlTest
  6. *
  7. * @internal
  8. */
  9. class ActiveUrlTest extends BaseTestCase
  10. {
  11. // 合法断言
  12. public function testValidCase()
  13. {
  14. // 可以连通的网址
  15. $this->freeValidate();
  16. $this->validate->addColumn('url')->activeUrl();
  17. $validateResult = $this->validate->validate(['url' => 'http://baidu.com']);
  18. $this->assertTrue($validateResult);
  19. }
  20. // 默认错误信息断言
  21. public function testDefaultErrorMsgCase()
  22. {
  23. // 有效网址但不能连通
  24. $this->freeValidate();
  25. $this->validate->addColumn('url', '网站')->activeUrl();
  26. $validateResult = $this->validate->validate(['url' => 'http://xxx.cn']);
  27. $this->assertFalse($validateResult);
  28. $this->assertEquals('网站必须是可访问的网址', $this->validate->getError()->__toString());
  29. // 无效的网址
  30. $this->freeValidate();
  31. $this->validate->addColumn('url')->activeUrl();
  32. $validateResult = $this->validate->validate(['url' => 'this is not a url']);
  33. $this->assertFalse($validateResult);
  34. $this->assertEquals('url必须是可访问的网址', $this->validate->getError()->__toString());
  35. }
  36. // 自定义错误信息断言
  37. public function testCustomErrorMsgCase()
  38. {
  39. $this->freeValidate();
  40. $this->validate->addColumn('url', '网站')->activeUrl('您输入的网址无效');
  41. $validateResult = $this->validate->validate(['url' => 'http://xxx.cn']);
  42. $this->assertFalse($validateResult);
  43. $this->assertEquals('您输入的网址无效', $this->validate->getError()->__toString());
  44. }
  45. }