验证器.是否为时间

Testing Is Documentation

tests/Validate/Validator/DateFormatTest.php验证器.是否为时间 - 图1

Uses

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

验证通过的数据

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

  1. # Tests\Validate\Validator\DateFormatTest::baseUseProvider
  2. public function baseUseProvider(): array
  3. {
  4. return [
  5. ['6.1.2018 13:00+01:00', 'j.n.Y H:iP'],
  6. ['15-Mar-2018', 'j-M-Y'],
  7. ];
  8. }

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

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

未验证通过的数据

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

  1. # Tests\Validate\Validator\DateFormatTest::badProvider
  2. public function badProvider(): array
  3. {
  4. return [
  5. ['2018.6.1 13:00+01:00', 'j.n.Y H:iP'],
  6. ['29/Feb/23:2018:59:31', 'd/M/Y:H:i:s'],
  7. ];
  8. }

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

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

date_format 参数缺失

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