验证器.大于或者全等

Testing Is Documentation

tests/Validate/Validator/EqualGreaterThanTest.php验证器.大于或者全等 - 图1

Uses

  1. <?php
  2. use Leevel\Validate\Validator;

验证通过的数据

以下是通过的校验数据示例。

  1. # Tests\Validate\Validator\EqualGreaterThanTest::baseUseProvider
  2. public function baseUseProvider(): array
  3. {
  4. return [
  5. [3, 2],
  6. [1.5, '1.1'],
  7. [2, '1.5'],
  8. [3, '1.5'],
  9. [4, '1.5'],
  10. [4.5, '1.5'],
  11. ['b', 'a'],
  12. ['c', 'a'],
  13. ['foo', 'bar'],
  14. [1.1, '1.1'],
  15. [0, '0'],
  16. [0, 0],
  17. ];
  18. }

上面的数据是测试的数据提供者。

  1. public function testBaseUse($value, $param): void
  2. {
  3. $validate = new Validator(
  4. [
  5. 'name' => $value,
  6. ],
  7. [
  8. 'name' => 'equal_greater_than:'.$param,
  9. ]
  10. );
  11. $this->assertTrue($validate->success());
  12. }

未验证通过的数据

以下是未通过的校验数据示例。

  1. # Tests\Validate\Validator\EqualGreaterThanTest::badProvider
  2. public function badProvider(): array
  3. {
  4. return [
  5. [2, 3],
  6. [1.1, '1.5'],
  7. [1.5, '2'],
  8. [1.5, '3'],
  9. [1.5, '4'],
  10. [1.5, '4.5'],
  11. ['a', 'b'],
  12. ['a', 'c'],
  13. ['bar', 'foo'],
  14. ['0', '0'],
  15. ];
  16. }

上面的数据是测试的数据提供者。

  1. public function testBad($value, $param): void
  2. {
  3. $validate = new Validator(
  4. [
  5. 'name' => $value,
  6. ],
  7. [
  8. 'name' => 'equal_greater_than:'.$param,
  9. ]
  10. );
  11. $this->assertFalse($validate->success());
  12. }

特殊例子的数据校验

特别注意字符串和数字的严格区分。

  1. public function testSpecial(): void
  2. {
  3. $validate = new Validator();
  4. $this->assertTrue($validate->equalGreaterThan('0', '0'));
  5. $this->assertFalse($validate->equalGreaterThan(0, '0'));
  6. $this->assertFalse($validate->equalGreaterThan('0', 0));
  7. }

equal_greater_than 参数缺失

  1. public function testMissParam(): void
  2. {
  3. $this->expectException(\InvalidArgumentException::class);
  4. $this->expectExceptionMessage(
  5. 'Missing the first element of param.'
  6. );
  7. $validate = new Validator(
  8. [
  9. 'name' => '',
  10. ],
  11. [
  12. 'name' => 'equal_greater_than',
  13. ]
  14. );
  15. $validate->success();
  16. }