MinTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace EasySwoole\Validate\tests;
  3. /**
  4. * 最小值测试用例
  5. * Class MinTest
  6. *
  7. * @internal
  8. */
  9. class MinTest extends BaseTestCase
  10. {
  11. /*
  12. * 合法
  13. */
  14. public function testValidCase()
  15. {
  16. /*
  17. * int 测试整数情况(最小值)
  18. */
  19. $this->freeValidate();
  20. $this->validate->addColumn('price')->min(10);
  21. $bool = $this->validate->validate(['price' => 12]);
  22. $this->assertTrue($bool);
  23. /*
  24. * int 测试整数情况(相等)
  25. */
  26. $this->freeValidate();
  27. $this->validate->addColumn('price')->min(10);
  28. $bool = $this->validate->validate(['price' => 10]);
  29. $this->assertTrue($bool);
  30. /*
  31. * float 测试浮点数情况(最小值)
  32. */
  33. $this->freeValidate();
  34. $this->validate->addColumn('price')->min(10);
  35. $bool = $this->validate->validate(['price' => 10.9]);
  36. $this->assertTrue($bool);
  37. /*
  38. * float 测试浮点数情况(相等)
  39. */
  40. $this->freeValidate();
  41. $this->validate->addColumn('price')->min(10.1);
  42. $bool = $this->validate->validate(['price' => 10.1]);
  43. $this->assertTrue($bool);
  44. /*
  45. * 字符串整数(最小值)
  46. */
  47. $this->freeValidate();
  48. $this->validate->addColumn('price')->min(10);
  49. $bool = $this->validate->validate(['price' => '12']);
  50. $this->assertTrue($bool);
  51. /*
  52. * 字符串整数(相等)
  53. */
  54. $this->freeValidate();
  55. $this->validate->addColumn('price')->min(10);
  56. $bool = $this->validate->validate(['price' => '10']);
  57. $this->assertTrue($bool);
  58. /*
  59. * 字符串浮点数(最小值)
  60. */
  61. $this->freeValidate();
  62. $this->validate->addColumn('price')->min(10);
  63. $bool = $this->validate->validate(['price' => '10.9']);
  64. $this->assertTrue($bool);
  65. /*
  66. * 字符串浮点数(相等)
  67. */
  68. $this->freeValidate();
  69. $this->validate->addColumn('price')->min(10.1);
  70. $bool = $this->validate->validate(['price' => '10.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')->min(20);
  83. $bool = $this->validate->validate(['price' => 10]);
  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')->min(20);
  91. $bool = $this->validate->validate(['price' => 11.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')->min(20);
  99. $bool = $this->validate->validate(['price' => '11']);
  100. $this->assertFalse($bool);
  101. $this->assertEquals('price的值不能小于20', $this->validate->getError()->__toString());
  102. /*
  103. * 字符串整数小数
  104. */
  105. $this->freeValidate();
  106. $this->validate->addColumn('price')->min(20);
  107. $bool = $this->validate->validate(['price' => '11.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')->min(20);
  115. $bool = $this->validate->validate(['price' => '11.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')->min(20, '价钱至少20');
  126. $bool = $this->validate->validate(['price' => 11]);
  127. $this->assertFalse($bool);
  128. $this->assertEquals('价钱至少20', $this->validate->getError()->__toString());
  129. }
  130. }