验证器.长度验证

Testing Is Documentation

tests/Validate/Validator/StrlenTest.php验证器.长度验证 - 图1

Uses

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

验证通过的数据

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

  1. # Tests\Validate\Validator\StrlenTest::baseUseProvider
  2. public function baseUseProvider(): array
  3. {
  4. return [
  5. ['http://www.google.com', 21],
  6. ['http://queryphp.com', 19],
  7. ['foobar', 6],
  8. ['helloworld', 10],
  9. ['中国', 6],
  10. ['成都no1', 9],
  11. ];
  12. }

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

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

未验证通过的数据

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

  1. # Tests\Validate\Validator\StrlenTest::badProvider
  2. public function badProvider(): array
  3. {
  4. return [
  5. ['not numeric', 21],
  6. [[], 21],
  7. [new stdClass(), 21],
  8. [['foo', 'bar'], 21],
  9. [[1, 2], 21],
  10. ['tel:+1-816-555-1212', 21],
  11. ['foo', 21],
  12. ['bar', 21],
  13. ['urn:oasis:names:specification:docbook:dtd:xml:4.1.2', 21],
  14. ['world', 21],
  15. [null, 21],
  16. ];
  17. }

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

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

strlen 参数缺失

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