Skip to content

Commit 7f21f6d

Browse files
committed
Add test for handle_response
1 parent 30c65b6 commit 7f21f6d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tests/integration/API/CacheableAPIBaseTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,51 @@ public function provider_do_remote_request() : array {
6767
}
6868

6969

70+
/**
71+
* Tests {@see Framework\API\Abstract_Cacheable_API_Base::handle_response()}.
72+
*
73+
* @dataProvider provider_handle_response
74+
*
75+
* @param bool $is_cacheable
76+
* @param bool|null $loaded_from_cache
77+
* @param bool $should_save_response_to_cache
78+
* @throws ReflectionException
79+
*/
80+
public function test_handle_response( bool $is_cacheable, bool $loaded_from_cache = false, bool $should_save_response_to_cache = false ) {
81+
82+
$api = $this->get_new_api_instance(['is_response_loaded_from_cache', 'get_response_handler', 'save_response_to_cache']);
83+
$request = $this->get_new_request_instance( $is_cacheable );
84+
85+
$api->method('get_response_handler')->willReturn( new stdClass );
86+
$api->method('is_response_loaded_from_cache')->willReturn( $loaded_from_cache );
87+
88+
$property = new ReflectionProperty( get_class( $api ), 'request' );
89+
$property->setAccessible( true );
90+
$property->setValue( $api, $request );
91+
92+
$method = new ReflectionMethod( get_class( $api ), 'handle_response' );
93+
$method->setAccessible( true );
94+
95+
$api->expects( $should_save_response_to_cache ? $this->once() : $this->never() )->method( 'save_response_to_cache' );
96+
97+
$method->invoke( $api, [] );
98+
}
99+
100+
101+
/**
102+
* Data provider for {@see CacheableAPIBaseTest::test_handle_response()}.
103+
*
104+
* @return array[]
105+
*/
106+
public function provider_handle_response() : array {
107+
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],
111+
];
112+
}
113+
114+
70115
/**
71116
* Tests {@see Framework\API\Abstract_Cacheable_API_Base::load_response_from_cache()}.
72117
* @throws ReflectionException

0 commit comments

Comments
 (0)