-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathDateTest.php
More file actions
205 lines (190 loc) · 8.06 KB
/
Copy pathDateTest.php
File metadata and controls
205 lines (190 loc) · 8.06 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
declare(strict_types=1);
namespace Yiisoft\Validator\Tests\Rule\Date;
use DateTimeImmutable;
use DateTimeZone;
use IntlDateFormatter;
use LogicException;
use stdClass;
use Yiisoft\Validator\Exception\UnexpectedRuleException;
use Yiisoft\Validator\Rule\Date\Date;
use Yiisoft\Validator\Rule\Date\DateHandler;
use Yiisoft\Validator\Rule\Date\DateTime;
use Yiisoft\Validator\Rule\Date\Time;
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
use Yiisoft\Validator\Tests\Support\Rule\RuleWithCustomHandler;
use Yiisoft\Validator\Validator;
final class DateTest 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', new Date(format: 'php:Y-m-d')],
'intl-format' => ['2021-01-01', new Date(format: 'yyyy-MM-dd')],
'datetime' => [new DateTimeImmutable('2021-01-01'), new Date()],
'min' => ['2021-01-01', new Date(format: 'yyyy-MM-dd', min: '2020-01-01')],
'max' => ['2021-01-01', new Date(format: 'yyyy-MM-dd', max: '2022-01-01')],
'min-equal' => ['2021-01-01', new Date(format: 'yyyy-MM-dd', min: '2021-01-01')],
'max-equal' => ['2021-01-01', new Date(format: 'yyyy-MM-dd', max: '2021-01-01')],
'timezone' => [
'12.11.2003',
new Date(
format: 'php:d.m.Y',
timeZone: 'UTC',
min: new DateTimeImmutable('12.11.2003, 1:00:00', new DateTimeZone('GMT+3')),
),
],
'timestamp' => [1711705158, new Date(min: 1711705100)],
'zero-time' => [
'2021-01-01',
new Date(format: 'php:Y-m-d', max: new DateTimeImmutable('2021-01-01, 00:00:00')),
],
'rule-locale' => [
'29.03.2024',
new Date(locale: 'ru'),
],
];
}
public static function dataValidationFailed(): array
{
$invalidDateMessage = ['' => ['Value must be a date.']];
return [
'php-format-invalid' => ['2021.01.01', new Date(format: 'php:Y-m-d'), $invalidDateMessage],
'intl-format-invalid' => ['2021.01.01', new Date(format: 'yyyy-MM-dd'), $invalidDateMessage],
'invalid-date' => ['2021.02.30', new Date(format: 'yyyy-MM-dd'), $invalidDateMessage],
'invalid-value' => [new stdClass(), new Date(), $invalidDateMessage],
'invalid-value-custom-message' => [
['a' => new stdClass()],
['a' => new Date(incorrectInputMessage: 'Invalid — {property}.')],
['a' => ['Invalid — a.']],
],
'min' => [
'2024-03-29',
new Date(format: 'yyyy-MM-dd', min: '2025-01-01'),
['' => ['Value must be no earlier than 1/1/25.']],
],
'min-custom-message' => [
['a' => '2024-03-29'],
[
'a' => new Date(
format: 'php:Y-m-d',
min: '2025-01-01',
tooEarlyMessage: 'Prop — {property}. Date — {value}. Min — {limit}.',
),
],
['a' => ['Prop — a. Date — 3/29/24. Min — 1/1/25.']],
],
'max' => [
'2024-03-29',
new Date(format: 'php:Y-m-d', max: '2024-01-01'),
['' => ['Value must be no later than 1/1/24.']],
],
'max-custom-message' => [
['a' => '2024-03-29'],
[
'a' => new Date(
format: 'php:Y-m-d',
max: '2024-01-01',
tooLateMessage: 'Prop — {property}. Date — {value}. Max — {limit}.',
),
],
['a' => ['Prop — a. Date — 3/29/24. Max — 1/1/24.']],
],
'rule-and-handler-locales' => [
'2024-03-29',
new Date(format: 'php:Y-m-d', locale: 'ru', max: '2024-01-01'),
['' => ['Value must be no later than 01.01.2024.']],
[DateHandler::class => new DateHandler(locale: 'en')],
],
'handler-locale' => [
'2024-03-29',
new Date(format: 'php:Y-m-d', max: '2024-01-01'),
['' => ['Value must be no later than 01.01.2024.']],
[DateHandler::class => new DateHandler(locale: 'ru')],
],
'timestamp' => [
1711705158,
new Date(min: 1711705200),
['' => ['Value must be no earlier than 3/29/24.']],
],
'without-message-date-type' => [
'29*03*2024',
new Date(format: 'php:d*m*Y', max: '11*11*2023', ),
['' => ['Value must be no later than 11/11/23.']],
[DateHandler::class => new DateHandler(messageDateType: null)],
],
'rule-message-format' => [
'29*03*2024',
new Date(format: 'php:d*m*Y', max: '11*11*2023', messageFormat: 'php:d=m=Y'),
['' => ['Value must be no later than 11=11=2023.']],
[DateHandler::class => new DateHandler(messageFormat: 'php:d_m_Y')],
],
'handler-message-type' => [
'Mar 29, 2024',
new Date(max: 'Dec 11, 2019', dateType: IntlDateFormatter::MEDIUM),
['' => ['Value must be no later than Wednesday, December 11, 2019.']],
[DateHandler::class => new DateHandler(messageDateType: IntlDateFormatter::FULL)],
],
'rule-message-type-override-handler' => [
'3/29/2024',
new Date(max: '12/11/2019', messageDateType: IntlDateFormatter::SHORT),
['' => ['Value must be no later than 12/11/19.']],
[DateHandler::class => new DateHandler(messageDateType: IntlDateFormatter::FULL)],
],
'rule-locale-override-handler' => [
'12.11.2002',
new Date(max: '10.11.2002', locale: 'ru'),
['' => ['Value must be no later than 10.11.2002.']],
[DateHandler::class => new DateHandler(locale: 'en')],
],
];
}
public function testDifferentRuleInHandlerItems(): array
{
$rule = new RuleWithCustomHandler(DateHandler::class);
$validator = new Validator();
$this->expectException(UnexpectedRuleException::class);
$this->expectExceptionMessage(
'Expected "' . Date::class . '", "' . DateTime::class . '", "' . Time::class . '", but "' . RuleWithCustomHandler::class . '" given.',
);
$validator->validate([], $rule);
}
public function testInvalidMinValue(): void
{
$rule = new Date(format: 'php:Y-m-d', min: '12.11.2023');
$validator = new Validator();
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Invalid date value.');
$validator->validate('2024-11-01', $rule);
}
public function testInvalidMaxValue(): void
{
$rule = new Date(format: 'php:Y-m-d', max: '12.11.2023');
$validator = new Validator();
$this->expectException(LogicException::class);
$this->expectExceptionMessage('Invalid date value.');
$validator->validate('2024-11-01', $rule);
}
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,
),
);
}
}