验证器.值是否为电话号码或者手机号码

Testing Is Documentation

tests/Validate/Validator/PhoneTest.php验证器.值是否为电话号码或者手机号码 - 图1

Uses

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

验证通过的数据

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

  1. # Tests\Validate\Validator\PhoneTest::baseUseProvider
  2. public function baseUseProvider(): array
  3. {
  4. return [
  5. [13000003333],
  6. [15323332222],
  7. ['13000003333'],
  8. ['15033332222'],
  9. ['18600003333'],
  10. ['14533333444'],
  11. ['17363332444'],
  12. ['17633332444'],
  13. ['028-8301444'],
  14. ['0818-8301111'],
  15. ['0818-83011113'],
  16. ['08188301111'],
  17. ['081883011113'],
  18. ['0818-830111355'],
  19. ['1733332444'],
  20. ];
  21. }

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

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

未验证通过的数据

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

  1. # Tests\Validate\Validator\PhoneTest::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' => 'phone',
  9. ]
  10. );
  11. $this->assertFalse($validate->success());
  12. }