验证器.值是否为空

Testing Is Documentation

tests/Validate/Validator/IsEmptyTest.php验证器.值是否为空 - 图1

Uses

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

验证通过的数据

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

  1. # Tests\Validate\Validator\IsEmptyTest::baseUseProvider
  2. public function baseUseProvider(): array
  3. {
  4. $val = null;
  5. return [
  6. [''],
  7. [0],
  8. [0.0],
  9. ['0'],
  10. [null],
  11. [false],
  12. [[]],
  13. [$val],
  14. ];
  15. }

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

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

未验证通过的数据

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

  1. # Tests\Validate\Validator\IsEmptyTest::badProvider
  2. public function badProvider(): array
  3. {
  4. return [
  5. [' '],
  6. ['not numeric'],
  7. [new stdClass()],
  8. [['foo', 'bar']],
  9. [[1, 2]],
  10. ['this is a string'],
  11. ['foo'],
  12. ['bar'],
  13. ['hello'],
  14. ['world'],
  15. [true],
  16. [1],
  17. [[[], []]],
  18. ];
  19. }

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

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