MaxTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * 最大值测试用例
  5. * Class MaxTest
  6. *
  7. * @internal
  8. */
  9. class MaxTest extends BaseTestCase
  10. {
  11. /*
  12. * 合法
  13. */
  14. public function testValidCase()
  15. {
  16. /*
  17. * int 测试整数情况(不超过)
  18. */
  19. $this->freeValidate();
  20. $this->validate->addColumn('price')->max(12);
  21. $bool = $this->validate->validate(['price' => 10]);
  22. $this->assertTrue($bool);
  23. /*
  24. * int 测试整数情况(相等)
  25. */
  26. $this->freeValidate();
  27. $this->validate->addColumn('price')->max(12);
  28. $bool = $this->validate->validate(['price' => 12]);
  29. $this->assertTrue($bool);
  30. /*
  31. * float 测试浮点数情况(不超过)
  32. */
  33. $this->freeValidate();
  34. $this->validate->addColumn('price')->max(12);
  35. $bool = $this->validate->validate(['price' => 10.9]);
  36. $this->assertTrue($bool);
  37. /*
  38. * float 测试浮点数情况(相等)
  39. */
  40. $this->freeValidate();
  41. $this->validate->addColumn('price')->max(10.9);
  42. $bool = $this->validate->validate(['price' => 10.9]);
  43. $this->assertTrue($bool);
  44. /*
  45. * 字符串整数 测试字符串整数情况(不超过)
  46. */
  47. $this->freeValidate();
  48. $this->validate->addColumn('price')->max(12);
  49. $bool = $this->validate->validate(['price' => '10']);
  50. $this->assertTrue($bool);
  51. /*
  52. * 字符串整数 测试字符串整数情况(相等)
  53. */
  54. $this->freeValidate();
  55. $this->validate->addColumn('price')->max(12);
  56. $bool = $this->validate->validate(['price' => '12']);
  57. $this->assertTrue($bool);
  58. /*
  59. * 字符串整数小数 测试字符串浮点数情况(不超过)
  60. */
  61. $this->freeValidate();
  62. $this->validate->addColumn('price')->max(12);
  63. $bool = $this->validate->validate(['price' => '10.9']);
  64. $this->assertTrue($bool);
  65. /*
  66. * 字符串整数 测试字符串浮点数情况(相等)
  67. */
  68. $this->freeValidate();
  69. $this->validate->addColumn('price')->max(12.1);
  70. $bool = $this->validate->validate(['price' => '12.1']);
  71. $this->assertTrue($bool);
  72. }
  73. /*
  74. * 默认错误信息
  75. */
  76. public function testDefaultErrorMsgCase()
  77. {
  78. /*
  79. * int
  80. */
  81. $this->freeValidate();
  82. $this->validate->addColumn('price')->max(20);
  83. $bool = $this->validate->validate(['price' => 21]);
  84. $this->assertFalse($bool);
  85. $this->assertEquals('price的值不能大于20', $this->validate->getError()->__toString());
  86. /*
  87. * float
  88. */
  89. $this->freeValidate();
  90. $this->validate->addColumn('price')->max(20);
  91. $bool = $this->validate->validate(['price' => 20.1]);
  92. $this->assertFalse($bool);
  93. $this->assertEquals('price的值不能大于20', $this->validate->getError()->__toString());
  94. /*
  95. * 字符串整数
  96. */
  97. $this->freeValidate();
  98. $this->validate->addColumn('price')->max(20);
  99. $bool = $this->validate->validate(['price' => '21']);
  100. $this->assertFalse($bool);
  101. $this->assertEquals('price的值不能大于20', $this->validate->getError()->__toString());
  102. /*
  103. * 字符串整数小数
  104. */
  105. $this->freeValidate();
  106. $this->validate->addColumn('price')->max(20);
  107. $bool = $this->validate->validate(['price' => '21.1']);
  108. $this->assertFalse($bool);
  109. $this->assertEquals('price的值不能大于20', $this->validate->getError()->__toString());
  110. /*
  111. * 非数字字符串
  112. */
  113. $this->freeValidate();
  114. $this->validate->addColumn('price')->max(20);
  115. $bool = $this->validate->validate(['price' => '21.1.1']);
  116. $this->assertFalse($bool);
  117. $this->assertEquals('price的值不能大于20', $this->validate->getError()->__toString());
  118. }
  119. /*
  120. * 自定义错误信息
  121. */
  122. public function testCustomErrorMsgCase()
  123. {
  124. $this->freeValidate();
  125. $this->validate->addColumn('price')->max(20, '价钱不超过20');
  126. $bool = $this->validate->validate(['price' => 21]);
  127. $this->assertFalse($bool);
  128. $this->assertEquals('价钱不超过20', $this->validate->getError()->__toString());
  129. }
  130. }