验证器.是否处于某个范围

Testing Is Documentation

tests/Validate/Validator/InTest.php验证器.是否处于某个范围 - 图1

Uses

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

验证通过的数据

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

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

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

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

未验证通过的数据

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

  1. # Tests\Validate\Validator\InTest::badProvider
  2. public function badProvider(): array
  3. {
  4. return [
  5. ['0.1', '1,5'],
  6. ['5.5', '1,5'],
  7. ['8', '1,5'],
  8. ['c', 'h,t'],
  9. ['w', 'h,t'],
  10. ['c', 'foo,bar'],
  11. ['w', 'h,hello,world,t'],
  12. ];
  13. }

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

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

in 参数缺失

  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' => 'in',
  13. ]
  14. );
  15. $validate->success();
  16. }