验证器.验证在给定日期之前

Testing Is Documentation

tests/Validate/Validator/BeforeTest.php验证器.验证在给定日期之前 - 图1

Uses

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

验证通过的数据

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

  1. # Tests\Validate\Validator\BeforeTest::baseUseProvider
  2. public function baseUseProvider(): array
  3. {
  4. return [
  5. ['2018-08-11', '2018-08-14'],
  6. ['2018-08-09', 'name2'],
  7. ['2018-08-13', '2018-08-14|date_format:Y-m-d'],
  8. ];
  9. }

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

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

未验证通过的数据

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

  1. # Tests\Validate\Validator\BeforeTest::badProvider
  2. public function badProvider(): array
  3. {
  4. return [
  5. ['2018-08-17', '2018-08-17'],
  6. ['2018-08-17', '2018-08-15'],
  7. ['2018-08-15', 'name2'],
  8. ['2018-08-15', '2018-08-14|date_format:Y-m'],
  9. [new stdClass(), '1.1'],
  10. [[], '2018-08-15'],
  11. [true, '2018-08-15'],
  12. [false, '2018-08-15'],
  13. ];
  14. }

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

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

日期格式化不一致无法通过验证

  1. public function testMakeDateTimeFormatWithNewDateTimeExceptionError(): void
  2. {
  3. $validate = new Validator(
  4. [
  5. 'name' => '2018-08-10',
  6. ],
  7. [
  8. 'name' => 'before:foobar|date_format:y',
  9. ]
  10. );
  11. $this->assertFalse($validate->success());
  12. }

before 参数缺失

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