FuncTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. use EasySwoole\Spl\SplArray;
  4. use EasySwoole\Validate\Validate;
  5. /**
  6. * @internal
  7. */
  8. class FuncTest extends BaseTestCase
  9. {
  10. // 合法断言
  11. public function testValidCase()
  12. {
  13. $this->freeValidate();
  14. $this->validate->addColumn('callback')->func(function ($itemData, $column, Validate $validate) {
  15. return ($validate->getVerifyData() instanceof SplArray) && $column === 'callback' && $itemData === 0.001;
  16. });
  17. $validateResult = $this->validate->validate(['callback' => 0.001]);
  18. $this->assertTrue($validateResult);
  19. }
  20. // 默认错误信息断言
  21. public function testDefaultErrorMsgCase()
  22. {
  23. $this->freeValidate();
  24. $this->validate->addColumn('callback', '用户名')->func(function ($data, $name) {
  25. return false;
  26. });
  27. $validateResult = $this->validate->validate(['callback' => 0.001]);
  28. $this->assertFalse($validateResult);
  29. $this->assertEquals('用户名自定义验证失败', $this->validate->getError()->__toString());
  30. }
  31. // 自定义错误信息断言
  32. public function testCustomErrorMsgCase()
  33. {
  34. $this->freeValidate();
  35. $this->validate->addColumn('username')->func(function ($data, $name) {
  36. return false;
  37. }, '用户不存在');
  38. $validateResult = $this->validate->validate(['username' => 'admin']);
  39. $this->assertFalse($validateResult);
  40. $this->assertEquals('用户不存在', $this->validate->getError()->__toString());
  41. }
  42. }