9
9
use PHPUnit \Framework \Attributes \Test ;
10
10
use PHPUnit \Framework \TestCase ;
11
11
12
+ /**
13
+ * @template R of int|float|\DateTimeInterface
14
+ *
15
+ * TODO: Remove PHPStan suppressions when covariant generics are supported
16
+ *
17
+ * @see https://github.com/phpstan/phpstan/issues/7427
18
+ */
12
19
abstract class BaseRangeTestCase extends TestCase
13
20
{
14
21
#[Test]
@@ -49,13 +56,19 @@ public function can_create_inclusive_range(): void
49
56
self ::assertFalse ($ range ->isEmpty ());
50
57
}
51
58
59
+ /**
60
+ * @param Range<R> $range
61
+ */
52
62
#[Test]
53
63
#[DataProvider('provideContainsTestCases ' )]
54
64
public function can_check_contains (Range $ range , mixed $ value , bool $ expected ): void
55
65
{
56
66
self ::assertEquals ($ expected , $ range ->contains ($ value ));
57
67
}
58
68
69
+ /**
70
+ * @param Range<R> $expectedRange
71
+ */
59
72
#[Test]
60
73
#[DataProvider('provideFromStringTestCases ' )]
61
74
public function can_parse_from_string (string $ input , Range $ expectedRange ): void
@@ -89,6 +102,8 @@ public function can_handle_comparison_via_is_empty(): void
89
102
90
103
/**
91
104
* Create a simple range for basic testing.
105
+ *
106
+ * @return Range<R>
92
107
*/
93
108
abstract protected function createSimpleRange (): Range ;
94
109
@@ -99,16 +114,22 @@ abstract protected function getExpectedSimpleRangeString(): string;
99
114
100
115
/**
101
116
* Create an empty range.
117
+ *
118
+ * @return Range<R>
102
119
*/
103
120
abstract protected function createEmptyRange (): Range ;
104
121
105
122
/**
106
123
* Create an infinite range.
124
+ *
125
+ * @return Range<R>
107
126
*/
108
127
abstract protected function createInfiniteRange (): Range ;
109
128
110
129
/**
111
130
* Create an inclusive range for testing.
131
+ *
132
+ * @return Range<R>
112
133
*/
113
134
abstract protected function createInclusiveRange (): Range ;
114
135
@@ -119,11 +140,15 @@ abstract protected function getExpectedInclusiveRangeString(): string;
119
140
120
141
/**
121
142
* Parse range from string.
143
+ *
144
+ * @return Range<R>
122
145
*/
123
146
abstract protected function parseFromString (string $ input ): Range ;
124
147
125
148
/**
126
149
* Create range for boundary testing.
150
+ *
151
+ * @return Range<R>
127
152
*/
128
153
abstract protected function createBoundaryTestRange (): Range ;
129
154
@@ -137,22 +162,25 @@ abstract protected function getBoundaryTestCases(): array;
137
162
/**
138
163
* Get comparison test cases.
139
164
*
140
- * @return array<string, array{range: Range, expectedEmpty: bool}>
165
+ * @return array<string, array{range: Range<R> , expectedEmpty: bool}>
141
166
*/
142
167
abstract protected function getComparisonTestCases (): array ;
143
168
144
169
/**
145
- * @return \Generator<string, array{Range, mixed, bool}>
170
+ * @return \Generator<string, array{Range<R> , mixed, bool}>
146
171
*/
147
172
abstract public static function provideContainsTestCases (): \Generator ;
148
173
149
174
/**
150
- * @return \Generator<string, array{string, Range}>
175
+ * @return \Generator<string, array{string, Range<R> }>
151
176
*/
152
177
abstract public static function provideFromStringTestCases (): \Generator ;
153
178
154
179
/**
155
180
* Assert that a range equals another range by comparing string representation and isEmpty state.
181
+ *
182
+ * @param Range<R> $expected
183
+ * @param Range<R> $actual
156
184
*/
157
185
protected function assertRangeEquals (Range $ expected , Range $ actual , string $ message = '' ): void
158
186
{
@@ -163,6 +191,7 @@ protected function assertRangeEquals(Range $expected, Range $actual, string $mes
163
191
/**
164
192
* Assert that a range contains all the given values.
165
193
*
194
+ * @param Range<R> $range
166
195
* @param array<mixed> $values
167
196
*/
168
197
protected function assertRangeContainsAll (Range $ range , array $ values , string $ message = '' ): void
@@ -178,6 +207,7 @@ protected function assertRangeContainsAll(Range $range, array $values, string $m
178
207
/**
179
208
* Assert that a range does not contain any of the given values.
180
209
*
210
+ * @param Range<R> $range
181
211
* @param array<mixed> $values
182
212
*/
183
213
protected function assertRangeContainsNone (Range $ range , array $ values , string $ message = '' ): void
@@ -192,6 +222,8 @@ protected function assertRangeContainsNone(Range $range, array $values, string $
192
222
193
223
/**
194
224
* Assert that a range has the expected string representation.
225
+ *
226
+ * @param Range<R> $range
195
227
*/
196
228
protected function assertRangeStringEquals (string $ expected , Range $ range , string $ message = '' ): void
197
229
{
@@ -200,6 +232,8 @@ protected function assertRangeStringEquals(string $expected, Range $range, strin
200
232
201
233
/**
202
234
* Assert that a range is empty.
235
+ *
236
+ * @param Range<R> $range
203
237
*/
204
238
protected function assertRangeIsEmpty (Range $ range , string $ message = '' ): void
205
239
{
@@ -209,6 +243,8 @@ protected function assertRangeIsEmpty(Range $range, string $message = ''): void
209
243
210
244
/**
211
245
* Assert that a range is not empty.
246
+ *
247
+ * @param Range<R> $range
212
248
*/
213
249
protected function assertRangeIsNotEmpty (Range $ range , string $ message = '' ): void
214
250
{
@@ -219,6 +255,7 @@ protected function assertRangeIsNotEmpty(Range $range, string $message = ''): vo
219
255
/**
220
256
* Test boundary conditions for a range with known bounds.
221
257
*
258
+ * @param Range<R> $range
222
259
* @param array<string, array{value: mixed, expected: bool}> $testCases
223
260
*/
224
261
protected function assertBoundaryConditions (Range $ range , array $ testCases , string $ message = '' ): void
@@ -257,7 +294,7 @@ protected function generateBoundaryTestCases(
257
294
/**
258
295
* Test that a range correctly handles equal bounds with different bracket combinations.
259
296
*
260
- * @param callable $rangeFactory Function that creates a range: fn($lower, $upper, $lowerInc, $upperInc) => Range
297
+ * @param callable(mixed, mixed, bool, bool): Range<R> $rangeFactory Function that creates a range
261
298
*/
262
299
protected function assertEqualBoundsHandling (callable $ rangeFactory , mixed $ value ): void
263
300
{
@@ -278,7 +315,7 @@ protected function assertEqualBoundsHandling(callable $rangeFactory, mixed $valu
278
315
/**
279
316
* Test that a range correctly handles reverse bounds (lower > upper).
280
317
*
281
- * @param callable $rangeFactory Function that creates a range: fn($lower, $upper, $lowerInc, $upperInc) => Range
318
+ * @param callable(mixed, mixed, bool, bool): Range<R> $rangeFactory Function that creates a range
282
319
*/
283
320
protected function assertReverseBoundsHandling (callable $ rangeFactory , mixed $ lower , mixed $ upper ): void
284
321
{
0 commit comments