NumericTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * 数字数字用例
  5. * Class NumericTest
  6. *
  7. * @internal
  8. */
  9. class NumericTest extends BaseTestCase
  10. {
  11. /*
  12. * 合法
  13. */
  14. public function testValidCase()
  15. {
  16. /*
  17. * int
  18. */
  19. $this->freeValidate();
  20. $this->validate->addColumn('age')->numeric();
  21. $bool = $this->validate->validate(['age' => 12]);
  22. $this->assertTrue($bool);
  23. /*
  24. * float
  25. */
  26. $this->freeValidate();
  27. $this->validate->addColumn('price')->numeric();
  28. $bool = $this->validate->validate(['price' => 2.3]);
  29. $this->assertTrue($bool);
  30. /*
  31. * 字符整数
  32. */
  33. $this->freeValidate();
  34. $this->validate->addColumn('age')->numeric();
  35. $bool = $this->validate->validate(['age' => '12']);
  36. $this->assertTrue($bool);
  37. /*
  38. * 字符小数
  39. */
  40. $this->freeValidate();
  41. $this->validate->addColumn('price')->numeric();
  42. $bool = $this->validate->validate(['price' => '2.3']);
  43. $this->assertTrue($bool);
  44. }
  45. /*
  46. * 默认错误信息
  47. */
  48. public function testDefaultErrorMsgCase()
  49. {
  50. /*
  51. * 非数字
  52. */
  53. $this->freeValidate();
  54. $this->validate->addColumn('price', '价格')->numeric();
  55. $bool = $this->validate->validate(['price' => 'blank']);
  56. $this->assertFalse($bool);
  57. $this->assertEquals('价格只能是数字类型', $this->validate->getError()->__toString());
  58. }
  59. /*
  60. * 自定义错误信息
  61. */
  62. public function testCustomErrorMsgCase()
  63. {
  64. /*
  65. * 非数字
  66. */
  67. $this->freeValidate();
  68. $this->validate->addColumn('price')->numeric('价格必须是数字');
  69. $bool = $this->validate->validate(['price' => 'blank']);
  70. $this->assertFalse($bool);
  71. $this->assertEquals('价格必须是数字', $this->validate->getError()->__toString());
  72. }
  73. }