BoolTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * @internal
  5. */
  6. class BoolTest extends BaseTestCase
  7. {
  8. // 合法断言
  9. public function testValidCase()
  10. {
  11. // 值为true
  12. $this->freeValidate();
  13. $this->validate->addColumn('boolean')->bool();
  14. $validateResult = $this->validate->validate(['boolean' => true]);
  15. $this->assertTrue($validateResult);
  16. // 值为 1 等同于 true
  17. $this->freeValidate();
  18. $this->validate->addColumn('boolean')->bool();
  19. $validateResult = $this->validate->validate(['boolean' => 1]);
  20. $this->assertTrue($validateResult);
  21. // 值为false
  22. $this->freeValidate();
  23. $this->validate->addColumn('boolean')->bool();
  24. $validateResult = $this->validate->validate(['boolean' => false]);
  25. $this->assertTrue($validateResult);
  26. // 值为 0 等同于 false
  27. $this->freeValidate();
  28. $this->validate->addColumn('boolean')->bool();
  29. $validateResult = $this->validate->validate(['boolean' => 0]);
  30. $this->assertTrue($validateResult);
  31. }
  32. // 默认错误信息断言 TODO 需要确认修正
  33. public function testDefaultErrorMsgCase()
  34. {
  35. // 值为文本值无法通过
  36. $this->freeValidate();
  37. $this->validate->addColumn('boolean')->bool();
  38. $validateResult = $this->validate->validate(['boolean' => 'true']);
  39. $this->assertFalse($validateResult);
  40. $this->assertEquals('boolean只能是布尔值', $this->validate->getError()->__toString());
  41. // 值为文本数字时无法通过
  42. $this->freeValidate();
  43. $this->validate->addColumn('boolean', '状态')->bool();
  44. $validateResult = $this->validate->validate(['boolean' => '1']);
  45. $this->assertFalse($validateResult);
  46. $this->assertEquals('状态只能是布尔值', $this->validate->getError()->__toString());
  47. }
  48. // 自定义错误信息断言
  49. public function testCustomErrorMsgCase()
  50. {
  51. // 异常值测试
  52. $this->freeValidate();
  53. $this->validate->addColumn('boolean')->bool('状态只能是开启或关闭');
  54. $validateResult = $this->validate->validate(['boolean' => null]);
  55. $this->assertFalse($validateResult);
  56. $this->assertEquals('状态只能是开启或关闭', $this->validate->getError()->__toString());
  57. }
  58. }