|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Tests\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use DateTimeInterface; |
|
8
|
|
|
use DateTimeImmutable; |
|
9
|
|
|
use Yiisoft\Validator\Rule\DateTime; |
|
10
|
|
|
use Yiisoft\Validator\Rule\DateTimeHandler; |
|
11
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase; |
|
12
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait; |
|
13
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait; |
|
14
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait; |
|
15
|
|
|
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait; |
|
16
|
|
|
|
|
17
|
|
|
final class DateTimeTest extends RuleTestCase |
|
18
|
|
|
{ |
|
19
|
|
|
use DifferentRuleInHandlerTestTrait; |
|
20
|
|
|
use RuleWithOptionsTestTrait; |
|
21
|
|
|
use SkipOnErrorTestTrait; |
|
22
|
|
|
use WhenTestTrait; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
public function dataValidationPassed(): array |
|
26
|
|
|
{ |
|
27
|
|
|
return [ |
|
28
|
|
|
['2024-08-15T15:52:01+00:00', [new DateTime(formats: DateTimeInterface::W3C)]], |
|
29
|
|
|
['2024-08-15 15:52:01', [new DateTime(formats: 'Y-m-d H:i:s')]], |
|
30
|
|
|
['15-08-2024 15:52', [new DateTime(formats: 'd-m-Y H:i')]], |
|
31
|
|
|
[ |
|
32
|
|
|
'2024-08-15 15:52:01', |
|
33
|
|
|
[ |
|
34
|
|
|
new DateTime( |
|
35
|
|
|
min: null, |
|
36
|
|
|
max: null, |
|
37
|
|
|
message: 'The value must be between 2024-08-15 15:52:01 and 2024-08-15 15:52:01.', |
|
38
|
|
|
lessThanMinMessage: 'The value must be no less than 2024-08-15 15:52:01.', |
|
39
|
|
|
greaterThanMaxMessage: 'The value must be no greater than 2024-08-15 15:52:01.', |
|
40
|
|
|
skipOnEmpty: false, |
|
41
|
|
|
skipOnError: false, |
|
42
|
|
|
when: null, |
|
43
|
|
|
formats: 'Y-m-d H:i:s', format: DateTimeInterface::W3C, |
|
44
|
|
|
), |
|
45
|
|
|
], |
|
46
|
|
|
], |
|
47
|
|
|
[ |
|
48
|
|
|
'2024-08-15 15:52:01', |
|
49
|
|
|
[ |
|
50
|
|
|
new DateTime( |
|
51
|
|
|
min: new DateTimeImmutable('2024-08-15 15:52:01'), |
|
52
|
|
|
formats: 'Y-m-d H:i:s' |
|
53
|
|
|
), |
|
54
|
|
|
], |
|
55
|
|
|
], |
|
56
|
|
|
[ |
|
57
|
|
|
new DateTimeImmutable('2024-08-15 15:52:01'), |
|
58
|
|
|
[new DateTime(formats: 'Y-m-d H:i:s')], |
|
59
|
|
|
], |
|
60
|
|
|
[ |
|
61
|
|
|
'2024-08-15 15:51:59', |
|
62
|
|
|
[ |
|
63
|
|
|
new DateTime( |
|
64
|
|
|
max: new DateTimeImmutable('2024-08-15 15:52:01'), |
|
65
|
|
|
formats: 'Y-m-d H:i:s' |
|
66
|
|
|
), |
|
67
|
|
|
], |
|
68
|
|
|
], |
|
69
|
|
|
[ |
|
70
|
|
|
'2024-08-15 15:52:01', |
|
71
|
|
|
[ |
|
72
|
|
|
new DateTime( |
|
73
|
|
|
max: new DateTimeImmutable('2024-08-15 15:52:01'), |
|
74
|
|
|
formats: 'Y-m-d H:i:s' |
|
75
|
|
|
), |
|
76
|
|
|
], |
|
77
|
|
|
['' => [' must be no greater than 2024-08-15 15:52:01.']], |
|
78
|
|
|
], |
|
79
|
|
|
|
|
80
|
|
|
[ |
|
81
|
|
|
1705322898, |
|
82
|
|
|
[new DateTime()], |
|
83
|
|
|
['' => [' value is not a valid DateTime.']], |
|
84
|
|
|
], |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function dataValidationFailed(): array |
|
89
|
|
|
{ |
|
90
|
|
|
return [ |
|
91
|
|
|
|
|
92
|
|
|
[ |
|
93
|
|
|
'2023-02-20ee', |
|
94
|
|
|
[new DateTime(message: '{attribute} value is not a valid DateTime.')], |
|
95
|
|
|
['' => [' value is not a valid DateTime.']], |
|
96
|
|
|
], |
|
97
|
|
|
[ |
|
98
|
|
|
'', |
|
99
|
|
|
[new DateTime(message: '{attribute} value is not a valid DateTime.')], |
|
100
|
|
|
['' => [' value is not a valid DateTime.']], |
|
101
|
|
|
], |
|
102
|
|
|
[ |
|
103
|
|
|
'2024-08-14 15:52:01', |
|
104
|
|
|
[ |
|
105
|
|
|
new DateTime( |
|
106
|
|
|
min: new DateTimeImmutable('2024-08-15 15:52:01'), |
|
107
|
|
|
formats: 'Y-m-d H:i:s' |
|
108
|
|
|
), |
|
109
|
|
|
], |
|
110
|
|
|
['' => [' must be no less than 2024-08-15 15:52:01.']], |
|
111
|
|
|
], |
|
112
|
|
|
[ |
|
113
|
|
|
'2024-08-15 15:52:02', |
|
114
|
|
|
[ |
|
115
|
|
|
new DateTime( |
|
116
|
|
|
max: new DateTimeImmutable('2024-08-15 15:52:01'), |
|
117
|
|
|
formats: 'Y-m-d H:i:s' |
|
118
|
|
|
), |
|
119
|
|
|
], |
|
120
|
|
|
['' => [' must be no greater than 2024-08-15 15:52:01.']], |
|
121
|
|
|
], |
|
122
|
|
|
]; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function dataOptions(): array |
|
126
|
|
|
{ |
|
127
|
|
|
return [ |
|
128
|
|
|
[ |
|
129
|
|
|
new DateTime(), |
|
130
|
|
|
[ |
|
131
|
|
|
'formats' => [], |
|
132
|
|
|
'min' => null, |
|
133
|
|
|
'max' => null, |
|
134
|
|
|
'lessThanMinMessage' => [ |
|
135
|
|
|
'template' => '{attribute} must be no less than {min}.', |
|
136
|
|
|
'parameters' => [], |
|
137
|
|
|
], |
|
138
|
|
|
'greaterThanMaxMessage' => [ |
|
139
|
|
|
'template' => '{attribute} must be no greater than {max}.', |
|
140
|
|
|
'parameters' => [], |
|
141
|
|
|
], |
|
142
|
|
|
'message' => [ |
|
143
|
|
|
'template' => '{attribute} value is not a valid DateTime.', |
|
144
|
|
|
'parameters' => [], |
|
145
|
|
|
], |
|
146
|
|
|
'skipOnEmpty' => false, |
|
147
|
|
|
'skipOnError' => false, |
|
148
|
|
|
], |
|
149
|
|
|
], |
|
150
|
|
|
]; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function testSkipOnError(): void |
|
154
|
|
|
{ |
|
155
|
|
|
$this->testSkipOnErrorInternal(new DateTime(), new DateTime(skipOnError: true)); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function testWhen(): void |
|
159
|
|
|
{ |
|
160
|
|
|
$when = static fn(mixed $value): bool => $value !== null; |
|
161
|
|
|
$this->testWhenInternal(new DateTime(), new DateTime(when: $when)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
protected function getDifferentRuleInHandlerItems(): array |
|
165
|
|
|
{ |
|
166
|
|
|
return [DateTime::class, DateTimeHandler::class]; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function testGetName(): void |
|
170
|
|
|
{ |
|
171
|
|
|
$rule = new DateTime(); |
|
172
|
|
|
$this->assertSame(DateTime::class, $rule->getName()); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|