Skip to content

Commit ac9f364

Browse files
committed
Initial commit, issue-6088
Add unit test for constraint
1 parent 3d06c6f commit ac9f364

File tree

1 file changed

+321
-0
lines changed

1 file changed

+321
-0
lines changed
Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PHPUnit\Framework\Constraint\Dictionary;
6+
7+
use AssertionError;
8+
use PHPUnit\Framework\Attributes\CoversClass;
9+
use PHPUnit\Framework\Attributes\DataProvider;
10+
use PHPUnit\Framework\Constraint\Constraint;
11+
use PHPUnit\Framework\ExpectationFailedException;
12+
use PHPUnit\Framework\TestCase;
13+
use stdClass;
14+
15+
#[CoversClass(IsIdenticalKeysValues::class)]
16+
#[CoversClass(Constraint::class)]
17+
#[Small]
18+
final class IsIdenticalKeysValuesTest extends TestCase
19+
{
20+
public static function provider(): array
21+
{
22+
return [
23+
'expected is not an array' => [
24+
false,
25+
'is identical to \'not-array\'',
26+
'assert(is_array($this->value))',
27+
'',
28+
'not-array',
29+
[],
30+
],
31+
'actual is not an array' => [
32+
false,
33+
'is identical to Array &0 []',
34+
'assert(is_array($other))',
35+
'',
36+
[],
37+
'not-array',
38+
],
39+
'expected key missing from actual' => [
40+
false,
41+
<<<'EOT'
42+
is identical to Array &%d [
43+
'a' => 0,
44+
]
45+
EOT
46+
,
47+
'Failed asserting that two arrays are equal.',
48+
<<<'EOT'
49+
Failed asserting that two arrays are equal.
50+
--- Expected
51+
+++ Actual
52+
@@ @@
53+
Array (
54+
- 'a' => 0
55+
+ 0 => 0
56+
)
57+
58+
EOT
59+
,
60+
['a' => 0],
61+
[0],
62+
],
63+
'actual has unexpected key' => [
64+
false,
65+
<<<'EOT'
66+
is identical to Array &%d [
67+
0 => 0,
68+
]
69+
EOT
70+
,
71+
'Failed asserting that two arrays are equal.',
72+
<<<'EOT'
73+
Failed asserting that two arrays are equal.
74+
--- Expected
75+
+++ Actual
76+
@@ @@
77+
Array (
78+
- 0 => 0
79+
+ 'a' => 0
80+
)
81+
82+
EOT
83+
,
84+
[0],
85+
['a' => 0],
86+
],
87+
'expected value is array and actual value is not' => [
88+
false,
89+
<<<'EOT'
90+
is identical to Array &%d [
91+
'a' => Array &%d [],
92+
]
93+
EOT
94+
,
95+
'Failed asserting that two arrays are equal.',
96+
<<<'EOT'
97+
Failed asserting that two arrays are equal.
98+
--- Expected
99+
+++ Actual
100+
@@ @@
101+
Array (
102+
- 'a' => Array &%d []
103+
+ 'a' => 0
104+
)
105+
106+
EOT
107+
,
108+
['a' => []],
109+
['a' => 0],
110+
],
111+
'expected value is object and actual value is not' => [
112+
false,
113+
<<<'EOT'
114+
is identical to Array &%d [
115+
'a' => stdClass Object #%d (),
116+
]
117+
EOT
118+
,
119+
'Failed asserting that two arrays are equal.',
120+
<<<'EOT'
121+
Failed asserting that two arrays are equal.
122+
--- Expected
123+
+++ Actual
124+
@@ @@
125+
Array (
126+
- 'a' => stdClass Object #%d ()
127+
+ 'a' => 0
128+
)
129+
130+
EOT
131+
,
132+
['a' => new stdClass()],
133+
['a' => 0],
134+
],
135+
'expected object value does not match actual object value' => [
136+
false,
137+
<<<'EOT'
138+
is identical to Array &%d [
139+
'a' => stdClass Object #%d (
140+
'a' => 1,
141+
),
142+
]
143+
EOT
144+
,
145+
'Failed asserting that two arrays are equal.',
146+
<<<'EOT'
147+
Failed asserting that two arrays are equal.
148+
--- Expected
149+
+++ Actual
150+
@@ @@
151+
Array (
152+
'a' => stdClass Object (
153+
- 'a' => 1
154+
+ 'a' => '1'
155+
)
156+
)
157+
158+
EOT
159+
,
160+
['a' => self::stdClass('a', 1)],
161+
['a' => self::stdClass('a', '1')],
162+
],
163+
'empty arrays are equal' => [
164+
true,
165+
'is identical to Array &%d []',
166+
'',
167+
'',
168+
[],
169+
[],
170+
],
171+
'key equality (string, bool, int)' => [
172+
true,
173+
<<<'EOT'
174+
is identical to Array &%d [
175+
'string' => 'string',
176+
1 => 1,
177+
]
178+
EOT
179+
,
180+
'',
181+
'',
182+
[
183+
'string' => 'string',
184+
true => true,
185+
1 => 1,
186+
],
187+
[
188+
'string' => 'string',
189+
true => true,
190+
1 => 1,
191+
],
192+
],
193+
'value equality (string, bool, int, float, object, array, dictionary)' => [
194+
true,
195+
<<<'EOT'
196+
is identical to Array &%d [
197+
'string' => 'string',
198+
1 => 1,
199+
2 => 2.5,
200+
'object' => stdClass Object #%d (
201+
'key' => 'value',
202+
),
203+
'array' => Array &%d [
204+
0 => 1,
205+
1 => 2,
206+
2 => 3,
207+
],
208+
'dictionary' => Array &%d [
209+
'string' => 'string',
210+
1 => 1,
211+
2 => 2.5,
212+
'object' => stdClass Object #%d (
213+
'key' => 'value',
214+
),
215+
'array' => Array &%d [
216+
0 => 1,
217+
1 => 2,
218+
2 => 3,
219+
],
220+
],
221+
]
222+
EOT
223+
,
224+
'',
225+
'',
226+
[
227+
'string' => 'string',
228+
true => true,
229+
1 => 1,
230+
2 => 2.5,
231+
'object' => self::stdClass('key', 'value'),
232+
'array' => [1, 2, 3],
233+
'dictionary' => [
234+
'string' => 'string',
235+
true => true,
236+
1 => 1,
237+
2 => 2.5,
238+
'object' => self::stdClass('key', 'value'),
239+
'array' => [1, 2, 3],
240+
],
241+
],
242+
[
243+
'string' => 'string',
244+
true => true,
245+
1 => 1,
246+
2 => 2.5,
247+
'object' => self::stdClass('key', 'value'),
248+
'array' => [1, 2, 3],
249+
'dictionary' => [
250+
'string' => 'string',
251+
true => true,
252+
1 => 1,
253+
2 => 2.5,
254+
'object' => self::stdClass('key', 'value'),
255+
'array' => [1, 2, 3],
256+
],
257+
],
258+
],
259+
];
260+
}
261+
262+
#[DataProvider('provider')]
263+
public function testCanBeEvaluated(
264+
bool $result,
265+
string $constraintAsString,
266+
string $failureDescription,
267+
string $comparisonFailureAsString,
268+
mixed $expected,
269+
mixed $actual
270+
): void {
271+
$constraint = new IsIdenticalKeysValues($expected);
272+
273+
try {
274+
$this->assertSame($result, $constraint->evaluate($actual, returnResult: true));
275+
if ($result) {
276+
return;
277+
}
278+
$constraint->evaluate($actual);
279+
} catch (AssertionError $e) {
280+
$this->assertSame($failureDescription, $e->getMessage());
281+
return;
282+
} catch (ExpectationFailedException $e) {
283+
$this->assertSame($failureDescription, $e->getMessage());
284+
$this->assertStringMatchesFormat(
285+
$comparisonFailureAsString,
286+
$e->getComparisonFailure() ? $e->getComparisonFailure()->toString() : ''
287+
);
288+
return;
289+
}
290+
291+
$this->fail();
292+
}
293+
294+
#[DataProvider('provider')]
295+
public function testCanBeRepresentedAsString(
296+
bool $result,
297+
string $constraintAsString,
298+
string $failureDescription,
299+
string $comparisonFailureAsString,
300+
mixed $expected,
301+
mixed $actual
302+
): void {
303+
$constraint = new IsIdenticalKeysValues($expected);
304+
305+
$this->assertStringMatchesFormat($constraintAsString, $constraint->toString());
306+
}
307+
308+
public function testIsCountable(): void
309+
{
310+
$this->assertCount(1, (new IsIdenticalKeysValues([])));
311+
}
312+
313+
private static function stdClass(string $key, mixed $value): stdClass
314+
{
315+
$o = new stdClass;
316+
317+
$o->{$key} = $value;
318+
319+
return $o;
320+
}
321+
}

0 commit comments

Comments
 (0)