11<?php
22
33use SkyVerge \WooCommerce \PluginFramework \v5_10_10 as Framework ;
4- use SkyVerge \WooCommerce \PluginFramework \v5_10_10 \SV_WC_API_JSON_Request ;
54use SkyVerge \WooCommerce \PluginFramework \v5_10_10 \API \Abstract_Cacheable_API_Base ;
65use SkyVerge \WooCommerce \PluginFramework \v5_10_10 \API \Traits \Cacheable_Request_Trait ;
6+ use SkyVerge \WooCommerce \PluginFramework \v5_10_10 \SV_WC_API_JSON_Request ;
77use SkyVerge \WooCommerce \PluginFramework \v5_10_10 \SV_WC_API_Request ;
88
99if ( ! defined ( 'ABSPATH ' ) ) {
@@ -22,18 +22,19 @@ class CacheableAPIBaseTest extends \Codeception\TestCase\WPTestCase {
2222 * @param bool|null $force_refresh
2323 * @param bool|null $cache_exists
2424 * @param bool $should_load_from_cache
25+ *
2526 * @throws ReflectionException
2627 */
2728 public function test_do_remote_request ( bool $ is_cacheable , bool $ force_refresh = null , bool $ cache_exists = null , bool $ should_load_from_cache = false ) {
2829
29- $ api = $ this ->get_new_api_instance ([ 'load_response_from_cache ' ] );
30+ $ api = $ this ->get_new_api_instance ( [ 'load_response_from_cache ' ] );
3031 $ request = $ this ->get_new_request_instance ( $ is_cacheable );
3132
3233 if ( $ is_cacheable ) {
3334 $ request ->set_force_refresh ( $ force_refresh );
3435 }
3536
36- $ api ->method ('load_response_from_cache ' )->willReturn ( $ cache_exists ? ['foo ' => 'bar ' ] : null );
37+ $ api ->method ( 'load_response_from_cache ' )->willReturn ( $ cache_exists ? [ 'foo ' => 'bar ' ] : null );
3738
3839 $ property = new ReflectionProperty ( get_class ( $ api ), 'request ' );
3940 $ property ->setAccessible ( true );
@@ -56,13 +57,13 @@ public function test_do_remote_request( bool $is_cacheable, bool $force_refresh
5657 *
5758 * @return array[]
5859 */
59- public function provider_do_remote_request () : array {
60+ public function provider_do_remote_request (): array {
6061 return [
61- 'cacheable, no refresh, cache exists ' => [true , false , true , true ],
62- 'cacheable, no refresh, cache does not exist ' => [true , false , false , false ],
63- 'cacheable, force refresh, cache exists ' => [true , true , true , false ],
64- 'cacheable, force refresh, cache does not exist ' => [true , true , false , false ],
65- 'non-cacheable ' => [false , null , null , false ],
62+ 'cacheable, no refresh, cache exists ' => [ true , false , true , true ],
63+ 'cacheable, no refresh, cache does not exist ' => [ true , false , false , false ],
64+ 'cacheable, force refresh, cache exists ' => [ true , true , true , false ],
65+ 'cacheable, force refresh, cache does not exist ' => [ true , true , false , false ],
66+ 'non-cacheable ' => [ false , null , null , false ],
6667 ];
6768 }
6869
@@ -75,15 +76,20 @@ public function provider_do_remote_request() : array {
7576 * @param bool $is_cacheable
7677 * @param bool|null $loaded_from_cache
7778 * @param bool $should_save_response_to_cache
79+ *
7880 * @throws ReflectionException
7981 */
8082 public function test_handle_response ( bool $ is_cacheable , bool $ loaded_from_cache = false , bool $ should_save_response_to_cache = false ) {
8183
82- $ api = $ this ->get_new_api_instance (['is_response_loaded_from_cache ' , 'get_response_handler ' , 'save_response_to_cache ' ]);
84+ $ api = $ this ->get_new_api_instance ( [
85+ 'is_response_loaded_from_cache ' ,
86+ 'get_response_handler ' ,
87+ 'save_response_to_cache '
88+ ] );
8389 $ request = $ this ->get_new_request_instance ( $ is_cacheable );
8490
85- $ api ->method ('get_response_handler ' )->willReturn ( new stdClass );
86- $ api ->method ('is_response_loaded_from_cache ' )->willReturn ( $ loaded_from_cache );
91+ $ api ->method ( 'get_response_handler ' )->willReturn ( new stdClass );
92+ $ api ->method ( 'is_response_loaded_from_cache ' )->willReturn ( $ loaded_from_cache );
8793
8894 $ property = new ReflectionProperty ( get_class ( $ api ), 'request ' );
8995 $ property ->setAccessible ( true );
@@ -103,11 +109,11 @@ public function test_handle_response( bool $is_cacheable, bool $loaded_from_cach
103109 *
104110 * @return array[]
105111 */
106- public function provider_handle_response () : array {
112+ public function provider_handle_response (): array {
107113 return [
108- 'cacheable, response loaded from cache ' => [true , true , false ],
109- 'cacheable, response not loaded from cache ' => [true , false , true ],
110- 'non-cacheable ' => [false , false , false ],
114+ 'cacheable, response loaded from cache ' => [ true , true , false ],
115+ 'cacheable, response not loaded from cache ' => [ true , false , true ],
116+ 'non-cacheable ' => [ false , false , false ],
111117 ];
112118 }
113119
@@ -118,14 +124,14 @@ public function provider_handle_response() : array {
118124 */
119125 public function test_load_response_from_cache () {
120126
121- $ api = $ this ->get_new_api_instance ([ 'get_request_transient_key ' ] );
127+ $ api = $ this ->get_new_api_instance ( [ 'get_request_transient_key ' ] );
122128 $ request = $ this ->get_new_request_instance ();
123129
124130 $ property = new ReflectionProperty ( get_class ( $ api ), 'request ' );
125131 $ property ->setAccessible ( true );
126132 $ property ->setValue ( $ api , $ request );
127133
128- $ api ->method ('get_request_transient_key ' )->willReturn ( 'foo ' );
134+ $ api ->method ( 'get_request_transient_key ' )->willReturn ( 'foo ' );
129135
130136 set_transient ( 'foo ' , 'bar ' );
131137
@@ -142,19 +148,19 @@ public function test_load_response_from_cache() {
142148 */
143149 public function test_save_response_to_cache () {
144150
145- $ api = $ this ->get_new_api_instance ([ 'get_request_transient_key ' ] );
151+ $ api = $ this ->get_new_api_instance ( [ 'get_request_transient_key ' ] );
146152 $ request = $ this ->get_new_request_instance ();
147153
148154 $ property = new ReflectionProperty ( get_class ( $ api ), 'request ' );
149155 $ property ->setAccessible ( true );
150156 $ property ->setValue ( $ api , $ request );
151157
152- $ api ->method ('get_request_transient_key ' )->willReturn ( 'foo ' );
158+ $ api ->method ( 'get_request_transient_key ' )->willReturn ( 'foo ' );
153159
154160 $ method = new ReflectionMethod ( get_class ( $ api ), 'save_response_to_cache ' );
155161 $ method ->setAccessible ( true );
156162
157- $ data = ['bar ' => 'baz ' ];
163+ $ data = [ 'bar ' => 'baz ' ];
158164
159165 $ method ->invoke ( $ api , $ data );
160166
@@ -219,15 +225,16 @@ public function test_reset_response() {
219225 * @param string $uri request uri
220226 * @param string $body request body
221227 * @param int $lifetime request lifetime
228+ *
222229 * @throws ReflectionException
223230 */
224231 public function test_get_request_transient_key ( string $ uri , string $ body , int $ lifetime ) {
225232
226- $ api = $ this ->get_new_api_instance ([ 'get_request_uri ' , 'get_request_body ' ] );
233+ $ api = $ this ->get_new_api_instance ( [ 'get_request_uri ' , 'get_request_body ' ] );
227234 $ request = $ this ->get_new_request_instance ()->set_cache_lifetime ( $ lifetime );
228235
229- $ api ->method ('get_request_uri ' )->willReturn ( $ uri );
230- $ api ->method ('get_request_body ' )->willReturn ( $ body );
236+ $ api ->method ( 'get_request_uri ' )->willReturn ( $ uri );
237+ $ api ->method ( 'get_request_body ' )->willReturn ( $ body );
231238
232239 $ property = new ReflectionProperty ( get_class ( $ api ), 'request ' );
233240 $ property ->setAccessible ( true );
@@ -252,13 +259,13 @@ public function test_get_request_transient_key( string $uri, string $body, int $
252259 *
253260 * @return array[]
254261 */
255- public function provider_get_request_transient_key () : array {
262+ public function provider_get_request_transient_key (): array {
256263 return [
257- ['foo ' , 'a=1 ' , 100 ],
258- ['foo ' , 'a=2 ' , 100 ],
259- ['foo ' , 'a=2 ' , 200 ],
260- ['bar ' , '' , 100 ],
261- ['bar/baz ' , '' , 100 ],
264+ [ 'foo ' , 'a=1 ' , 100 ],
265+ [ 'foo ' , 'a=2 ' , 100 ],
266+ [ 'foo ' , 'a=2 ' , 200 ],
267+ [ 'bar ' , '' , 100 ],
268+ [ 'bar/baz ' , '' , 100 ],
262269 ];
263270 }
264271
@@ -271,6 +278,7 @@ public function provider_get_request_transient_key() : array {
271278 * @param bool $cacheable whether to test with a cacheable request
272279 * @param null|bool $filter_value when provided, will filter is_cacheable with the given value
273280 * @param bool $expected expected return value
281+ *
274282 * @throws ReflectionException
275283 */
276284 public function test_is_request_cacheable ( bool $ cacheable , $ filter_value = null , bool $ expected ) {
@@ -285,7 +293,7 @@ public function test_is_request_cacheable( bool $cacheable, $filter_value = null
285293 add_filter (
286294 'wc_plugin_ ' . sv_wc_test_plugin ()->get_id () . '_api_request_is_cacheable ' ,
287295 // the typehints in the closure ensure we're passing the correct arguments to the filter from `is_request_cacheable`
288- static function ( bool $ is_cacheable , SV_WC_API_Request $ request ) use ( $ filter_value ) {
296+ static function ( bool $ is_cacheable , SV_WC_API_Request $ request ) use ( $ filter_value ) {
289297 return $ filter_value ;
290298 }, 10 , 2 );
291299 }
@@ -302,12 +310,12 @@ static function( bool $is_cacheable, SV_WC_API_Request $request ) use ( $filter_
302310 *
303311 * @return array[]
304312 */
305- public function provider_is_request_cacheable () : array {
313+ public function provider_is_request_cacheable (): array {
306314 return [
307- 'cacheable request, no filtering ' => [true , null , true ],
308- 'non-cacheable request, no filtering ' => [false , null , false ],
309- 'cacheable request, filtering to false ' => [true , false , false ],
310- 'non-cacheable request, filtering to true ' => [false , true , false ],
315+ 'cacheable request, no filtering ' => [ true , null , true ],
316+ 'non-cacheable request, no filtering ' => [ false , null , false ],
317+ 'cacheable request, filtering to false ' => [ true , false , false ],
318+ 'non-cacheable request, filtering to true ' => [ false , true , false ],
311319 ];
312320 }
313321
@@ -320,6 +328,7 @@ public function provider_is_request_cacheable() : array {
320328 * @param int $lifetime request cache lifetime
321329 * @param null|int $filter_value when provided, will filter cache_lifetime with the given value
322330 * @param int $expected expected return value
331+ *
323332 * @throws ReflectionException
324333 */
325334 public function test_get_request_cache_lifetime ( int $ lifetime , $ filter_value = null , int $ expected ) {
@@ -335,7 +344,7 @@ public function test_get_request_cache_lifetime( int $lifetime, $filter_value =
335344 add_filter (
336345 'wc_plugin_ ' . sv_wc_test_plugin ()->get_id () . '_api_request_cache_lifetime ' ,
337346 // the typehints in the closure ensure we're passing the correct arguments to the filter from `is_request_cacheable`
338- static function ( int $ lifetime , SV_WC_API_Request $ request ) use ( $ filter_value ) {
347+ static function ( int $ lifetime , SV_WC_API_Request $ request ) use ( $ filter_value ) {
339348 return $ filter_value ;
340349 }, 10 , 2 );
341350 }
@@ -352,10 +361,10 @@ static function( int $lifetime, SV_WC_API_Request $request ) use ( $filter_value
352361 *
353362 * @return array[]
354363 */
355- public function provider_get_request_cache_lifetime () : array {
364+ public function provider_get_request_cache_lifetime (): array {
356365 return [
357- 'non-filtered ' => [100 , null , 100 ],
358- 'filtered ' => [100 , 200 , 200 ],
366+ 'non-filtered ' => [ 100 , null , 100 ],
367+ 'filtered ' => [ 100 , 200 , 200 ],
359368 ];
360369 }
361370
@@ -368,9 +377,10 @@ public function provider_get_request_cache_lifetime() : array {
368377 * @param bool $is_cacheable
369378 * @param bool|null $force_refresh
370379 * @param bool|null $should_cache
380+ *
371381 * @throws ReflectionException
372382 */
373- public function test_get_request_data_for_broadcast (bool $ is_cacheable , bool $ force_refresh = null , bool $ should_cache = null ) {
383+ public function test_get_request_data_for_broadcast ( bool $ is_cacheable , bool $ force_refresh = null , bool $ should_cache = null ) {
374384
375385 $ api = $ this ->get_new_api_instance ();
376386 $ request = $ this ->get_new_request_instance ( $ is_cacheable );
@@ -411,12 +421,12 @@ public function test_get_request_data_for_broadcast(bool $is_cacheable, bool $fo
411421 *
412422 * @return array[]
413423 */
414- public function provider_get_request_data_for_broadcast () : array {
424+ public function provider_get_request_data_for_broadcast (): array {
415425 return [
416- 'cacheable, no refresh, should cache ' => [true , false , true ],
417- 'cacheable, force refresh, should cache ' => [true , true , true ],
418- 'cacheable, force refresh, no cache ' => [true , true , false ],
419- 'non-cacheable ' => [false ],
426+ 'cacheable, no refresh, should cache ' => [ true , false , true ],
427+ 'cacheable, force refresh, should cache ' => [ true , true , true ],
428+ 'cacheable, force refresh, no cache ' => [ true , true , false ],
429+ 'non-cacheable ' => [ false ],
420430 ];
421431 }
422432
@@ -430,6 +440,7 @@ public function provider_get_request_data_for_broadcast() : array {
430440 * @param bool|null $cache_exists
431441 * @param bool|null $force_refresh
432442 * @param bool|null $expected_from_cache
443+ *
433444 * @throws ReflectionException
434445 */
435446 public function test_get_response_data_for_broadcast ( bool $ is_cacheable , bool $ response_loaded_from_cache = false ) {
@@ -468,11 +479,11 @@ public function test_get_response_data_for_broadcast( bool $is_cacheable, bool $
468479 *
469480 * @return array[]
470481 */
471- public function provider_get_response_data_for_broadcast () : array {
482+ public function provider_get_response_data_for_broadcast (): array {
472483 return [
473- 'cacheable, loading response from cache ' => [true , true ],
474- 'cacheable, not loading response from cache ' => [true , false ],
475- 'non-cacheable ' => [false ],
484+ 'cacheable, loading response from cache ' => [ true , true ],
485+ 'cacheable, not loading response from cache ' => [ true , false ],
486+ 'non-cacheable ' => [ false ],
476487 ];
477488 }
478489
@@ -502,13 +513,13 @@ protected function get_new_api_instance( array $mockMethods = [] ) {
502513 Abstract_Cacheable_API_Base::class,
503514 [],
504515 '' ,
505- true ,
506- true ,
507- true ,
516+ true ,
517+ true ,
518+ true ,
508519 $ mockMethods
509520 );
510521
511- $ api ->method ('get_plugin ' )->willReturn ( sv_wc_test_plugin () );
522+ $ api ->method ( 'get_plugin ' )->willReturn ( sv_wc_test_plugin () );
512523
513524 return $ api ;
514525 }
0 commit comments