-
Notifications
You must be signed in to change notification settings - Fork 30
/
api.php
398 lines (358 loc) · 11.8 KB
/
api.php
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
<?php # -*- coding: utf-8 -*-
/*
* This file is part of the BrainMonkey package.
*
* (c) Giuseppe Mazzapica
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
// Ignore this. Just a safeguard in case of WordPress + Composer broken (really broken) setup.
namespace {
if (function_exists('Brain\Monkey\setUp')) {
return;
}
}
namespace Brain\Monkey {
/**
* Setup function to be called before _each_ unit test. This is not required to just mock
* PHP functions without using WP features.
*/
function setUp()
{
require_once dirname(__DIR__).'/inc/patchwork-loader.php';
require_once dirname(__DIR__).'/inc/wp-hook-functions.php';
require_once dirname(__DIR__).'/inc/wp-helper-functions.php';
}
/**
* Setup function to be called after _each_ unit test. This is *always* required.
*/
function tearDown()
{
Container::instance()->reset();
\Mockery::close();
\Patchwork\restoreAll();
}
}
namespace Brain\Monkey\Functions {
use Brain\Monkey\Container;
use Brain\Monkey\Expectation\EscapeHelper;
use Brain\Monkey\Expectation\FunctionStubFactory;
use Brain\Monkey\Name\FunctionName;
/**
* API entry-point for plain functions stub.
*
* Factory method: receives the name of the function to mock and returns an instance of
* FunctionStub.
*
* @param string $function_name the name of the function to mock
* @return \Brain\Monkey\Expectation\FunctionStub
*/
function when($function_name)
{
return Container::instance()
->functionStubFactory()
->create(new FunctionName($function_name), FunctionStubFactory::SCOPE_STUB);
}
/**
* API method to fast & simple create multiple functions stubs.
*
* It does not allow to add expectations.
*
* The function name to create stub for can be passed as array key or as array value (with no
* key).
*
* When the function name is in the key, the value can be:
* - a callable, in which case the function will be aliased to it
* - anything else, in which case a stub returning given value will be created for the
* function
*
* When the function name is in the value, and no key is set, the behavior will change based on
* the second param:
* - when 2nd param is `null` (default) the created stub will return the 1st param it will
* receive
* - when 2nd param is anything else the created stub will return it
*
*
* @param array $functions
* @param mixed|null $default_return
*/
function stubs(array $functions, $default_return = null)
{
foreach ($functions as $key => $value) {
list($function_name, $return_value) = is_numeric($key)
? [$value, $default_return]
: [$key, $value];
if (is_callable($return_value)) {
when($function_name)->alias($return_value);
continue;
}
$return_value === null
? when($function_name)->returnArg()
: when($function_name)->justReturn($return_value);
}
}
/**
* API entry-point for plain functions expectations.
*
* Returns a Mockery Expectation object, where is possible to set all the expectations, using
* Mockery methods.
*
* @param string $function_name
* @return \Brain\Monkey\Expectation\Expectation
*/
function expect($function_name)
{
$name = new FunctionName($function_name);
$expectation = Container::instance()
->expectationFactory()
->forFunctionExecuted($function_name);
$factory = Container::instance()->functionStubFactory();
if ( ! $factory->has($name)) {
$factory->create($name, FunctionStubFactory::SCOPE_EXPECTATION)
->redefineUsingExpectation($expectation);
}
return $expectation;
}
/**
* Stub translation functions.
*
* @see EscapeHelper
*/
function stubTranslationFunctions()
{
stubs(
[
'__',
'_x',
'translate',
'_n' => static function($single, $plural, $number) {
return ($number === 1) ? $single : $plural;
},
'_nx' => static function($single, $plural, $number) {
return ($number === 1) ? $single : $plural;
},
'esc_html__' => [EscapeHelper::class, 'esc'],
'esc_html_x' => [EscapeHelper::class, 'esc'],
'esc_attr__' => [EscapeHelper::class, 'esc'],
'esc_attr_x' => [EscapeHelper::class, 'esc'],
'esc_html_e' => [EscapeHelper::class, 'escAndEcho'],
'esc_attr_e' => [EscapeHelper::class, 'escAndEcho'],
]
);
when('_e')->echoArg();
when('_ex')->echoArg();
}
/**
* Stub escape functions with default behavior.
*
* @see EscapeHelper
*/
function stubEscapeFunctions()
{
stubs(
[
'esc_js' => [EscapeHelper::class, 'esc'],
'esc_sql' => 'addslashes',
'esc_attr' => [EscapeHelper::class, 'esc'],
'esc_html' => [EscapeHelper::class, 'esc'],
'esc_textarea' => [EscapeHelper::class, 'esc'],
'esc_url' => [EscapeHelper::class, 'escUrl'],
'esc_url_raw' => [EscapeHelper::class, 'escUrlRaw'],
'esc_xml' => [EscapeHelper::class, 'escXml'],
]
);
}
}
namespace Brain\Monkey\Actions {
use Brain\Monkey\Container;
use Brain\Monkey\Hook;
/**
* API entry-point for added action expectations.
*
* Takes the action name and returns a Mockery Expectation object, where is possible to set all
* the expectations, using Mockery methods.
*
* @param string $action
* @return \Brain\Monkey\Expectation\Expectation
*/
function expectAdded($action)
{
return Container::instance()
->expectationFactory()
->forActionAdded($action);
}
/**
* API entry-point for fired action expectations.
*
* Takes the action name and returns a Mockery Expectation object, where is possible to set all
* the expectations, using Mockery methods.
*
* @param string $action
* @return \Brain\Monkey\Expectation\Expectation
*/
function expectDone($action)
{
return Container::instance()
->expectationFactory()
->forActionDone($action);
}
/**
* Utility method to check if any or specific callback has been added to given action.
*
* Brain Monkey version of `has_action` will alias here.
*
* @param string $action
* @param null $callback
* @return bool
*/
function has($action, $callback = null)
{
$type = Hook\HookStorage::ACTIONS;
$hookStorage = Container::instance()->hookStorage();
if ($callback === null) {
return $hookStorage->isHookAdded($type, $action);
}
return $hookStorage->hookPriority($type, $action, $callback);
}
/**
* Utility method to check if given action has been done.
*
* Brain Monkey version of `did_action` will alias here.
*
* @param string $action
* @return int
*/
function did($action)
{
return Container::instance()
->hookStorage()
->isHookDone(Hook\HookStorage::ACTIONS, $action);
}
/**
* Utility method to check if given action is currently being done.
*
* Brain Monkey version of `doing_action` will alias here.
*
* @param string $action
* @return bool
*/
function doing($action)
{
return Container::instance()
->hookRunningStack()
->has($action);
}
/**
* API entry-point for removed action expectations.
*
* Takes the action name and returns a Mockery Expectation object, where is possible to set all
* the expectations, using Mockery methods.
*
* @param string $action
* @return \Brain\Monkey\Expectation\Expectation
*/
function expectRemoved($action)
{
return Container::instance()
->expectationFactory()
->forActionRemoved($action);
}
}
namespace Brain\Monkey\Filters {
use Brain\Monkey\Container;
use Brain\Monkey\Hook;
/**
* API entry-point for added filter expectations.
*
* Takes the filter name and returns a Mockery Expectation object, where is possible to set all
* the expectations, using Mockery methods.
*
* @param string $filter
* @return \Brain\Monkey\Expectation\Expectation
*/
function expectAdded($filter)
{
return Container::instance()
->expectationFactory()
->forFilterAdded($filter);
}
/**
* API entry-point for applied filter expectations.
*
* Takes the filter name and returns a Mockery Expectation object, where is possible to set all
* the expectations, using Mockery methods.
*
* @param string $filter
* @return \Brain\Monkey\Expectation\Expectation
*/
function expectApplied($filter)
{
return Container::instance()
->expectationFactory()
->forFilterApplied($filter);
}
/**
* Utility method to check if any or specific callback has been added to given filter.
*
* Brain Monkey version of `has_filter` will alias here.
*
* @param string $filter
* @param null $callback
* @return bool|int If callback is omitted, returns boolean for whether the hook has anything registered.
* When checking a specific callback, the priority of that hook is returned,
* or false if the callback is not attached.
*/
function has($filter, $callback = null)
{
$type = Hook\HookStorage::FILTERS;
$hookStorage = Container::instance()->hookStorage();
if ($callback === null) {
return $hookStorage->isHookAdded($type, $filter);
}
return $hookStorage->hookPriority($type, $filter, $callback);
}
/**
* Utility method to check if given filter as been applied.
*
* There's no WordPress function counter part for it.
*
* @param string $filter
* @return int
*/
function applied($filter)
{
return Container::instance()
->hookStorage()
->isHookDone(Hook\HookStorage::FILTERS, $filter);
}
/**
* Utility method to check if given filter is currently being done.
*
* Brain Monkey version of `doing_filter` will alias here.
*
* @param string $filter
* @return bool
*/
function doing($filter)
{
return Container::instance()
->hookRunningStack()
->has($filter);
}
/**
* API entry-point for removed action expectations.
*
* Takes the action name and returns a Mockery Expectation object, where is possible to set all
* the expectations, using Mockery methods.
*
* @param string $filter
* @return \Brain\Monkey\Expectation\Expectation
*/
function expectRemoved($filter)
{
return Container::instance()
->expectationFactory()
->forFilterRemoved($filter);
}
}