FloatTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * @internal
  5. */
  6. class FloatTest extends BaseTestCase
  7. {
  8. // 合法断言
  9. public function testValidCase()
  10. {
  11. // 小数位浮点数
  12. $this->freeValidate();
  13. $this->validate->addColumn('float')->float();
  14. $validateResult = $this->validate->validate(['float' => 0.001]);
  15. $this->assertTrue($validateResult);
  16. // 字符串表达
  17. $this->freeValidate();
  18. $this->validate->addColumn('float')->float();
  19. $validateResult = $this->validate->validate(['float' => '0.001']);
  20. $this->assertTrue($validateResult);
  21. // 整数作为浮点数
  22. $this->freeValidate();
  23. $this->validate->addColumn('float')->float();
  24. $validateResult = $this->validate->validate(['float' => 2]);
  25. $this->assertTrue($validateResult);
  26. }
  27. // 默认错误信息断言
  28. public function testDefaultErrorMsgCase()
  29. {
  30. // 不是合法的浮点值
  31. $this->freeValidate();
  32. $this->validate->addColumn('float')->float();
  33. $validateResult = $this->validate->validate(['float' => 'aaa']);
  34. $this->assertFalse($validateResult);
  35. $this->assertEquals('float只能是浮点数', $this->validate->getError()->__toString());
  36. }
  37. // 自定义错误信息断言
  38. public function testCustomErrorMsgCase()
  39. {
  40. $this->freeValidate();
  41. $this->validate->addColumn('float')->float('请输入一个浮点数');
  42. $validateResult = $this->validate->validate(['float' => 'a']);
  43. $this->assertFalse($validateResult);
  44. $this->assertEquals('请输入一个浮点数', $this->validate->getError()->__toString());
  45. }
  46. }