1
1
<?php
2
2
3
3
declare (strict_types=1 );
4
-
4
+ /*
5
+ * This file is part of PHPUnit.
6
+ *
7
+ * (c) Sebastian Bergmann <[email protected] >
8
+ *
9
+ * For the full copyright and license information, please view the LICENSE
10
+ * file that was distributed with this source code.
11
+ */
5
12
namespace PHPUnit \Framework \Constraint \Dictionary ;
6
13
14
+ use function array_key_exists ;
15
+ use function assert ;
16
+ use function in_array ;
17
+ use function is_array ;
18
+ use function is_object ;
19
+ use function sprintf ;
20
+ use function str_replace ;
21
+ use function substr_replace ;
22
+ use function trim ;
7
23
use PHPUnit \Framework \Constraint \Constraint ;
8
24
use PHPUnit \Framework \ExpectationFailedException ;
9
25
use SebastianBergmann \Comparator \ComparisonFailure ;
@@ -42,11 +58,13 @@ public function evaluate(mixed $other, string $description = '', bool $returnRes
42
58
if ($ returnResult ) {
43
59
return false ;
44
60
}
61
+
45
62
throw new ExpectationFailedException (
46
63
trim ($ description . "\n" . $ f ->getMessage ()),
47
- $ f
64
+ $ f,
48
65
);
49
66
}
67
+
50
68
return true ;
51
69
}
52
70
@@ -60,15 +78,15 @@ public function toString(): string
60
78
61
79
/**
62
80
* cribbed from `vendor/sebastian/comparator/src/ArrayComparator.php`
63
- * This potentially should be a dictionarycomparator or type-strict arraycomparator
81
+ * This potentially should be a dictionarycomparator or type-strict arraycomparator.
64
82
*/
65
83
private function compareDictionary (array $ expected , array $ actual , array &$ processed = []): void
66
84
{
67
- $ remaining = $ actual ;
68
- $ actualAsString = "Array ( \n" ;
85
+ $ remaining = $ actual ;
86
+ $ actualAsString = "Array ( \n" ;
69
87
$ expectedAsString = "Array ( \n" ;
70
- $ equal = true ;
71
- $ exporter = new Exporter ;
88
+ $ equal = true ;
89
+ $ exporter = new Exporter ;
72
90
73
91
foreach ($ expected as $ key => $ value ) {
74
92
unset($ remaining [$ key ]);
@@ -80,6 +98,7 @@ private function compareDictionary(array $expected, array $actual, array &$proce
80
98
$ exporter ->shortenedExport ($ value ),
81
99
);
82
100
$ equal = false ;
101
+
83
102
continue ;
84
103
}
85
104
@@ -94,12 +113,13 @@ private function compareDictionary(array $expected, array $actual, array &$proce
94
113
$ exporter ->export ($ actual [$ key ]),
95
114
);
96
115
97
- // expected array, got array
116
+ // expected array, got array
98
117
case is_array ($ value ) && is_array ($ actual [$ key ]):
99
118
$ this ->compareDictionary ($ value , $ actual [$ key ]);
119
+
100
120
break ;
101
121
102
- // type mismatch, expected object, got something else
122
+ // type mismatch, expected object, got something else
103
123
case is_object ($ value ) && !is_object ($ actual [$ key ]):
104
124
throw new ComparisonFailure (
105
125
$ value ,
@@ -108,16 +128,18 @@ private function compareDictionary(array $expected, array $actual, array &$proce
108
128
$ exporter ->export ($ actual [$ key ]),
109
129
);
110
130
111
- // type mismatch, expected object, got object
131
+ // type mismatch, expected object, got object
112
132
case is_object ($ value ) && is_object ($ actual [$ key ]):
113
133
$ this ->compareObjects ($ value , $ actual [$ key ], $ processed );
134
+
114
135
break ;
115
136
116
- // both are not array, both are not objects, strict comparison check
137
+ // both are not array, both are not objects, strict comparison check
117
138
default :
118
139
if ($ value === $ actual [$ key ]) {
119
140
continue 2 ;
120
141
}
142
+
121
143
throw new ComparisonFailure (
122
144
$ value ,
123
145
$ actual [$ key ],
@@ -141,14 +163,14 @@ private function compareDictionary(array $expected, array $actual, array &$proce
141
163
" %s => %s \n" ,
142
164
$ exporter ->export ($ key ),
143
165
$ e ->getExpectedAsString () !== '' ? $ this ->indent (
144
- $ e ->getExpectedAsString ()
166
+ $ e ->getExpectedAsString (),
145
167
) : $ exporter ->shortenedExport ($ e ->getExpected ()),
146
168
);
147
169
$ actualAsString .= sprintf (
148
170
" %s => %s \n" ,
149
171
$ exporter ->export ($ key ),
150
172
$ e ->getActualAsString () !== '' ? $ this ->indent (
151
- $ e ->getActualAsString ()
173
+ $ e ->getActualAsString (),
152
174
) : $ exporter ->shortenedExport ($ e ->getActual ()),
153
175
);
154
176
$ equal = false ;
@@ -180,9 +202,9 @@ private function compareDictionary(array $expected, array $actual, array &$proce
180
202
181
203
/**
182
204
* cribbed from `vendor/sebastian/comparator/src/ObjectComparator.php`
183
- * this potentially should be a type-strict objectcomparator
205
+ * this potentially should be a type-strict objectcomparator.
184
206
*/
185
- private function compareObjects (object $ expected , object $ actual , array &$ processed = [])
207
+ private function compareObjects (object $ expected , object $ actual , array &$ processed = []): void
186
208
{
187
209
if ($ actual ::class !== $ expected ::class) {
188
210
$ exporter = new Exporter ;
@@ -207,9 +229,11 @@ private function compareObjects(object $expected, object $actual, array &$proces
207
229
}
208
230
209
231
$ processed [] = [$ actual , $ expected ];
232
+
210
233
if ($ actual === $ expected ) {
211
234
return ;
212
235
}
236
+
213
237
try {
214
238
$ this ->compareDictionary ($ this ->toArray ($ expected ), $ this ->toArray ($ actual ), $ processed );
215
239
} catch (ComparisonFailure $ e ) {
@@ -225,15 +249,15 @@ private function compareObjects(object $expected, object $actual, array &$proces
225
249
}
226
250
227
251
/**
228
- * cribbed from `vendor/sebastian/comparator/src/ObjectComparator.php`
252
+ * cribbed from `vendor/sebastian/comparator/src/ObjectComparator.php`.
229
253
*/
230
254
private function toArray (object $ object ): array
231
255
{
232
256
return (new Exporter )->toArray ($ object );
233
257
}
234
258
235
259
/**
236
- * cribbed from `vendor/sebastian/comparator/src/ArrayComparator.php`
260
+ * cribbed from `vendor/sebastian/comparator/src/ArrayComparator.php`.
237
261
*/
238
262
private function indent (string $ lines ): string
239
263
{
0 commit comments