Skip to content

Commit 9ce786b

Browse files
committed
🧪 Add fallback filter test and stub apply_filters on cache hit
1 parent e498452 commit 9ce786b

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

‎tests/PHPUnit/ApiClient/Endpoint/PartnersEndpointTest.php‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ public function testSellerStatusReturnsCachedValueWithoutHttpCall(): void
124124
->with(PartnersEndpoint::SELLER_STATUS_CACHE_KEY)
125125
->andReturn($cached_status);
126126

127+
when('apply_filters')->alias(function (string $hook, ...$args) {
128+
return $args[0] ?? null;
129+
});
130+
127131
// wp_remote_get must not be called — any invocation will cause the
128132
// Brain Monkey expectation to fail with an unexpected call.
129133
expect('wp_remote_get')->never();
@@ -211,6 +215,58 @@ public function testSellerStatusOnApiErrorRegistersFailureAndThrows(): void
211215
$this->make_endpoint()->seller_status();
212216
}
213217

218+
// -----------------------------------------------------------------------
219+
// Tests: seller_status() fallback filter
220+
// -----------------------------------------------------------------------
221+
222+
/**
223+
* GIVEN the transient cache is empty
224+
* AND the PayPal API returns a non-200 status code
225+
* AND the fallback filter returns a SellerStatus
226+
* WHEN seller_status() is called
227+
* THEN the fallback is used instead of throwing
228+
* AND the result is stored in the cache
229+
*/
230+
public function testSellerStatusOnApiErrorUsesFallbackFilter(): void
231+
{
232+
$fallback = Mockery::mock(SellerStatus::class);
233+
$raw_response = $this->make_raw_response(500);
234+
235+
$this->cache
236+
->shouldReceive('get')
237+
->once()
238+
->with(PartnersEndpoint::SELLER_STATUS_CACHE_KEY)
239+
->andReturn(false);
240+
241+
$this->cache
242+
->shouldReceive('set')
243+
->once()
244+
->with(
245+
PartnersEndpoint::SELLER_STATUS_CACHE_KEY,
246+
$fallback,
247+
PartnersEndpoint::SELLER_STATUS_CACHE_TTL
248+
);
249+
250+
$this->failure_registry
251+
->shouldReceive('add_failure')
252+
->once()
253+
->with(FailureRegistry::SELLER_STATUS_KEY);
254+
255+
when('apply_filters')->alias(function (string $hook, ...$args) use ($fallback) {
256+
if ($hook === 'woocommerce_paypal_payments_seller_status_fallback') {
257+
return $fallback;
258+
}
259+
return $args[0] ?? null;
260+
});
261+
when('wp_remote_get')->justReturn($raw_response);
262+
when('is_wp_error')->justReturn(false);
263+
when('wp_remote_retrieve_response_code')->justReturn(500);
264+
265+
$result = $this->make_endpoint()->seller_status();
266+
267+
$this->assertSame($fallback, $result);
268+
}
269+
214270
// -----------------------------------------------------------------------
215271
// Tests: clear_seller_status_cache()
216272
// -----------------------------------------------------------------------

0 commit comments

Comments
 (0)