forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupport.php
More file actions
224 lines (191 loc) · 6.16 KB
/
Copy pathsupport.php
File metadata and controls
224 lines (191 loc) · 6.16 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
<?php
// Utility functions used by Behat steps
function assertRegExp( $regex, $actual ) {
if ( ! preg_match( $regex, $actual ) ) {
throw new Exception( "Actual value: " . var_export( $actual, true ) );
}
}
function assertEquals( $expected, $actual ) {
if ( $expected != $actual ) {
throw new Exception( "Actual value: " . var_export( $actual, true ) );
}
}
function assertNotEquals( $expected, $actual ) {
if ( $expected == $actual ) {
throw new Exception( "Actual value: " . var_export( $actual, true ) );
}
}
function assertNumeric( $actual ) {
if ( !is_numeric( $actual ) ) {
throw new Exception( "Actual value: " . var_export( $actual, true ) );
}
}
function assertNotNumeric( $actual ) {
if ( is_numeric( $actual ) ) {
throw new Exception( "Actual value: " . var_export( $actual, true ) );
}
}
function checkString( $output, $expected, $action, $message = false ) {
switch ( $action ) {
case 'be':
$r = $expected === rtrim( _wp_cli_nocr( $output ), "\n" );
break;
case 'contain':
$r = false !== strpos( $output, $expected );
break;
case 'not contain':
$r = false === strpos( $output, $expected );
break;
default:
throw new Behat\Behat\Exception\PendingException();
}
if ( !$r ) {
if ( false === $message )
$message = $output;
throw new Exception( $message );
}
}
function compareTables( $expected_rows, $actual_rows, $output ) {
// the first row is the header and must be present
if ( $expected_rows[0] != $actual_rows[0] ) {
throw new \Exception( $output );
}
unset( $actual_rows[0] );
unset( $expected_rows[0] );
$missing_rows = array_diff( $expected_rows, $actual_rows );
if ( !empty( $missing_rows ) ) {
throw new \Exception( $output );
}
}
function compareContents( $expected, $actual ) {
if ( gettype( $expected ) != gettype( $actual ) ) {
return false;
}
if ( is_object( $expected ) ) {
foreach ( get_object_vars( $expected ) as $name => $value ) {
if ( ! compareContents( $value, $actual->$name ) )
return false;
}
} else if ( is_array( $expected ) ) {
foreach ( $expected as $key => $value ) {
if ( ! compareContents( $value, $actual[$key] ) )
return false;
}
} else {
return $expected === $actual;
}
return true;
}
/**
* Compare two strings containing JSON to ensure that @a $actualJson contains at
* least what the JSON string @a $expectedJson contains.
*
* @return whether or not @a $actualJson contains @a $expectedJson
* @retval true @a $actualJson contains @a $expectedJson
* @retval false @a $actualJson does not contain @a $expectedJson
*
* @param[in] $actualJson the JSON string to be tested
* @param[in] $expectedJson the expected JSON string
*
* 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
*/
function checkThatJsonStringContainsJsonString( $actualJson, $expectedJson ) {
$actualValue = json_decode( $actualJson );
$expectedValue = json_decode( $expectedJson );
if ( !$actualValue ) {
return false;
}
return compareContents( $expectedValue, $actualValue );
}
/**
* 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 A CSV string
* @param array A nested array of values
* @return bool Whether $actualCSV contains $expectedCSV
*/
function checkThatCsvStringContainsValues( $actualCSV, $expectedCSV ) {
$actualCSV = array_map( 'str_getcsv', explode( PHP_EOL, $actualCSV ) );
if ( empty( $actualCSV ) )
return false;
// Each sample must have headers
$actualHeaders = array_values( array_shift( $actualCSV ) );
$expectedHeaders = array_values( array_shift( $expectedCSV ) );
// Each expectedCSV must exist somewhere in actualCSV in the proper column
$expectedResult = 0;
foreach ( $expectedCSV as $expected_row ) {
$expected_row = array_combine( $expectedHeaders, $expected_row );
foreach ( $actualCSV as $actual_row ) {
if ( count( $actualHeaders ) != count( $actual_row ) )
continue;
$actual_row = array_intersect_key( array_combine( $actualHeaders, $actual_row ), $expected_row );
if ( $actual_row == $expected_row )
$expectedResult++;
}
}
return $expectedResult >= count( $expectedCSV );
}
/**
* Compare two strings containing YAML to ensure that @a $actualYaml contains at
* least what the YAML string @a $expectedYaml contains.
*
* @return whether or not @a $actualYaml contains @a $expectedJson
* @retval true @a $actualYaml contains @a $expectedJson
* @retval false @a $actualYaml does not contain @a $expectedJson
*
* @param[in] $actualYaml the YAML string to be tested
* @param[in] $expectedYaml the expected YAML string
*/
function checkThatYamlStringContainsYamlString( $actualYaml, $expectedYaml ) {
$actualValue = Mustangostang\Spyc::YAMLLoad( $actualYaml );
$expectedValue = Mustangostang\Spyc::YAMLLoad( $expectedYaml );
if ( !$actualValue ) {
return false;
}
return compareContents( $expectedValue, $actualValue );
}
/**
* Helper to strip carriage returns from input for Windows compat.
*
* @param string|array String or array of strings to strip.
* @return string|array String or array stripped of carriage returns.
*/
function _wp_cli_nocr( $str_or_arr ) {
if ( is_string( $str_or_arr ) ) {
return str_replace( "\r", '', $str_or_arr );
}
return array_map( function ( $str ) {
return str_replace( "\r", '', $str );
}, $str_or_arr );
}
/**
* Helper to convert *nix shell escaped command to Windows. Primitive.
*
* @param string $cmd Command to convert.
* @return string Command escaped for Windows - ie. single quotes converted to double quotes and vice versa.
*/
function _wp_cli_esc_cmd_win( $cmd ) {
return str_replace( array( "'", '"', '<esc>' ), array( '<esc>', "'", '"' ), $cmd );
}