6
6
7
7
use MartinGeorgiev \Doctrine \DBAL \Types \Exceptions \InvalidRangeForPHPException ;
8
8
use MartinGeorgiev \Doctrine \DBAL \Types \ValueObject \DateRange ;
9
+ use MartinGeorgiev \Doctrine \DBAL \Types \ValueObject \Range ;
9
10
use PHPUnit \Framework \Attributes \DataProvider ;
10
11
use PHPUnit \Framework \Attributes \Test ;
11
- use PHPUnit \Framework \TestCase ;
12
12
13
- final class DateRangeTest extends TestCase
13
+ final class DateRangeTest extends BaseRangeTestCase
14
14
{
15
- #[Test]
16
- public function can_create_simple_range (): void
15
+ protected function createSimpleRange (): Range
17
16
{
18
- $ start = new \DateTimeImmutable ('2023-01-01 ' );
19
- $ end = new \DateTimeImmutable ('2023-12-31 ' );
20
- $ dateRange = new DateRange ($ start , $ end );
17
+ return new DateRange (
18
+ new \DateTimeImmutable ('2023-01-01 ' ),
19
+ new \DateTimeImmutable ('2023-12-31 ' )
20
+ );
21
+ }
21
22
22
- self ::assertEquals ('[2023-01-01,2023-12-31) ' , (string ) $ dateRange );
23
- self ::assertFalse ($ dateRange ->isEmpty ());
23
+ protected function getExpectedSimpleRangeString (): string
24
+ {
25
+ return '[2023-01-01,2023-12-31) ' ;
24
26
}
25
27
26
- #[Test]
27
- public function can_create_empty_range (): void
28
+ protected function createEmptyRange (): Range
28
29
{
29
- $ dateRange = DateRange::empty ();
30
+ return DateRange::empty ();
31
+ }
30
32
31
- self ::assertEquals ('empty ' , (string ) $ dateRange );
32
- self ::assertTrue ($ dateRange ->isEmpty ());
33
+ protected function createInfiniteRange (): Range
34
+ {
35
+ return DateRange::infinite ();
33
36
}
34
37
35
- #[Test]
36
- public function can_create_infinite_range (): void
38
+ protected function createInclusiveRange (): Range
37
39
{
38
- $ dateRange = DateRange::infinite ();
40
+ return new DateRange (
41
+ new \DateTimeImmutable ('2023-01-01 ' ),
42
+ new \DateTimeImmutable ('2023-12-31 ' ),
43
+ true ,
44
+ true
45
+ );
46
+ }
39
47
40
- self ::assertEquals ('(,) ' , (string ) $ dateRange );
41
- self ::assertFalse ($ dateRange ->isEmpty ());
48
+ protected function getExpectedInclusiveRangeString (): string
49
+ {
50
+ return '[2023-01-01,2023-12-31] ' ;
51
+ }
52
+
53
+ protected function parseFromString (string $ input ): Range
54
+ {
55
+ return DateRange::fromString ($ input );
56
+ }
57
+
58
+ protected function createBoundaryTestRange (): Range
59
+ {
60
+ return new DateRange (
61
+ new \DateTimeImmutable ('2023-01-01 ' ),
62
+ new \DateTimeImmutable ('2023-01-10 ' ),
63
+ true ,
64
+ false
65
+ );
66
+ }
67
+
68
+ protected function getBoundaryTestCases (): array
69
+ {
70
+ return [
71
+ 'contains lower bound (inclusive) ' => [
72
+ 'value ' => new \DateTimeImmutable ('2023-01-01 ' ),
73
+ 'expected ' => true ,
74
+ ],
75
+ 'does not contain value before range ' => [
76
+ 'value ' => new \DateTimeImmutable ('2022-12-31 ' ),
77
+ 'expected ' => false ,
78
+ ],
79
+ 'does not contain upper bound (exclusive) ' => [
80
+ 'value ' => new \DateTimeImmutable ('2023-01-10 ' ),
81
+ 'expected ' => false ,
82
+ ],
83
+ 'contains value just before upper ' => [
84
+ 'value ' => new \DateTimeImmutable ('2023-01-09 ' ),
85
+ 'expected ' => true ,
86
+ ],
87
+ 'does not contain value after range ' => [
88
+ 'value ' => new \DateTimeImmutable ('2023-01-11 ' ),
89
+ 'expected ' => false ,
90
+ ],
91
+ 'contains middle value ' => [
92
+ 'value ' => new \DateTimeImmutable ('2023-01-05 ' ),
93
+ 'expected ' => true ,
94
+ ],
95
+ ];
96
+ }
97
+
98
+ protected function getComparisonTestCases (): array
99
+ {
100
+ return [
101
+ 'reverse range should be empty ' => [
102
+ 'range ' => new DateRange (
103
+ new \DateTimeImmutable ('2023-12-31 ' ),
104
+ new \DateTimeImmutable ('2023-01-01 ' )
105
+ ),
106
+ 'expectedEmpty ' => true ,
107
+ ],
108
+ 'normal range should not be empty ' => [
109
+ 'range ' => new DateRange (
110
+ new \DateTimeImmutable ('2023-01-01 ' ),
111
+ new \DateTimeImmutable ('2023-12-31 ' )
112
+ ),
113
+ 'expectedEmpty ' => false ,
114
+ ],
115
+ 'equal bounds exclusive should be empty ' => [
116
+ 'range ' => new DateRange (
117
+ new \DateTimeImmutable ('2023-06-15 ' ),
118
+ new \DateTimeImmutable ('2023-06-15 ' ),
119
+ false ,
120
+ false
121
+ ),
122
+ 'expectedEmpty ' => true ,
123
+ ],
124
+ 'equal bounds inclusive should not be empty ' => [
125
+ 'range ' => new DateRange (
126
+ new \DateTimeImmutable ('2023-06-15 ' ),
127
+ new \DateTimeImmutable ('2023-06-15 ' ),
128
+ true ,
129
+ true
130
+ ),
131
+ 'expectedEmpty ' => false ,
132
+ ],
133
+ ];
42
134
}
43
135
44
136
#[Test]
@@ -69,68 +161,58 @@ public function can_create_month_range(): void
69
161
self ::assertFalse ($ dateRange ->isEmpty ());
70
162
}
71
163
72
- #[Test]
73
- #[DataProvider('providesContainsTestCases ' )]
74
- public function can_check_contains (DateRange $ dateRange , mixed $ value , bool $ expected ): void
75
- {
76
- self ::assertEquals ($ expected , $ dateRange ->contains ($ value ));
77
- }
78
-
79
- #[Test]
80
- #[DataProvider('providesFromStringTestCases ' )]
81
- public function can_parse_from_string (string $ input , DateRange $ dateRange ): void
82
- {
83
- $ result = DateRange::fromString ($ input );
84
-
85
- self ::assertEquals ($ dateRange ->__toString (), $ result ->__toString ());
86
- self ::assertEquals ($ dateRange ->isEmpty (), $ result ->isEmpty ());
87
- }
88
-
89
- #[Test]
90
- public function throws_exception_for_invalid_lower_bound (): void
91
- {
92
- $ this ->expectException (\InvalidArgumentException::class);
93
- $ this ->expectExceptionMessage ('Lower bound must be DateTimeInterface ' );
94
-
95
- new DateRange ('invalid ' , new \DateTimeImmutable ('2023-12-31 ' ));
96
- }
97
-
98
- #[Test]
99
- public function throws_exception_for_invalid_upper_bound (): void
100
- {
101
- $ this ->expectException (\InvalidArgumentException::class);
102
- $ this ->expectExceptionMessage ('Upper bound must be DateTimeInterface ' );
103
-
104
- new DateRange (new \DateTimeImmutable ('2023-01-01 ' ), 'invalid ' );
105
- }
106
-
107
- public static function providesContainsTestCases (): \Generator
164
+ public static function provideContainsTestCases (): \Generator
108
165
{
109
166
$ dateRange = new DateRange (
110
167
new \DateTimeImmutable ('2023-01-01 ' ),
111
168
new \DateTimeImmutable ('2023-12-31 ' )
112
169
);
113
170
114
- yield 'contains date in range ' => [$ dateRange , new \DateTimeImmutable ('2023-06-15 ' ), true ];
115
- yield 'contains lower bound (inclusive) ' => [$ dateRange , new \DateTimeImmutable ('2023-01-01 ' ), true ];
116
- yield 'does not contain upper bound (exclusive) ' => [$ dateRange , new \DateTimeImmutable ('2023-12-31 ' ), false ];
117
- yield 'does not contain date before range ' => [$ dateRange , new \DateTimeImmutable ('2022-12-31 ' ), false ];
118
- yield 'does not contain date after range ' => [$ dateRange , new \DateTimeImmutable ('2024-01-01 ' ), false ];
171
+ yield 'contains date in range ' => [
172
+ $ dateRange ,
173
+ new \DateTimeImmutable ('2023-06-15 ' ),
174
+ true ,
175
+ ];
176
+ yield 'contains lower bound (inclusive) ' => [
177
+ $ dateRange ,
178
+ new \DateTimeImmutable ('2023-01-01 ' ),
179
+ true ,
180
+ ];
181
+ yield 'does not contain upper bound (exclusive) ' => [
182
+ $ dateRange ,
183
+ new \DateTimeImmutable ('2023-12-31 ' ),
184
+ false ,
185
+ ];
186
+ yield 'does not contain date before range ' => [
187
+ $ dateRange ,
188
+ new \DateTimeImmutable ('2022-12-31 ' ),
189
+ false ,
190
+ ];
191
+ yield 'does not contain date after range ' => [
192
+ $ dateRange ,
193
+ new \DateTimeImmutable ('2024-01-01 ' ),
194
+ false ,
195
+ ];
119
196
yield 'does not contain null ' => [$ dateRange , null , false ];
120
-
121
- $ emptyRange = DateRange::empty ();
122
- yield 'empty range contains nothing ' => [$ emptyRange , new \DateTimeImmutable ('2023-06-15 ' ), false ];
123
197
}
124
198
125
- public static function providesFromStringTestCases (): \Generator
199
+ public static function provideFromStringTestCases (): \Generator
126
200
{
127
- yield 'simple range ' => [
201
+ yield 'simple date range ' => [
128
202
'[2023-01-01,2023-12-31) ' ,
129
- new DateRange (new \DateTimeImmutable ('2023-01-01 ' ), new \DateTimeImmutable ('2023-12-31 ' )),
203
+ new DateRange (
204
+ new \DateTimeImmutable ('2023-01-01 ' ),
205
+ new \DateTimeImmutable ('2023-12-31 ' )
206
+ ),
130
207
];
131
- yield 'inclusive range ' => [
208
+ yield 'inclusive date range ' => [
132
209
'[2023-01-01,2023-12-31] ' ,
133
- new DateRange (new \DateTimeImmutable ('2023-01-01 ' ), new \DateTimeImmutable ('2023-12-31 ' ), true , true ),
210
+ new DateRange (
211
+ new \DateTimeImmutable ('2023-01-01 ' ),
212
+ new \DateTimeImmutable ('2023-12-31 ' ),
213
+ true ,
214
+ true
215
+ ),
134
216
];
135
217
yield 'infinite lower ' => [
136
218
'[,2023-12-31) ' ,
@@ -143,6 +225,24 @@ public static function providesFromStringTestCases(): \Generator
143
225
yield 'empty range ' => ['empty ' , DateRange::empty ()];
144
226
}
145
227
228
+ #[Test]
229
+ public function throws_exception_for_invalid_lower_bound (): void
230
+ {
231
+ $ this ->expectException (\InvalidArgumentException::class);
232
+ $ this ->expectExceptionMessage ('Lower bound must be DateTimeInterface ' );
233
+
234
+ new DateRange ('invalid ' , new \DateTimeImmutable ('2023-12-31 ' ));
235
+ }
236
+
237
+ #[Test]
238
+ public function throws_exception_for_invalid_upper_bound (): void
239
+ {
240
+ $ this ->expectException (\InvalidArgumentException::class);
241
+ $ this ->expectExceptionMessage ('Upper bound must be DateTimeInterface ' );
242
+
243
+ new DateRange (new \DateTimeImmutable ('2023-01-01 ' ), 'invalid ' );
244
+ }
245
+
146
246
#[Test]
147
247
public function throws_exception_for_invalid_lower_bound_type (): void
148
248
{
@@ -178,7 +278,8 @@ public function throws_exception_for_invalid_datetime_in_comparison_via_contains
178
278
#[Test]
179
279
public function throws_exception_for_invalid_value_in_constructor (): void
180
280
{
181
- $ this ->expectException (\TypeError::class);
281
+ $ this ->expectException (\InvalidArgumentException::class);
282
+ $ this ->expectExceptionMessage ('Lower bound must be DateTimeInterface ' );
182
283
183
284
new DateRange ('invalid ' , new \DateTimeImmutable ('2023-12-31 ' ));
184
285
}
@@ -187,7 +288,7 @@ public function throws_exception_for_invalid_value_in_constructor(): void
187
288
public function throws_exception_for_invalid_date_string_in_parse_via_from_string (): void
188
289
{
189
290
$ this ->expectException (\InvalidArgumentException::class);
190
- $ this ->expectExceptionMessage ('Invalid range format ' );
291
+ $ this ->expectExceptionMessage ('Invalid date value ' );
191
292
192
293
DateRange::fromString ('[invalid_date,2023-12-31) ' );
193
294
}
0 commit comments