验证器.值是否为电话号码

Testing Is Documentation

tests/Validate/Validator/TelephoneTest.php验证器.值是否为电话号码 - 图1

Uses

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

验证通过的数据

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

  1. # Tests\Validate\Validator\TelephoneTest::baseUseProvider
  2. public function baseUseProvider(): array
  3. {
  4. return [
  5. ['028-8301444'],
  6. ['0818-8301111'],
  7. ['0818-83011113'],
  8. ['08188301111'],
  9. ['081883011113'],
  10. ['0818-830111355'],
  11. ['1733332444'],
  12. ];
  13. }

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

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

未验证通过的数据

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

  1. # Tests\Validate\Validator\TelephoneTest::badProvider
  2. public function badProvider(): array
  3. {
  4. return [
  5. ['130222000333311'],
  6. ['1533333333332222'],
  7. ['181222100003333'],
  8. ['143311222333444'],
  9. ['17333322222444'],
  10. [' '],
  11. [new stdClass()],
  12. [['foo', 'bar']],
  13. [[1, 2]],
  14. [true],
  15. [[[], []]],
  16. ['02228-8301444'],
  17. ['08128-8301111'],
  18. ['173111223332444'],
  19. ];
  20. }

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

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