BetweenTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * 是否在两值之间(包含极端值)
  5. * Class ActiveUrlTest
  6. *
  7. * @internal
  8. */
  9. class BetweenTest extends BaseTestCase
  10. {
  11. // 合法断言
  12. public function testValidCase()
  13. {
  14. // 整数表示
  15. $this->freeValidate();
  16. $this->validate->addColumn('number')->between(5, 10);
  17. $validateResult = $this->validate->validate(['number' => 6]);
  18. $this->assertTrue($validateResult);
  19. // 小数表示
  20. $this->freeValidate();
  21. $this->validate->addColumn('number')->between(5, 10);
  22. $validateResult = $this->validate->validate(['number' => 6.33333]);
  23. $this->assertTrue($validateResult);
  24. // 字符串表示
  25. $this->freeValidate();
  26. $this->validate->addColumn('number')->between(5, 10);
  27. $validateResult = $this->validate->validate(['number' => '6']);
  28. $this->assertTrue($validateResult);
  29. // 等于最小值
  30. $this->freeValidate();
  31. $this->validate->addColumn('number')->between(5, 10);
  32. $validateResult = $this->validate->validate(['number' => 5]);
  33. $this->assertTrue($validateResult);
  34. // 等于最大值
  35. $this->freeValidate();
  36. $this->validate->addColumn('number')->between(5, 10);
  37. $validateResult = $this->validate->validate(['number' => 10]);
  38. $this->assertTrue($validateResult);
  39. }
  40. // 默认错误信息断言
  41. public function testDefaultErrorMsgCase()
  42. {
  43. // 不在值之间
  44. $this->freeValidate();
  45. $this->validate->addColumn('number')->between(5, 10);
  46. $validateResult = $this->validate->validate(['number' => 20]);
  47. $this->assertFalse($validateResult);
  48. $this->assertEquals('number只能在 5 - 10 之间', $this->validate->getError()->__toString());
  49. // 不是合法值
  50. $this->freeValidate();
  51. $this->validate->addColumn('number', '年龄')->between(5, 10);
  52. $validateResult = $this->validate->validate(['number' => 'aaa']);
  53. $this->assertFalse($validateResult);
  54. $this->assertEquals('年龄只能在 5 - 10 之间', $this->validate->getError()->__toString());
  55. }
  56. // 自定义错误信息断言
  57. public function testCustomErrorMsgCase()
  58. {
  59. // 不在值之间
  60. $this->freeValidate();
  61. $this->validate->addColumn('number')->between(5, 10, '您输入的年龄不符');
  62. $validateResult = $this->validate->validate(['number' => '!']);
  63. $this->assertFalse($validateResult);
  64. $this->assertEquals('您输入的年龄不符', $this->validate->getError()->__toString());
  65. }
  66. }