66
77use MartinGeorgiev \Doctrine \DBAL \Types \Exceptions \InvalidRangeForPHPException ;
88use MartinGeorgiev \Doctrine \DBAL \Types \ValueObject \DateRange ;
9+ use MartinGeorgiev \Doctrine \DBAL \Types \ValueObject \Range ;
910use PHPUnit \Framework \Attributes \DataProvider ;
1011use PHPUnit \Framework \Attributes \Test ;
11- use PHPUnit \Framework \TestCase ;
1212
13- final class DateRangeTest extends TestCase
13+ final class DateRangeTest extends BaseRangeTestCase
1414{
15- #[Test]
16- public function can_create_simple_range (): void
15+ protected function createSimpleRange (): Range
1716 {
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+ }
2122
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) ' ;
2426 }
2527
26- #[Test]
27- public function can_create_empty_range (): void
28+ protected function createEmptyRange (): Range
2829 {
29- $ dateRange = DateRange::empty ();
30+ return DateRange::empty ();
31+ }
3032
31- self ::assertEquals ('empty ' , (string ) $ dateRange );
32- self ::assertTrue ($ dateRange ->isEmpty ());
33+ protected function createInfiniteRange (): Range
34+ {
35+ return DateRange::infinite ();
3336 }
3437
35- #[Test]
36- public function can_create_infinite_range (): void
38+ protected function createInclusiveRange (): Range
3739 {
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+ }
3947
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+ ];
42134 }
43135
44136 #[Test]
@@ -69,68 +161,58 @@ public function can_create_month_range(): void
69161 self ::assertFalse ($ dateRange ->isEmpty ());
70162 }
71163
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
108165 {
109166 $ dateRange = new DateRange (
110167 new \DateTimeImmutable ('2023-01-01 ' ),
111168 new \DateTimeImmutable ('2023-12-31 ' )
112169 );
113170
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+ ];
119196 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 ];
123197 }
124198
125- public static function providesFromStringTestCases (): \Generator
199+ public static function provideFromStringTestCases (): \Generator
126200 {
127- yield 'simple range ' => [
201+ yield 'simple date range ' => [
128202 '[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+ ),
130207 ];
131- yield 'inclusive range ' => [
208+ yield 'inclusive date range ' => [
132209 '[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+ ),
134216 ];
135217 yield 'infinite lower ' => [
136218 '[,2023-12-31) ' ,
@@ -143,6 +225,24 @@ public static function providesFromStringTestCases(): \Generator
143225 yield 'empty range ' => ['empty ' , DateRange::empty ()];
144226 }
145227
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+
146246 #[Test]
147247 public function throws_exception_for_invalid_lower_bound_type (): void
148248 {
@@ -178,7 +278,8 @@ public function throws_exception_for_invalid_datetime_in_comparison_via_contains
178278 #[Test]
179279 public function throws_exception_for_invalid_value_in_constructor (): void
180280 {
181- $ this ->expectException (\TypeError::class);
281+ $ this ->expectException (\InvalidArgumentException::class);
282+ $ this ->expectExceptionMessage ('Lower bound must be DateTimeInterface ' );
182283
183284 new DateRange ('invalid ' , new \DateTimeImmutable ('2023-12-31 ' ));
184285 }
@@ -187,7 +288,7 @@ public function throws_exception_for_invalid_value_in_constructor(): void
187288 public function throws_exception_for_invalid_date_string_in_parse_via_from_string (): void
188289 {
189290 $ this ->expectException (\InvalidArgumentException::class);
190- $ this ->expectExceptionMessage ('Invalid range format ' );
291+ $ this ->expectExceptionMessage ('Invalid date value ' );
191292
192293 DateRange::fromString ('[invalid_date,2023-12-31) ' );
193294 }
0 commit comments