IntegerTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * @internal
  5. */
  6. class IntegerTest extends BaseTestCase
  7. {
  8. // 合法断言
  9. public function testValidCase()
  10. {
  11. // 正常的int类型
  12. $this->freeValidate();
  13. $this->validate->addColumn('integer')->integer();
  14. $validateResult = $this->validate->validate(['integer' => 1]);
  15. $this->assertTrue($validateResult);
  16. // 文本型int
  17. $this->freeValidate();
  18. $this->validate->addColumn('integer')->integer();
  19. $validateResult = $this->validate->validate(['integer' => '100']);
  20. $this->assertTrue($validateResult);
  21. }
  22. // 默认错误信息断言
  23. public function testDefaultErrorMsgCase()
  24. {
  25. // 不是一个数字
  26. $this->freeValidate();
  27. $this->validate->addColumn('integer')->integer();
  28. $validateResult = $this->validate->validate(['integer' => 'xxx']);
  29. $this->assertFalse($validateResult);
  30. $this->assertEquals('integer只能是整数', $this->validate->getError()->__toString());
  31. // 不是一个整数
  32. $this->freeValidate();
  33. $this->validate->addColumn('integer', '个数')->integer();
  34. $validateResult = $this->validate->validate(['integer' => 0.001]);
  35. $this->assertFalse($validateResult);
  36. $this->assertEquals('个数只能是整数', $this->validate->getError()->__toString());
  37. }
  38. // 自定义错误信息断言
  39. public function testCustomErrorMsgCase()
  40. {
  41. $this->freeValidate();
  42. $this->validate->addColumn('integer')->integer('请输入正确的数量');
  43. $validateResult = $this->validate->validate(['integer' => 0.001]);
  44. $this->assertFalse($validateResult);
  45. $this->assertEquals('请输入正确的数量', $this->validate->getError()->__toString());
  46. }
  47. }