-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathDateTimeTest.php
More file actions
112 lines (103 loc) · 4.4 KB
/
Copy pathDateTimeTest.php
File metadata and controls
112 lines (103 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
declare(strict_types=1);
namespace Yiisoft\Validator\Tests\Rule\Date;
use DateTimeImmutable;
use DateTimeZone;
use Yiisoft\Validator\Rule\Date\DateTime;
use Yiisoft\Validator\Rule\Date\Date;
use Yiisoft\Validator\Rule\Date\DateTimeHandler;
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
final class DateTimeTest extends RuleTestCase
{
use SkipOnErrorTestTrait;
use WhenTestTrait;
public function testGetName(): void
{
$rule = new Date();
$this->assertSame('date', $rule->getName());
}
public static function dataValidationPassed(): array
{
return [
'php-format' => ['2021-01-01, 12:35', new DateTime(format: 'php:Y-m-d, H:i')],
'intl-format' => ['2021-01-01, 12:35', new DateTime(format: 'yyyy-MM-dd, HH:mm')],
'datetime' => [new DateTimeImmutable('2021-01-01, 12:35'), new DateTime()],
'min' => ['2021-01-01, 12:35', new DateTime(format: 'yyyy-MM-dd, HH:mm', min: '2020-01-01, 16:00')],
'max' => ['2021-01-01, 12:35', new DateTime(format: 'yyyy-MM-dd, HH:mm', max: '2022-01-01, 16:00')],
'min-equal' => ['2021-01-01, 12:35', new DateTime(format: 'yyyy-MM-dd, HH:mm', min: '2021-01-01, 12:35')],
'max-equal' => ['2021-01-01, 12:35', new DateTime(format: 'yyyy-MM-dd, HH:mm', max: '2021-01-01, 12:35')],
'timezone' => [
'12.11.2003, 15:00:00',
new DateTime(
format: 'php:d.m.Y, H:i:s',
timeZone: 'UTC',
min: new DateTimeImmutable('12.11.2003, 16:00:00', new DateTimeZone('GMT+3')),
),
],
'timestamp' => [1711705158, new DateTime(min: 1711705100)],
'rule-timezone-override-handler' => [
'12.11.2003, 15:00:00',
new DateTime(
format: 'php:d.m.Y, H:i:s',
timeZone: 'UTC',
min: new DateTimeImmutable('12.11.2003, 16:00:00', new DateTimeZone('GMT+3')),
),
[DateTimeHandler::class => new DateTimeHandler(timeZone: 'GMT+3')],
],
];
}
public static function dataValidationFailed(): array
{
$invalidDateMessage = ['' => ['Value must be a date.']];
return [
'php-format-invalid' => ['2021.01.01, 12:35', new DateTime(format: 'php:Y-m-d, H:i'), $invalidDateMessage],
'php-format-invalid-2' => [
'2021-17-35 16:60:97',
new DateTime(format: 'php:Y-m-d H:i:s'),
$invalidDateMessage,
],
'intl-format-invalid' => [
'2021.01.01, 12:35',
new DateTime(format: 'yyyy-MM-dd, HH:mm'),
$invalidDateMessage,
],
'invalid-date' => ['2021.02.12, 25:24', new DateTime(format: 'yyyy-MM-dd, HH:mm'), $invalidDateMessage],
'min' => [
'2024-03-29, 12:35',
new DateTime(format: 'yyyy-MM-dd, HH:mm', min: '2025-01-01, 11:00'),
['' => ['Value must be no earlier than 1/1/25, 11:00 AM.']],
],
'max' => [
'2024-03-29, 12:50',
new DateTime(format: 'php:Y-m-d, H:i', max: '2024-01-01, 00:00'),
['' => ['Value must be no later than 1/1/24, 12:00 AM.']],
],
'timestamp' => [
1711705158,
new DateTime(format: 'php:d.m.Y, H:i:s', min: 1711705200),
['' => ['Value must be no earlier than 3/29/24, 9:40 AM.']],
],
'without-message-date-and-time-type' => [
'29*03*2024*12*35',
new DateTime(format: 'php:d*m*Y*H*i', max: '11*11*2023*12*35'),
['' => ['Value must be no later than 11/11/23, 12:35 PM.']],
[DateTimeHandler::class => new DateTimeHandler(messageDateType: null, messageTimeType: null)],
],
];
}
public function testSkipOnError(): void
{
$this->testSkipOnErrorInternal(new Date(), new Date(skipOnError: true));
}
public function testWhen(): void
{
$this->testWhenInternal(
new Date(),
new Date(
when: static fn(mixed $value): bool => $value !== null,
),
);
}
}