Skip to content

Commit afceedd

Browse files
committed
Use data providers for tests
1 parent 7fa955d commit afceedd

1 file changed

Lines changed: 140 additions & 69 deletions

File tree

tests/Php86/Php86Test.php

Lines changed: 140 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -15,83 +15,154 @@
1515

1616
class Php86Test extends TestCase
1717
{
18-
public function testClamp(): void
18+
/**
19+
* @dataProvider provideValidClampInput
20+
*/
21+
public function testClampSuccess(array $arguments, $result): void
22+
{
23+
[$value, $min, $max] = $arguments;
24+
25+
$this->assertSame($result, clamp($value, $min, $max));
26+
}
27+
28+
public function testClampNanReturn(): void
1929
{
20-
$this->assertSame(2, clamp(2, 1, 3));
21-
$this->assertSame(1, clamp(0, 1, 3));
22-
$this->assertSame(3, clamp(6, 1, 3));
23-
$this->assertSame(2, clamp(2, 1.3, 3.4));
24-
$this->assertSame(2.5, clamp(2.5, 1, 3));
25-
$this->assertSame(2.5, clamp(2.5, 1.3, 3.4));
26-
$this->assertSame(1.3, clamp(0, 1.3, 3.4));
27-
$this->assertSame(M_PI, clamp(M_PI, -INF, INF));
2830
$this->assertTrue(is_nan(clamp(NAN, 4, 6)));
29-
$this->assertSame('c', clamp('a', 'c', 'g'));
30-
$this->assertSame('d', clamp('d', 'c', 'g'));
31-
$this->assertSame('2025-08-15', clamp('2025-08-01', '2025-08-15', '2025-09-15'));
32-
$this->assertSame('2025-08-20', clamp('2025-08-20', '2025-08-15', '2025-09-15'));
33-
$this->assertSame('2025-08-15', clamp(new \DateTimeImmutable('2025-08-01'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'));
34-
$this->assertSame('2025-08-20', clamp(new \DateTimeImmutable('2025-08-20'), new \DateTimeImmutable('2025-08-15'), new \DateTimeImmutable('2025-09-15'))->format('Y-m-d'));
35-
$this->assertSame(-1, clamp(null, -1, 1));
36-
$this->assertSame(1, clamp(null, 1, 3));
37-
$this->assertSame(-3, clamp(null, -3, -1));
38-
$this->assertSame(-9999, clamp(-9999, null, 10));
39-
$this->assertSame(10, clamp(12, null, 10));
31+
}
32+
33+
public static function provideValidClampInput(): array
34+
{
4035
$a = new \InvalidArgumentException('a');
4136
$b = new \RuntimeException('b');
4237
$c = new \LogicException('c');
43-
$this->assertSame($a, clamp($a, $b, $c));
44-
$this->assertSame($b, clamp($b, $a, $c));
45-
$this->assertSame($c, clamp($c, $a, $b));
46-
47-
$errorMessage = null;
48-
49-
try {
50-
clamp(4, NAN, 6);
51-
} catch (\ValueError $error) {
52-
$errorMessage = $error->getMessage();
53-
}
54-
55-
$this->assertSame('clamp(): Argument #2 ($min) cannot be NAN', $errorMessage);
56-
57-
$errorMessage = null;
5838

59-
try {
60-
clamp(7, 6, NAN);
61-
} catch (\ValueError $error) {
62-
$errorMessage = $error->getMessage();
63-
}
64-
65-
$this->assertSame('clamp(): Argument #3 ($max) cannot be NAN', $errorMessage);
66-
67-
$errorMessage = null;
68-
69-
try {
70-
clamp(1, 3, 2);
71-
} catch (\ValueError $error) {
72-
$errorMessage = $error->getMessage();
73-
}
74-
75-
$this->assertSame('clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)', $errorMessage);
76-
77-
$errorMessage = null;
78-
79-
try {
80-
clamp(-9999, 5, null);
81-
} catch (\ValueError $error) {
82-
$errorMessage = $error->getMessage();
83-
}
84-
85-
$this->assertSame('clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)', $errorMessage);
39+
return [
40+
[
41+
[2, 1, 3],
42+
2,
43+
],
44+
[
45+
[0, 1, 3],
46+
1,
47+
],
48+
[
49+
[6, 1, 3],
50+
3,
51+
],
52+
[
53+
[2, 1.3, 3.4],
54+
2,
55+
],
56+
[
57+
[2.5, 1, 3],
58+
2.5,
59+
],
60+
[
61+
[2.5, 1.3, 3.4],
62+
2.5,
63+
],
64+
[
65+
[0, 1.3, 3.4],
66+
1.3,
67+
],
68+
[
69+
[M_PI, -INF, INF],
70+
M_PI,
71+
],
72+
[
73+
['a', 'c', 'g'],
74+
'c',
75+
],
76+
[
77+
['d', 'c', 'g'],
78+
'd',
79+
],
80+
[
81+
['2025-08-01', '2025-08-15', '2025-09-15'],
82+
'2025-08-15',
83+
],
84+
[
85+
['2025-08-20', '2025-08-15', '2025-09-15'],
86+
'2025-08-20',
87+
],
88+
[
89+
[new \DateTimeImmutable('2025-08-01'), new \DateTimeImmutable('2025-08-15'), (new \DateTimeImmutable('2025-09-15'))->format('Y-m-d')],
90+
'2025-08-15',
91+
],
92+
[
93+
[new \DateTimeImmutable('2025-08-20'), new \DateTimeImmutable('2025-08-15'), (new \DateTimeImmutable('2025-09-15'))->format('Y-m-d')],
94+
'2025-08-20',
95+
],
96+
[
97+
[null, -1, 1],
98+
-1,
99+
],
100+
[
101+
[null, 1, 3],
102+
1,
103+
],
104+
[
105+
[null, -3, -1],
106+
-3,
107+
],
108+
[
109+
[-9999, null, 10],
110+
-9999,
111+
],
112+
[
113+
[12, null, 10],
114+
10,
115+
],
116+
[
117+
[$a, $b, $c],
118+
$a,
119+
],
120+
[
121+
[$b, $a, $c],
122+
$b,
123+
],
124+
[
125+
[$c, $a, $b],
126+
$c,
127+
],
128+
];
129+
}
86130

87-
$errorMessage = null;
131+
/**
132+
* @dataProvider provideInvalidClampInput
133+
*/
134+
public function testClampFailure(array $arguments, string $error): void
135+
{
136+
$this->expectException(\ValueError::class);
137+
$this->expectExceptionMessage($error);
88138

89-
try {
90-
clamp(12, -5, null);
91-
} catch (\ValueError $error) {
92-
$errorMessage = $error->getMessage();
93-
}
139+
[$value, $min, $max] = $arguments;
140+
clamp($value, $min, $max);
141+
}
94142

95-
$this->assertSame('clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)', $errorMessage);
143+
public static function provideInvalidClampInput(): array
144+
{
145+
return [
146+
[
147+
[4, NAN, 6],
148+
'clamp(): Argument #2 ($min) must be NAN',
149+
],
150+
[
151+
[7, 6, NAN],
152+
'clamp(): Argument #3 ($max) must be NAN',
153+
],
154+
[
155+
[1, 3, 2],
156+
'clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)',
157+
],
158+
[
159+
[-9999, 5, null],
160+
'clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)',
161+
],
162+
[
163+
[12, -5, null],
164+
'clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)',
165+
],
166+
];
96167
}
97168
}

0 commit comments

Comments
 (0)