123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace EasySwoole\Validate\tests;
- /**
- * 最小值测试用例
- * Class MinTest
- *
- * @internal
- */
- class MinTest extends BaseTestCase
- {
- /*
- * 合法
- */
- public function testValidCase()
- {
- /*
- * int 测试整数情况(最小值)
- */
- $this->freeValidate();
- $this->validate->addColumn('price')->min(10);
- $bool = $this->validate->validate(['price' => 12]);
- $this->assertTrue($bool);
- /*
- * int 测试整数情况(相等)
- */
- $this->freeValidate();
- $this->validate->addColumn('price')->min(10);
- $bool = $this->validate->validate(['price' => 10]);
- $this->assertTrue($bool);
- /*
- * float 测试浮点数情况(最小值)
- */
- $this->freeValidate();
- $this->validate->addColumn('price')->min(10);
- $bool = $this->validate->validate(['price' => 10.9]);
- $this->assertTrue($bool);
- /*
- * float 测试浮点数情况(相等)
- */
- $this->freeValidate();
- $this->validate->addColumn('price')->min(10.1);
- $bool = $this->validate->validate(['price' => 10.1]);
- $this->assertTrue($bool);
- /*
- * 字符串整数(最小值)
- */
- $this->freeValidate();
- $this->validate->addColumn('price')->min(10);
- $bool = $this->validate->validate(['price' => '12']);
- $this->assertTrue($bool);
- /*
- * 字符串整数(相等)
- */
- $this->freeValidate();
- $this->validate->addColumn('price')->min(10);
- $bool = $this->validate->validate(['price' => '10']);
- $this->assertTrue($bool);
- /*
- * 字符串浮点数(最小值)
- */
- $this->freeValidate();
- $this->validate->addColumn('price')->min(10);
- $bool = $this->validate->validate(['price' => '10.9']);
- $this->assertTrue($bool);
- /*
- * 字符串浮点数(相等)
- */
- $this->freeValidate();
- $this->validate->addColumn('price')->min(10.1);
- $bool = $this->validate->validate(['price' => '10.1']);
- $this->assertTrue($bool);
- }
- /*
- * 默认错误信息
- */
- public function testDefaultErrorMsgCase()
- {
- /*
- * int
- */
- $this->freeValidate();
- $this->validate->addColumn('price')->min(20);
- $bool = $this->validate->validate(['price' => 10]);
- $this->assertFalse($bool);
- $this->assertEquals('price的值不能小于20', $this->validate->getError()->__toString());
- /*
- * float
- */
- $this->freeValidate();
- $this->validate->addColumn('price')->min(20);
- $bool = $this->validate->validate(['price' => 11.1]);
- $this->assertFalse($bool);
- $this->assertEquals('price的值不能小于20', $this->validate->getError()->__toString());
- /*
- * 字符串整数
- */
- $this->freeValidate();
- $this->validate->addColumn('price')->min(20);
- $bool = $this->validate->validate(['price' => '11']);
- $this->assertFalse($bool);
- $this->assertEquals('price的值不能小于20', $this->validate->getError()->__toString());
- /*
- * 字符串整数小数
- */
- $this->freeValidate();
- $this->validate->addColumn('price')->min(20);
- $bool = $this->validate->validate(['price' => '11.1']);
- $this->assertFalse($bool);
- $this->assertEquals('price的值不能小于20', $this->validate->getError()->__toString());
- /*
- * 非数字字符串
- */
- $this->freeValidate();
- $this->validate->addColumn('price')->min(20);
- $bool = $this->validate->validate(['price' => '11.1.1']);
- $this->assertFalse($bool);
- $this->assertEquals('price的值不能小于20', $this->validate->getError()->__toString());
- }
- /*
- * 自定义错误信息
- */
- public function testCustomErrorMsgCase()
- {
- $this->freeValidate();
- $this->validate->addColumn('price')->min(20, '价钱至少20');
- $bool = $this->validate->validate(['price' => 11]);
- $this->assertFalse($bool);
- $this->assertEquals('价钱至少20', $this->validate->getError()->__toString());
- }
- }
|