-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathFunctions.php
More file actions
437 lines (367 loc) · 14.1 KB
/
Functions.php
File metadata and controls
437 lines (367 loc) · 14.1 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<?php
namespace WP_Mock;
use Closure;
use InvalidArgumentException;
use Mockery;
use Mockery\Matcher\AnyOf;
use Mockery\Matcher\Type;
use WP_Mock;
use WP_Mock\Functions\Handler;
use WP_Mock\Functions\ReturnSequence;
/**
* Functions mocking manager.
*
* This internal class is responsible for mocking WordPress functions and methods.
*
* @see WP_Mock::userFunction()
* @see WP_Mock::echoFunction()
* @see WP_Mock::passthruFunction()
*/
class Functions
{
/** @var array<string, Mockery\Mock> container of function names holding a Mock object each handled by WP_Mock */
private array $mockedFunctions = [];
/** @var string[] list of user-defined functions (e.g. WordPress functions) mocked by WP_Mock */
private static array $userMockedFunctions = [];
/** @var string[] list of functions redefined by WP_Mock through Patchwork */
private array $patchworkFunctions = [];
/** @var string[] list of PHP internal functions as per {@see get_defined_functions()} */
private array $internalFunctions = [];
/**
* Initializes the handler.
*/
public function __construct()
{
Handler::cleanup();
$this->flush();
}
/**
* Flushes (resets) the registered mocked functions.
*
* @return void
*/
public function flush(): void
{
$this->mockedFunctions = [];
Handler::cleanup();
$this->patchworkFunctions = [];
if (function_exists('Patchwork\undoAll')) {
\Patchwork\restoreAll();
}
if (empty(self::$userMockedFunctions)) {
self::$userMockedFunctions = [
'__',
'_e',
'_n',
'_x',
'add_action',
'add_filter',
'apply_filters',
'do_action',
'esc_attr',
'esc_attr__',
'esc_attr_e',
'esc_attr_x',
'esc_html',
'esc_html__',
'esc_html_e',
'esc_html_x',
'esc_js',
'esc_textarea',
'esc_url',
'esc_url_raw',
];
}
}
/**
* Registers a function to be mocked and sets up its expectations.
*
* @param string|callable-string $function function name
* @param array<string, mixed> $args optional arguments
* @return Mockery\Expectation
* @throws InvalidArgumentException
*/
public function register(string $function, array $args = [])
{
$functionArgs = isset($args['args']) && is_array($args['args']) ? $args['args'] : [];
$this->generateFunction($function, $functionArgs);
if (empty($this->mockedFunctions[$function])) {
/** @phpstan-ignore-next-line */
$this->mockedFunctions[$function] = Mockery::mock('wp_api');
}
/** @var Mockery\Mock $mock */
$mock = $this->mockedFunctions[$function];
/** @var callable-string $method */
$method = preg_replace('/\\\\+/', '_', $function);
/** @var Mockery\Expectation $expectation */
$expectation = $this->setUpMock($mock, $method, $args);
Handler::registerHandler($function, [$mock, $method]);
return $expectation;
}
/**
* Sets up the mock object with expectations.
*
* @param Mockery\Mock|Mockery\MockInterface|Mockery\LegacyMockInterface $mock mock object
* @param string $functionName function name
* @param array<string, mixed> $args optional arguments for setting expectations on the mock
* @return Mockery\Expectation|Mockery\CompositeExpectation
*/
protected function setUpMock($mock, string $functionName, array $args = [])
{
/** @var Mockery\Expectation|Mockery\CompositeExpectation $expectation */
$expectation = $mock->shouldReceive($functionName);
// set the expected times the function should be called
if (isset($args['times'])) {
$this->setExpectedTimes($expectation, $args['times']);
}
// set the expected arguments the function should be called with
if (isset($args['args'])) {
$this->setExpectedArgs(
$expectation,
is_array($args['args']) ? array_values($args['args']): $args['args']
);
}
// set the expected return value based on a passed argument or return values for each call in order
if (isset($args['return_arg']) || isset($args['return_in_order'])) {
$args['return'] = $this->parseExpectedReturn($args);
}
// set the expected return value of the function
if (isset($args['return'])) {
$this->setExpectedReturn($expectation, $args['return']);
}
return $expectation;
}
/**
* Sets the expected times a function should be called based on arguments.
*
* @param Mockery\Expectation|Mockery\CompositeExpectation $expectation
* @param int|string|mixed $times
* @return Mockery\Expectation|Mockery\CompositeExpectation
*/
protected function setExpectedTimes(&$expectation, $times)
{
if (is_int($times) || (is_string($times) && preg_match('/^\d+$/', $times))) {
/** @phpstan-ignore-next-line method exists */
$expectation->times((int) $times);
} elseif (is_string($times)) {
if (preg_match('/^(\d+)([\-+])$/', $times, $matches)) {
$method = '+' === $matches[2] ? 'atLeast' : 'atMost';
$expectation->$method()->times((int) $matches[1]);
} elseif (preg_match('/^(\d+)-(\d+)$/', $times, $matches)) {
$num1 = (int) $matches[1];
$num2 = (int) $matches[2];
if ($num1 === $num2) {
/** @phpstan-ignore-next-line method exists */
$expectation->times($num1);
} else {
/** @phpstan-ignore-next-line method exists */
$expectation->between(min($num1, $num2), max($num1, $num2));
}
}
}
return $expectation;
}
/**
* Sets the expected arguments that a function should be called with.
*
* @param Mockery\Expectation|Mockery\CompositeExpectation $expectation
* @param mixed $args expected arguments passed to the function
* @return Mockery\Expectation|Mockery\CompositeExpectation
*/
protected function setExpectedArgs(&$expectation, $args)
{
$args = array_map(function ($argument) {
if ($argument instanceof Closure) {
return Mockery::on($argument);
}
if ($argument === '*') {
return Mockery::any();
}
return $argument;
}, (array) $args);
/** @phpstan-ignore-next-line method exists on expectation */
call_user_func_array([$expectation, 'with'], $args);
return $expectation;
}
/**
* Parses arguments for setting the expectation `return` arg.
*
* @param array<string, mixed> $args
* @return Closure|ReturnSequence|null
*/
protected function parseExpectedReturn(array $args)
{
$returnValue = null;
if (isset($args['return_arg'])) {
/** @phpstan-ignore-next-line */
$argPosition = max(true === $args['return_arg'] ? 0 : (int) $args['return_arg'], 0);
// set the expected return value based on an argument passed to the function
$returnValue = function () use ($argPosition) {
if ($argPosition >= func_num_args()) {
return null;
}
return func_get_arg($argPosition);
};
} elseif (isset($args['return_in_order'])) {
// sets the return values for each call in order
$returnValue = new ReturnSequence();
$returnValue->setReturnValues((array) $args['return_in_order']);
}
return $returnValue;
}
/**
* Sets the expected return value for the expectation.
*
* @param Mockery\Expectation $expectation
* @param Closure|ReturnSequence|mixed $return
* @return Mockery\Expectation
*/
protected function setExpectedReturn(&$expectation, $return)
{
if ($return instanceof ReturnSequence) {
$expectation->andReturnValues($return->getReturnValues());
} elseif ($return instanceof Closure) {
$expectation->andReturnUsing($return);
} else {
$expectation->andReturn($return);
}
return $expectation;
}
/**
* Dynamically declares a function if it doesn't already exist.
*
* The declared function is namespace-aware.
*
* @param string $functionName function name
* @param array<int|string, mixed> $functionArgs function arguments
* @return void
* @throws InvalidArgumentException
*/
protected function generateFunction(string $functionName, array $functionArgs = []): void
{
$functionName = $this->sanitizeFunctionName($functionName);
$this->validateFunctionName($functionName);
$this->createFunction($functionName, $functionArgs) or $this->replaceFunction($functionName);
}
/**
* Creates a function using eval.
*
* @param string $functionName function name
* @param array<int|string, mixed> $functionArgs function arguments, required only when calling mocked functions using named parameters
* @return bool true if this function created the mock, false otherwise
*/
protected function createFunction(string $functionName, array $functionArgs = []): bool
{
if (in_array($functionName, self::$userMockedFunctions, true)) {
return true;
}
if (function_exists($functionName)) {
return false;
}
$parts = explode('\\', $functionName);
$name = array_pop($parts);
$namespace = empty($parts) ? '' : 'namespace '.implode('\\', $parts).';'.PHP_EOL;
$functionNamedParameters = '';
$has_named_parameters = array_reduce( array_keys($functionArgs), function ($carry, $arg) {
return $carry && !is_int($arg);
}, true);
if($has_named_parameters){
$functionNamedParameters = implode(', ', array_map(fn($name) => '$' . $name, array_keys($functionArgs)));
}
$declaration = <<<EOF
$namespace
function $name($functionNamedParameters) {
return \\WP_Mock\\Functions\\Handler::handleFunction('$functionName', func_get_args());
}
EOF;
eval($declaration);
self::$userMockedFunctions[] = $functionName;
return true;
}
/**
* Replaces a function using Patchwork.
*
* @param string $functionName function name
* @return bool
*/
protected function replaceFunction(string $functionName): bool
{
if (in_array($functionName, $this->patchworkFunctions, true)) {
return true;
}
if (! function_exists('Patchwork\\replace')) {
return true;
}
$this->patchworkFunctions[] = $functionName;
\Patchwork\redefine($functionName, function () use ($functionName) {
return Handler::handleFunction($functionName, func_get_args());
});
return true;
}
/**
* Cleans a function name to be of a standard shape.
*
* Trims any namespace separators from the function name.
*
* @param string $functionName
* @return string
*/
protected function sanitizeFunctionName(string $functionName): string
{
return trim($functionName, '\\');
}
/**
* Validates a function name for format and other considerations.
*
* Validation will fail if not a valid function name, if it's an internal function, or if it is a reserved word in PHP.
*
* @param string $functionName
* @return void
* @throws InvalidArgumentException
*/
protected function validateFunctionName(string $functionName): void
{
if (function_exists($functionName)) {
if (empty($this->internalFunctions)) {
$definedFunctions = get_defined_functions();
$this->internalFunctions = $definedFunctions['internal'];
}
if (in_array($functionName, $this->internalFunctions)) {
throw new InvalidArgumentException('Cannot override internal PHP functions!');
}
}
$parts = explode('\\', $functionName);
$name = array_pop($parts);
if (! preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $functionName)) {
throw new InvalidArgumentException('Function name not properly formatted!');
}
$reservedWords = ' __halt_compiler abstract and array as break callable case catch class clone const continue declare default die do echo else elseif empty enddeclare endfor endforeach endif endswitch endwhile eval exit extends final for foreach function global goto if implements include include_once instanceof insteadof interface isset list namespace new or print private protected public require require_once return static switch throw trait try unset use var while xor __CLASS__ __DIR__ __FILE__ __FUNCTION__ __LINE__ __METHOD__ __NAMESPACE__ __TRAIT__ ';
if (false !== strpos($reservedWords, " $name ")) {
throw new InvalidArgumentException('Function name cannot be a reserved word!');
}
}
/**
* Sets up an argument placeholder that allows it to be any of an enumerated list of possibilities.
*
* @return AnyOf
*/
public static function anyOf(): AnyOf
{
/** @phpstan-ignore-next-line */
return call_user_func_array(['\\Mockery', 'anyOf'], func_get_args());
}
/**
* Sets up an argument placeholder that requires the argument to be of a certain type.
*
* This may be any type for which there is a "is_*" function, or any class or interface.
*
* @param string $expected
* @return Type
*/
public static function type(string $expected): Type
{
$type = Mockery::type($expected);
Filter::$objects[ $expected ] = spl_object_hash($type);
return $type;
}
}