MoneyTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * @internal
  5. */
  6. class MoneyTest extends BaseTestCase
  7. {
  8. /*
  9. * 合法
  10. */
  11. public function testValidCase()
  12. {
  13. $this->freeValidate();
  14. $this->validate->addColumn('no')->money(2);
  15. $bool = $this->validate->validate(['no' => 1111.13]);
  16. $this->assertTrue($bool);
  17. $this->freeValidate();
  18. $this->validate->addColumn('no')->money(1);
  19. $bool = $this->validate->validate(['no' => 1111.1]);
  20. $this->assertTrue($bool);
  21. }
  22. /*
  23. * 默认错误信息
  24. */
  25. public function testDefaultErrorMsgCase()
  26. {
  27. $this->freeValidate();
  28. $this->validate->addColumn('no')->money(2);
  29. $bool = $this->validate->validate(['no' => 1234]);
  30. $this->assertFalse($bool);
  31. $this->assertEquals('no必须是合法的金额', $this->validate->getError()->__toString());
  32. }
  33. /*
  34. * 自定义错误信息
  35. */
  36. public function testCustomErrorMsgCase()
  37. {
  38. $this->freeValidate();
  39. $this->validate->addColumn('no')->money(2, 'no必须是合法的金额!');
  40. $bool = $this->validate->validate(['no' => 1161709455.999]);
  41. $this->assertFalse($bool);
  42. $this->assertEquals('no必须是合法的金额!', $this->validate->getError()->__toString());
  43. }
  44. /*
  45. * 验证整数金额
  46. */
  47. public function testValidateIntegerMoneyCase()
  48. {
  49. $this->freeValidate();
  50. $this->validate->addColumn('no')->money(0, 'no必须是合法的金额!');
  51. $bool = $this->validate->validate(['no' => 110.01]);
  52. $this->assertFalse($bool);
  53. $this->assertEquals('no必须是合法的金额!', $this->validate->getError()->__toString());
  54. $this->freeValidate();
  55. $this->validate->addColumn('no')->money(0, 'no必须是合法的金额!');
  56. $bool = $this->validate->validate(['no' => 110]);
  57. $this->assertTrue($bool);
  58. $this->assertNull($this->validate->getError());
  59. }
  60. }