-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathCacheableAPIBaseTest.php
More file actions
516 lines (399 loc) · 15.5 KB
/
CacheableAPIBaseTest.php
File metadata and controls
516 lines (399 loc) · 15.5 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<?php
use SkyVerge\WooCommerce\PluginFramework\v5_15_4 as Framework;
use SkyVerge\WooCommerce\PluginFramework\v5_15_4\API\Abstract_Cacheable_API_Base;
use SkyVerge\WooCommerce\PluginFramework\v5_15_4\API\Traits\Cacheable_Request_Trait;
use SkyVerge\WooCommerce\PluginFramework\v5_15_4\SV_WC_API_JSON_Request;
use SkyVerge\WooCommerce\PluginFramework\v5_15_4\SV_WC_API_Request;
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', true );
}
class CacheableAPIBaseTest extends \Codeception\TestCase\WPTestCase {
/**
* Tests {@see Framework\API\Abstract_Cacheable_API_Base::do_remote_request()}.
*
* @dataProvider provider_do_remote_request
*
* @param bool $is_cacheable
* @param bool|null $force_refresh
* @param bool|null $cache_exists
* @param bool $should_load_from_cache
*
* @throws ReflectionException
*/
public function test_do_remote_request( bool $is_cacheable, bool $force_refresh = null, bool $cache_exists = null, bool $should_load_from_cache = false ) {
$request = $this->get_new_request_instance( $is_cacheable );
if ( $is_cacheable ) {
$request->set_force_refresh( $force_refresh );
}
$api = $this->get_new_api_instance_with_request( $request, [ 'load_response_from_cache' ] );
$api->method( 'load_response_from_cache' )->willReturn( $cache_exists ? [ 'foo' => 'bar' ] : null );
$loaded_from_cache = new ReflectionProperty( get_class( $api ), 'response_loaded_from_cache' );
$loaded_from_cache->setAccessible( true );
$method = new ReflectionMethod( get_class( $api ), 'do_remote_request' );
$method->setAccessible( true );
$method->invoke( $api, 'foo', [] );
$this->assertEquals( $should_load_from_cache, $loaded_from_cache->getValue( $api ) );
}
/**
* Data provider for {@see CacheableAPIBaseTest::test_do_remote_request()}.
*
* @return array[]
*/
public function provider_do_remote_request(): array {
return [
'cacheable, no refresh, cache exists' => [ true, false, true, true ],
'cacheable, no refresh, cache does not exist' => [ true, false, false, false ],
'cacheable, force refresh, cache exists' => [ true, true, true, false ],
'cacheable, force refresh, cache does not exist' => [ true, true, false, false ],
'non-cacheable' => [ false, null, null, false ],
];
}
/**
* Tests {@see Framework\API\Abstract_Cacheable_API_Base::handle_response()}.
*
* @dataProvider provider_handle_response
*
* @param bool $is_cacheable
* @param bool|null $loaded_from_cache
* @param bool $should_save_response_to_cache
*
* @throws ReflectionException
*/
public function test_handle_response( bool $is_cacheable, bool $loaded_from_cache = false, bool $should_save_response_to_cache = false ) {
$request = $this->get_new_request_instance( $is_cacheable );
$api = $this->get_new_api_instance_with_request( $request, [
'is_response_loaded_from_cache',
'get_response_handler',
'save_response_to_cache'
] );
$api->method( 'get_response_handler' )->willReturn( new stdClass );
$api->method( 'is_response_loaded_from_cache' )->willReturn( $loaded_from_cache );
$method = new ReflectionMethod( get_class( $api ), 'handle_response' );
$method->setAccessible( true );
$api->expects( $should_save_response_to_cache ? $this->once() : $this->never() )->method( 'save_response_to_cache' );
$method->invoke( $api, [] );
}
/**
* Data provider for {@see CacheableAPIBaseTest::test_handle_response()}.
*
* @return array[]
*/
public function provider_handle_response(): array {
return [
'cacheable, response loaded from cache' => [ true, true, false ],
'cacheable, response not loaded from cache' => [ true, false, true ],
'non-cacheable' => [ false, false, false ],
];
}
/**
* Tests {@see Framework\API\Abstract_Cacheable_API_Base::load_response_from_cache()}.
* @throws ReflectionException
*/
public function test_load_response_from_cache() {
$api = $this->get_new_api_instance_with_request(
$this->get_new_request_instance(),
[ 'get_request_transient_key' ]
);
$api->method( 'get_request_transient_key' )->willReturn( 'foo' );
set_transient( 'foo', 'bar' );
$method = new ReflectionMethod( get_class( $api ), 'load_response_from_cache' );
$method->setAccessible( true );
$this->assertEquals( 'bar', $method->invoke( $api ) );
}
/**
* Tests {@see Framework\API\Abstract_Cacheable_API_Base::save_response_to_cache()}.
* @throws ReflectionException
*/
public function test_save_response_to_cache() {
$api = $this->get_new_api_instance_with_request(
$this->get_new_request_instance(),
[ 'get_request_transient_key' ]
);
$api->method( 'get_request_transient_key' )->willReturn( 'foo' );
$method = new ReflectionMethod( get_class( $api ), 'save_response_to_cache' );
$method->setAccessible( true );
$data = [ 'bar' => 'baz' ];
$method->invoke( $api, $data );
$this->assertEquals( get_transient( 'foo' ), $data );
$this->assertNotFalse( get_option( '_transient_timeout_foo' ) ); // ensure a timeout was set
}
/**
* Tests {@see Framework\API\Abstract_Cacheable_API_Base::is_response_loaded_from_cache()}.
* @throws ReflectionException
*/
public function test_is_response_loaded_from_cache() {
$api = $this->get_new_api_instance();
$property = new ReflectionProperty( get_class( $api ), 'response_loaded_from_cache' );
$property->setAccessible( true );
$method = new ReflectionMethod( get_class( $api ), 'is_response_loaded_from_cache' );
$method->setAccessible( true );
$method->invoke( $api );
$this->assertFalse( $method->invoke( $api ) );
$property->setValue( $api, true );
$this->assertTrue( $method->invoke( $api ) );
}
/**
* Tests {@see Framework\API\Abstract_Cacheable_API_Base::reset_response()}.
* @throws ReflectionException
*/
public function test_reset_response() {
$api = $this->get_new_api_instance();
$property = new ReflectionProperty( get_class( $api ), 'response_loaded_from_cache' );
$property->setAccessible( true );
$this->assertFalse( $property->getValue( $api ) );
$property->setValue( $api, true );
$this->assertTrue( $property->getValue( $api ) );
$method = new ReflectionMethod( get_class( $api ), 'reset_response' );
$method->setAccessible( true );
$method->invoke( $api );
$this->assertFalse( $property->getValue( $api ) );
}
/**
* Tests {@see Framework\API\Abstract_Cacheable_API_Base::get_request_transient_key()}.
*
* @dataProvider provider_get_request_transient_key
*
* @param string $uri request uri
* @param string $body request body
* @param int $lifetime request lifetime
*
* @throws ReflectionException
*/
public function test_get_request_transient_key( string $uri, string $body, int $lifetime ) {
$api = $this->get_new_api_instance_with_request(
$this->get_new_request_instance()->set_cache_lifetime( $lifetime ),
[ 'get_request_uri', 'get_request_body' ]
);
$api->method( 'get_request_uri' )->willReturn( $uri );
$api->method( 'get_request_body' )->willReturn( $body );
$method = new ReflectionMethod( get_class( $api ), 'get_request_transient_key' );
$method->setAccessible( true );
$this->assertEquals(
sprintf( 'wc_%s_api_response_%s', sv_wc_test_plugin()->get_id(), md5( implode( '_', [
$uri,
$body,
$lifetime,
] ) ) ),
$method->invoke( $api )
);
}
/**
* Data provider for {@see CacheableAPIBaseTest::_get_request_transient_key()}.
*
* @return array[]
*/
public function provider_get_request_transient_key(): array {
return [
[ 'foo', 'a=1', 100 ],
[ 'foo', 'a=2', 100 ],
[ 'foo', 'a=2', 200 ],
[ 'bar', '', 100 ],
[ 'bar/baz', '', 100 ],
];
}
/**
* Tests {@see Framework\API\Abstract_Cacheable_API_Base::is_request_cacheable()}.
*
* @dataProvider provider_is_request_cacheable
*
* @param bool $is_cacheable whether to test with a cacheable request
* @param null|bool $filter_value when provided, will filter is_cacheable with the given value
* @param bool $expected expected return value
*
* @throws ReflectionException
*/
public function test_is_request_cacheable( bool $is_cacheable, $filter_value = null, bool $expected ) {
$api = $this->get_new_api_instance_with_request( $this->get_new_request_instance( $is_cacheable ) );
if ( is_bool( $filter_value ) ) {
add_filter(
'wc_plugin_' . sv_wc_test_plugin()->get_id() . '_api_request_is_cacheable',
// the typehints in the closure ensure we're passing the correct arguments to the filter from `is_request_cacheable`
static function ( bool $is_cacheable, SV_WC_API_Request $request ) use ( $filter_value ) {
return $filter_value;
}, 10, 2 );
}
$method = new ReflectionMethod( get_class( $api ), 'is_request_cacheable' );
$method->setAccessible( true );
$this->assertEquals( $expected, $method->invoke( $api ) );
}
/**
* Data provider for {@see CacheableAPIBaseTest::test_is_request_cacheable()}.
*
* @return array[]
*/
public function provider_is_request_cacheable(): array {
return [
'cacheable request, no filtering' => [ true, null, true ],
'non-cacheable request, no filtering' => [ false, null, false ],
'cacheable request, filtering to false' => [ true, false, false ],
'non-cacheable request, filtering to true' => [ false, true, false ],
];
}
/**
* Tests {@see Framework\API\Abstract_Cacheable_API_Base::get_request_cache_lifetime()}.
*
* @dataProvider provider_get_request_cache_lifetime
*
* @param int $lifetime request cache lifetime
* @param null|int $filter_value when provided, will filter cache_lifetime with the given value
* @param int $expected expected return value
*
* @throws ReflectionException
*/
public function test_get_request_cache_lifetime( int $lifetime, $filter_value = null, int $expected ) {
$api = $this->get_new_api_instance_with_request( $this->get_new_request_instance()->set_cache_lifetime( $lifetime ) );
if ( is_int( $filter_value ) ) {
add_filter(
'wc_plugin_' . sv_wc_test_plugin()->get_id() . '_api_request_cache_lifetime',
// the typehints in the closure ensure we're passing the correct arguments to the filter from `get_request_cache_lifetime`
static function ( int $lifetime, SV_WC_API_Request $request ) use ( $filter_value ) {
return $filter_value;
}, 10, 2 );
}
$method = new ReflectionMethod( get_class( $api ), 'get_request_cache_lifetime' );
$method->setAccessible( true );
$this->assertEquals( $expected, $method->invoke( $api ) );
}
/**
* Data provider for {@see CacheableAPIBaseTest::test_get_request_cache_lifetime()}.
*
* @return array[]
*/
public function provider_get_request_cache_lifetime(): array {
return [
'non-filtered' => [ 100, null, 100 ],
'filtered' => [ 100, 200, 200 ],
];
}
/**
* Tests {@see Framework\API\Abstract_Cacheable_API_Base::get_request_data_for_broadcast()}.
*
* @dataProvider provider_get_request_data_for_broadcast
*
* @param bool $is_cacheable
* @param bool|null $force_refresh
* @param bool|null $should_cache
*
* @throws ReflectionException
*/
public function test_get_request_data_for_broadcast( bool $is_cacheable, bool $force_refresh = null, bool $should_cache = null ) {
$request = $this->get_new_request_instance( $is_cacheable );
if ( $is_cacheable ) {
$request->set_force_refresh( $force_refresh )->set_should_cache( $should_cache );
}
$api = $this->get_new_api_instance_with_request( $request );
$method = new ReflectionMethod( get_class( $api ), 'get_request_data_for_broadcast' );
$method->setAccessible( true );
$request_data = $method->invoke( $api );
if ( $is_cacheable ) {
$keys = array_keys( $request_data );
// ensure our keys are at the top of the array
$this->assertEquals( 'force_refresh', $keys[0] );
$this->assertEquals( 'should_cache', $keys[1] );
$this->assertEquals( 'method', $keys[2] );
$this->assertEquals( $force_refresh, $request_data['force_refresh'] );
$this->assertEquals( $should_cache, $request_data['should_cache'] );
} else {
$this->assertArrayNotHasKey( 'force_refresh', $request_data );
$this->assertArrayNotHasKey( 'should_cache', $request_data );
}
}
/**
* Data provider for {@see CacheableAPIBaseTest::test_get_request_data_for_broadcast()}.
*
* @return array[]
*/
public function provider_get_request_data_for_broadcast(): array {
return [
'cacheable, no refresh, should cache' => [ true, false, true ],
'cacheable, force refresh, should cache' => [ true, true, true ],
'cacheable, force refresh, no cache' => [ true, true, false ],
'non-cacheable' => [ false ],
];
}
/**
* Tests {@see Framework\API\Abstract_Cacheable_API_Base::get_response_data_for_broadcast()}.
*
* @dataProvider provider_get_response_data_for_broadcast
*
* @param bool $is_cacheable
* @param bool|null $cache_exists
* @param bool|null $force_refresh
* @param bool|null $expected_from_cache
*
* @throws ReflectionException
*/
public function test_get_response_data_for_broadcast( bool $is_cacheable, bool $response_loaded_from_cache = false ) {
$api = $this->get_new_api_instance_with_request(
$this->get_new_request_instance( $is_cacheable ),
[ 'is_response_loaded_from_cache' ]
);
$api->method( 'is_response_loaded_from_cache' )->willReturn( $response_loaded_from_cache );
$method = new ReflectionMethod( get_class( $api ), 'get_response_data_for_broadcast' );
$method->setAccessible( true );
$response_data = $method->invoke( $api );
if ( $is_cacheable ) {
$keys = array_keys( $response_data );
// ensure our keys are at the top of the array
$this->assertEquals( 'from_cache', $keys[0] );
$this->assertEquals( 'code', $keys[1] );
$this->assertEquals( $response_loaded_from_cache, $response_data['from_cache'] );
} else {
$this->assertArrayNotHasKey( 'from_cache', $response_data );
}
}
/**
* Data provider for {@see CacheableAPIBaseTest::test_get_response_data_for_broadcast()}.
*
* @return array[]
*/
public function provider_get_response_data_for_broadcast(): array {
return [
'cacheable, loading response from cache' => [ true, true ],
'cacheable, not loading response from cache' => [ true, false ],
'non-cacheable' => [ false ],
];
}
/**
* Gets a test request instance using the CacheableRequestTrait.
*
* @param bool $cacheable whether to return a cacheable or regular request
*/
protected function get_new_request_instance( bool $cacheable = true ) {
return $cacheable
? new class extends SV_WC_API_JSON_Request {
use Cacheable_Request_Trait;
}
: $this->getMockForAbstractClass( SV_WC_API_JSON_Request::class );
}
/**
* Gets a test API instance extending Abstract_Cacheable_API_Base class.
*
* @param array $mockMethods additional methods to mock on the class
*/
protected function get_new_api_instance( array $mockMethods = [] ) {
$api = $this->getMockForAbstractClass(
Abstract_Cacheable_API_Base::class,
[],
'',
true,
true,
true,
$mockMethods
);
$api->method( 'get_plugin' )->willReturn( sv_wc_test_plugin() );
return $api;
}
/**
* Gets a new API instance with the given request attached to it.
*
* @throws ReflectionException
*/
protected function get_new_api_instance_with_request( SV_WC_API_Request $request, array $mockApiMethods = [] ) {
$api = $this->get_new_api_instance( $mockApiMethods );
$property = new ReflectionProperty( get_class( $api ), 'request' );
$property->setAccessible( true );
$property->setValue( $api, $request );
return $api;
}
}