验证器.验证数据最大长度

Testing Is Documentation

tests/Validate/Validator/MaxLengthTest.php验证器.验证数据最大长度 - 图1

Uses

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

验证通过的数据

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

  1. # Tests\Validate\Validator\MaxLengthTest::baseUseProvider
  2. public function baseUseProvider(): array
  3. {
  4. return [
  5. [2, 1],
  6. ['中国', 2],
  7. ['中国', 3],
  8. ['成都', 2],
  9. ['hello', 5],
  10. ['hello', 6],
  11. ['foo', 3],
  12. ['world', 5],
  13. ['中国no1', 5],
  14. [true, 1],
  15. [false, 0],
  16. ];
  17. }

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

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

未验证通过的数据

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

  1. # Tests\Validate\Validator\MaxLengthTest::badProvider
  2. public function badProvider(): array
  3. {
  4. return [
  5. [2, 0],
  6. ['中国', 1],
  7. ['成都', 1],
  8. ['hello', 4],
  9. ['foo', 2],
  10. ['world', 4],
  11. ['中国no1', 4],
  12. [new stdClass(), 0],
  13. [['foo', 'bar'], 0],
  14. [[1, 2], 0],
  15. [1, 0],
  16. [[[], []], 0],
  17. [true, 0],
  18. ];
  19. }

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

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

max_length 参数缺失

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