Skip to content

Commit eb6dadd

Browse files
committed
dry up tests
1 parent 6858440 commit eb6dadd

File tree

1 file changed

+42
-53
lines changed

1 file changed

+42
-53
lines changed

tests/integration/API/CacheableAPIBaseTest.php

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,15 @@ class CacheableAPIBaseTest extends \Codeception\TestCase\WPTestCase {
2727
*/
2828
public function test_do_remote_request( bool $is_cacheable, bool $force_refresh = null, bool $cache_exists = null, bool $should_load_from_cache = false ) {
2929

30-
$api = $this->get_new_api_instance( [ 'load_response_from_cache' ] );
3130
$request = $this->get_new_request_instance( $is_cacheable );
3231

3332
if ( $is_cacheable ) {
3433
$request->set_force_refresh( $force_refresh );
3534
}
3635

36+
$api = $this->get_new_api_instance_with_request( $request, [ 'load_response_from_cache' ] );
3737
$api->method( 'load_response_from_cache' )->willReturn( $cache_exists ? [ 'foo' => 'bar' ] : null );
3838

39-
$property = new ReflectionProperty( get_class( $api ), 'request' );
40-
$property->setAccessible( true );
41-
$property->setValue( $api, $request );
42-
4339
$loaded_from_cache = new ReflectionProperty( get_class( $api ), 'response_loaded_from_cache' );
4440
$loaded_from_cache->setAccessible( true );
4541

@@ -81,20 +77,16 @@ public function provider_do_remote_request(): array {
8177
*/
8278
public function test_handle_response( bool $is_cacheable, bool $loaded_from_cache = false, bool $should_save_response_to_cache = false ) {
8379

84-
$api = $this->get_new_api_instance( [
80+
$request = $this->get_new_request_instance( $is_cacheable );
81+
$api = $this->get_new_api_instance_with_request( $request, [
8582
'is_response_loaded_from_cache',
8683
'get_response_handler',
8784
'save_response_to_cache'
8885
] );
89-
$request = $this->get_new_request_instance( $is_cacheable );
9086

9187
$api->method( 'get_response_handler' )->willReturn( new stdClass );
9288
$api->method( 'is_response_loaded_from_cache' )->willReturn( $loaded_from_cache );
9389

94-
$property = new ReflectionProperty( get_class( $api ), 'request' );
95-
$property->setAccessible( true );
96-
$property->setValue( $api, $request );
97-
9890
$method = new ReflectionMethod( get_class( $api ), 'handle_response' );
9991
$method->setAccessible( true );
10092

@@ -124,12 +116,10 @@ public function provider_handle_response(): array {
124116
*/
125117
public function test_load_response_from_cache() {
126118

127-
$api = $this->get_new_api_instance( [ 'get_request_transient_key' ] );
128-
$request = $this->get_new_request_instance();
129-
130-
$property = new ReflectionProperty( get_class( $api ), 'request' );
131-
$property->setAccessible( true );
132-
$property->setValue( $api, $request );
119+
$api = $this->get_new_api_instance_with_request(
120+
$this->get_new_request_instance(),
121+
[ 'get_request_transient_key' ]
122+
);
133123

134124
$api->method( 'get_request_transient_key' )->willReturn( 'foo' );
135125

@@ -148,12 +138,10 @@ public function test_load_response_from_cache() {
148138
*/
149139
public function test_save_response_to_cache() {
150140

151-
$api = $this->get_new_api_instance( [ 'get_request_transient_key' ] );
152-
$request = $this->get_new_request_instance();
153-
154-
$property = new ReflectionProperty( get_class( $api ), 'request' );
155-
$property->setAccessible( true );
156-
$property->setValue( $api, $request );
141+
$api = $this->get_new_api_instance_with_request(
142+
$this->get_new_request_instance(),
143+
[ 'get_request_transient_key' ]
144+
);
157145

158146
$api->method( 'get_request_transient_key' )->willReturn( 'foo' );
159147

@@ -230,16 +218,14 @@ public function test_reset_response() {
230218
*/
231219
public function test_get_request_transient_key( string $uri, string $body, int $lifetime ) {
232220

233-
$api = $this->get_new_api_instance( [ 'get_request_uri', 'get_request_body' ] );
234-
$request = $this->get_new_request_instance()->set_cache_lifetime( $lifetime );
221+
$api = $this->get_new_api_instance_with_request(
222+
$this->get_new_request_instance()->set_cache_lifetime( $lifetime ),
223+
[ 'get_request_uri', 'get_request_body' ]
224+
);
235225

236226
$api->method( 'get_request_uri' )->willReturn( $uri );
237227
$api->method( 'get_request_body' )->willReturn( $body );
238228

239-
$property = new ReflectionProperty( get_class( $api ), 'request' );
240-
$property->setAccessible( true );
241-
$property->setValue( $api, $request );
242-
243229
$method = new ReflectionMethod( get_class( $api ), 'get_request_transient_key' );
244230
$method->setAccessible( true );
245231

@@ -275,19 +261,15 @@ public function provider_get_request_transient_key(): array {
275261
*
276262
* @dataProvider provider_is_request_cacheable
277263
*
278-
* @param bool $cacheable whether to test with a cacheable request
264+
* @param bool $is_cacheable whether to test with a cacheable request
279265
* @param null|bool $filter_value when provided, will filter is_cacheable with the given value
280266
* @param bool $expected expected return value
281267
*
282268
* @throws ReflectionException
283269
*/
284-
public function test_is_request_cacheable( bool $cacheable, $filter_value = null, bool $expected ) {
270+
public function test_is_request_cacheable( bool $is_cacheable, $filter_value = null, bool $expected ) {
285271

286-
$api = $this->get_new_api_instance();
287-
288-
$property = new ReflectionProperty( get_class( $api ), 'request' );
289-
$property->setAccessible( true );
290-
$property->setValue( $api, $this->get_new_request_instance( $cacheable ) );
272+
$api = $this->get_new_api_instance_with_request( $this->get_new_request_instance( $is_cacheable ) );
291273

292274
if ( is_bool( $filter_value ) ) {
293275
add_filter(
@@ -333,17 +315,12 @@ public function provider_is_request_cacheable(): array {
333315
*/
334316
public function test_get_request_cache_lifetime( int $lifetime, $filter_value = null, int $expected ) {
335317

336-
$api = $this->get_new_api_instance();
337-
$request = $this->get_new_request_instance()->set_cache_lifetime( $lifetime );
338-
339-
$property = new ReflectionProperty( get_class( $api ), 'request' );
340-
$property->setAccessible( true );
341-
$property->setValue( $api, $request );
318+
$api = $this->get_new_api_instance_with_request( $this->get_new_request_instance()->set_cache_lifetime( $lifetime ) );
342319

343320
if ( is_int( $filter_value ) ) {
344321
add_filter(
345322
'wc_plugin_' . sv_wc_test_plugin()->get_id() . '_api_request_cache_lifetime',
346-
// the typehints in the closure ensure we're passing the correct arguments to the filter from `is_request_cacheable`
323+
// the typehints in the closure ensure we're passing the correct arguments to the filter from `get_request_cache_lifetime`
347324
static function ( int $lifetime, SV_WC_API_Request $request ) use ( $filter_value ) {
348325
return $filter_value;
349326
}, 10, 2 );
@@ -382,16 +359,13 @@ public function provider_get_request_cache_lifetime(): array {
382359
*/
383360
public function test_get_request_data_for_broadcast( bool $is_cacheable, bool $force_refresh = null, bool $should_cache = null ) {
384361

385-
$api = $this->get_new_api_instance();
386362
$request = $this->get_new_request_instance( $is_cacheable );
387363

388364
if ( $is_cacheable ) {
389365
$request->set_force_refresh( $force_refresh )->set_should_cache( $should_cache );
390366
}
391367

392-
$property = new ReflectionProperty( get_class( $api ), 'request' );
393-
$property->setAccessible( true );
394-
$property->setValue( $api, $request );
368+
$api = $this->get_new_api_instance_with_request( $request );
395369

396370
$method = new ReflectionMethod( get_class( $api ), 'get_request_data_for_broadcast' );
397371
$method->setAccessible( true );
@@ -445,15 +419,13 @@ public function provider_get_request_data_for_broadcast(): array {
445419
*/
446420
public function test_get_response_data_for_broadcast( bool $is_cacheable, bool $response_loaded_from_cache = false ) {
447421

448-
$api = $this->get_new_api_instance( [ 'is_response_loaded_from_cache' ] );
449-
$request = $this->get_new_request_instance( $is_cacheable );
422+
$api = $this->get_new_api_instance_with_request(
423+
$this->get_new_request_instance( $is_cacheable ),
424+
[ 'is_response_loaded_from_cache' ]
425+
);
450426

451427
$api->method( 'is_response_loaded_from_cache' )->willReturn( $response_loaded_from_cache );
452428

453-
$property = new ReflectionProperty( get_class( $api ), 'request' );
454-
$property->setAccessible( true );
455-
$property->setValue( $api, $request );
456-
457429
$method = new ReflectionMethod( get_class( $api ), 'get_response_data_for_broadcast' );
458430
$method->setAccessible( true );
459431

@@ -523,5 +495,22 @@ protected function get_new_api_instance( array $mockMethods = [] ) {
523495

524496
return $api;
525497
}
498+
499+
500+
/**
501+
* Gets a new API instance with the given request attached to it.
502+
*
503+
* @throws ReflectionException
504+
*/
505+
protected function get_new_api_instance_with_request( SV_WC_API_Request $request, array $mockApiMethods = [] ) {
506+
507+
$api = $this->get_new_api_instance( $mockApiMethods );
508+
509+
$property = new ReflectionProperty( get_class( $api ), 'request' );
510+
$property->setAccessible( true );
511+
$property->setValue( $api, $request );
512+
513+
return $api;
514+
}
526515
}
527516

0 commit comments

Comments
 (0)