-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathSupport.php
More file actions
339 lines (299 loc) · 8.97 KB
/
Support.php
File metadata and controls
339 lines (299 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* Utility functions used by the Behat steps.
*/
namespace WP_CLI\Tests\Context;
use Exception;
use Mustangostang\Spyc;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
trait Support {
/**
* @param string $regex
* @param string $actual
* @throws Exception
*/
protected function assert_regex( $regex, $actual ): void {
if ( ! preg_match( $regex, $actual ) ) {
throw new Exception( 'Actual value: ' . var_export( $actual, true ) );
}
}
/**
* @param string $regex
* @param string $actual
* @throws Exception
*/
protected function assert_not_regex( $regex, $actual ): void {
if ( preg_match( $regex, $actual ) ) {
throw new Exception( 'Actual value: ' . var_export( $actual, true ) );
}
}
/**
* Loose comparison.
*
* @param mixed $expected
* @param mixed $actual
* @throws Exception
*/
protected function assert_equals( $expected, $actual ): void {
// phpcs:ignore Universal.Operators.StrictComparisons.LooseNotEqual -- Deliberate loose comparison.
if ( $expected != $actual ) {
throw new Exception( 'Actual value: ' . var_export( $actual, true ) );
}
}
/**
* Loose comparison.
*
* @param mixed $expected
* @param mixed $actual
* @throws Exception
*/
protected function assert_not_equals( $expected, $actual ): void {
// phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual -- Deliberate loose comparison.
if ( $expected == $actual ) {
throw new Exception( 'Actual value: ' . var_export( $actual, true ) );
}
}
/**
* @param mixed $actual
* @throws Exception
*
* @phpstan-assert numeric-string|number $actual
*/
protected function assert_numeric( $actual ): void {
if ( ! is_numeric( $actual ) ) {
throw new Exception( 'Actual value: ' . var_export( $actual, true ) );
}
}
/**
* @param mixed $actual
* @throws Exception
*
* @phpstan-assert !(numeric-string|number) $actual
*/
protected function assert_not_numeric( $actual ): void {
if ( is_numeric( $actual ) ) {
throw new Exception( 'Actual value: ' . var_export( $actual, true ) );
}
}
/**
* @param string $output
* @param string $expected
* @param string $action
* @param string|false $message
* @param bool $strictly
* @throws Exception
*/
protected function check_string( $output, $expected, $action, $message = false, $strictly = false ): void {
// Strip ANSI color codes before comparing strings.
if ( ! $strictly ) {
$output = preg_replace( '/\e[[][A-Za-z0-9];?[0-9]*m?/', '', $output );
}
switch ( $action ) {
case 'be':
$r = rtrim( $output, "\n" ) === $expected;
break;
case 'contain':
$r = false !== strpos( $output, $expected );
break;
case 'not contain':
$r = false === strpos( $output, $expected );
break;
default:
throw new \Behat\Behat\Tester\Exception\PendingException();
}
if ( ! $r ) {
if ( false === $message ) {
$message = $output;
}
$action_message = '';
switch ( $action ) {
case 'be':
$action_message = 'Output does not exactly match expected string:';
break;
case 'contain':
$action_message = 'Output does not contain expected string:';
break;
case 'not contain':
$action_message = 'Output unexpectedly contains string:';
break;
default:
throw new \Behat\Behat\Tester\Exception\PendingException();
}
$message .= "\n\n" . $action_message . "\n" . $expected;
$diff = $this->generate_diff( $expected, rtrim( $output, "\n" ) );
if ( ! empty( $diff ) ) {
$message .= "\n\n" . $diff;
}
throw new Exception( $message );
}
}
/**
* @param array<int, mixed> $expected_rows
* @param array<int, mixed> $actual_rows
* @param string $output
* @throws Exception
*/
protected function compare_tables( $expected_rows, $actual_rows, $output ): void {
// The first row is the header and must be present.
if ( $expected_rows[0] !== $actual_rows[0] ) {
$expected_table = implode( "\n", $expected_rows );
$actual_table = implode( "\n", $actual_rows );
$diff = $this->generate_diff( $expected_table, $actual_table );
throw new Exception( $output . "\n\n" . $diff );
}
unset( $actual_rows[0] );
unset( $expected_rows[0] );
$missing_rows = array_diff( $expected_rows, $actual_rows );
if ( ! empty( $missing_rows ) ) {
$expected_table = implode( "\n", $expected_rows );
$actual_table = implode( "\n", $actual_rows );
$diff = $this->generate_diff( $expected_table, $actual_table );
throw new Exception( $output . "\n\n" . $diff );
}
}
/**
* @param mixed $expected
* @param mixed $actual
* @return bool
*/
protected function compare_contents( $expected, $actual ) {
if ( gettype( $expected ) !== gettype( $actual ) ) {
return false;
}
if ( is_object( $expected ) ) {
foreach ( get_object_vars( $expected ) as $name => $value ) {
if ( ! $this->compare_contents( $value, $actual->$name ) ) {
return false;
}
}
} elseif ( is_array( $expected ) ) {
foreach ( $expected as $key => $value ) {
if ( ! $this->compare_contents( $value, $actual[ $key ] ) ) {
return false;
}
}
} else {
return $expected === $actual;
}
return true;
}
/**
* Compare two strings containing JSON to ensure that $actualJson contains at
* least what the JSON string $expectedJson contains.
*
* @param string $actual_json the JSON string to be tested
* @param string $expected_json the expected JSON string
*
* @return bool Whether or not $actual_json contains $expected_json.
*
* Examples:
* expected: {'a':1,'array':[1,3,5]}
*
* 1 )
* actual: {'a':1,'b':2,'c':3,'array':[1,2,3,4,5]}
* return: true
*
* 2 )
* actual: {'b':2,'c':3,'array':[1,2,3,4,5]}
* return: false
* element 'a' is missing from the root object
*
* 3 )
* actual: {'a':0,'b':2,'c':3,'array':[1,2,3,4,5]}
* return: false
* the value of element 'a' is not 1
*
* 4 )
* actual: {'a':1,'b':2,'c':3,'array':[1,2,4,5]}
* return: false
* the contents of 'array' does not include 3
*/
protected function check_that_json_string_contains_json_string( $actual_json, $expected_json ) {
$actual_value = json_decode( $actual_json );
$expected_value = json_decode( $expected_json );
if ( ! $actual_value ) {
return false;
}
return $this->compare_contents( $expected_value, $actual_value );
}
/**
* Compare two strings to confirm $actualCSV contains $expectedCSV
* Both strings are expected to have headers for their CSVs.
* $actualCSV must match all data rows in $expectedCSV
*
* @param string $actual_csv A CSV string
* @param array<array<string>> $expected_csv A nested array of values
* @return bool Whether $actual_csv contains $expected_csv
*/
protected function check_that_csv_string_contains_values( $actual_csv, $expected_csv ) {
$actual_csv = array_map(
static function ( $str ) {
return str_getcsv( $str, ',', '"', '\\' );
},
explode( PHP_EOL, $actual_csv )
);
if ( empty( $actual_csv ) ) {
return false;
}
/**
* @var array<array<string>> $actual_csv
*/
// Each sample must have headers.
$actual_headers = array_values( array_shift( $actual_csv ) );
$expected_headers = array_values( array_shift( $expected_csv ) );
// Each expected_csv must exist somewhere in actual_csv in the proper column.
$expected_result = 0;
foreach ( $expected_csv as $expected_row ) {
$expected_row = array_combine( $expected_headers, $expected_row );
foreach ( $actual_csv as $actual_row ) {
if ( count( $actual_headers ) !== count( $actual_row ) ) {
continue;
}
$actual_row = array_intersect_key(
array_combine(
$actual_headers,
$actual_row
),
$expected_row
);
if ( $actual_row === $expected_row ) {
++$expected_result;
}
}
}
return $expected_result >= count( $expected_csv );
}
/**
* Compare two strings containing YAML to ensure that $actualYaml contains at
* least what the YAML string $expectedYaml contains.
*
* @param string $actual_yaml the YAML string to be tested
* @param string $expected_yaml the expected YAML string
*
* @return bool whether or not $actual_yaml contains $expected_json
*/
protected function check_that_yaml_string_contains_yaml_string( $actual_yaml, $expected_yaml ) {
$actual_value = Spyc::YAMLLoad( $actual_yaml );
$expected_value = Spyc::YAMLLoad( $expected_yaml );
if ( ! $actual_value ) {
return false;
}
return $this->compare_contents( $expected_value, $actual_value );
}
/**
* Generate a unified diff between two strings.
*
* @param string $expected The expected string.
* @param string $actual The actual string.
* @return string The unified diff output.
*/
protected function generate_diff( string $expected, string $actual ): string {
$builder = new UnifiedDiffOutputBuilder(
"--- Expected\n+++ Actual\n",
false
);
$differ = new Differ( $builder );
return $differ->diff( $expected, $actual );
}
}