-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathPartnersEndpointTest.php
More file actions
315 lines (260 loc) · 9.04 KB
/
Copy pathPartnersEndpointTest.php
File metadata and controls
315 lines (260 loc) · 9.04 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
<?php
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Mockery;
use Psr\Log\LoggerInterface;
use Requests_Utility_CaseInsensitiveDictionary;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatus;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Token;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Factory\SellerStatusFactory;
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
use WooCommerce\PayPalCommerce\ApiClient\Helper\FailureRegistry;
use WooCommerce\PayPalCommerce\TestCase;
use function Brain\Monkey\Functions\expect;
use function Brain\Monkey\Functions\when;
class PartnersEndpointTest extends TestCase
{
private const HOST = 'https://api.sandbox.paypal.com/';
private const PARTNER_ID = 'partner-abc';
private const MERCHANT_ID = 'merchant-xyz';
/**
* @var Bearer&\Mockery\MockInterface
*/
private $bearer;
/**
* @var LoggerInterface&\Mockery\MockInterface
*/
private $logger;
/**
* @var SellerStatusFactory&\Mockery\MockInterface
*/
private $seller_status_factory;
/**
* @var FailureRegistry&\Mockery\MockInterface
*/
private $failure_registry;
/**
* @var Cache&\Mockery\MockInterface
*/
private $cache;
public function setUp(): void
{
parent::setUp();
$token = Mockery::mock(Token::class);
$token->shouldReceive('token')->andReturn('test-token');
$this->bearer = Mockery::mock(Bearer::class);
$this->bearer->shouldReceive('bearer')->andReturn($token);
$this->logger = Mockery::mock(LoggerInterface::class);
$this->logger->shouldReceive('debug')->andReturnNull();
$this->logger->shouldReceive('log')->andReturnNull();
$this->seller_status_factory = Mockery::mock(SellerStatusFactory::class);
$this->failure_registry = Mockery::mock(FailureRegistry::class);
$this->cache = Mockery::mock(Cache::class);
when('trailingslashit')->alias(function (string $url): string {
return rtrim($url, '/') . '/';
});
when('wp_remote_retrieve_body')->alias(function (array $response): string {
return $response['body'] ?? '';
});
}
// -----------------------------------------------------------------------
// Factory helper
// -----------------------------------------------------------------------
private function make_endpoint(): PartnersEndpoint
{
return new PartnersEndpoint(
self::HOST,
$this->bearer,
$this->logger,
$this->seller_status_factory,
self::PARTNER_ID,
self::MERCHANT_ID,
$this->failure_registry,
$this->cache
);
}
private function make_raw_response(int $status_code = 200): array
{
$headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class);
$headers->shouldReceive('getAll')->andReturn([]);
return [
'body' => '{}',
'headers' => $headers,
'response' => ['code' => $status_code, 'message' => 'OK'],
];
}
// -----------------------------------------------------------------------
// Tests: seller_status() caching behaviour
// -----------------------------------------------------------------------
/**
* GIVEN a SellerStatus result is stored in the transient cache
* WHEN seller_status() is called
* THEN the cached value is returned immediately
* AND no HTTP request is made to the PayPal API
*/
public function testSellerStatusReturnsCachedValueWithoutHttpCall(): void
{
$cached_status = Mockery::mock(SellerStatus::class);
$this->cache
->shouldReceive('get')
->once()
->with(PartnersEndpoint::SELLER_STATUS_CACHE_KEY)
->andReturn($cached_status);
when('apply_filters')->alias(function (string $hook, ...$args) {
return $args[0] ?? null;
});
// wp_remote_get must not be called — any invocation will cause the
// Brain Monkey expectation to fail with an unexpected call.
expect('wp_remote_get')->never();
$result = $this->make_endpoint()->seller_status();
$this->assertSame($cached_status, $result);
}
/**
* GIVEN the transient cache contains no seller status entry
* WHEN seller_status() is called
* THEN the PayPal API is queried
* AND the response is stored in the cache with the correct key and TTL
* AND the resulting SellerStatus is returned
*/
public function testSellerStatusOnCacheMissFetchesAndStoresResult(): void
{
$seller_status = Mockery::mock(SellerStatus::class);
$raw_response = $this->make_raw_response(200);
$this->cache
->shouldReceive('get')
->once()
->with(PartnersEndpoint::SELLER_STATUS_CACHE_KEY)
->andReturn(false);
$this->cache
->shouldReceive('set')
->once()
->with(
PartnersEndpoint::SELLER_STATUS_CACHE_KEY,
$seller_status,
PartnersEndpoint::SELLER_STATUS_CACHE_TTL
);
$this->seller_status_factory
->shouldReceive('from_paypal_response')
->once()
->andReturn($seller_status);
$this->failure_registry
->shouldReceive('clear_failures')
->once()
->with(FailureRegistry::SELLER_STATUS_KEY);
$this->stub_successful_http_round_trip($raw_response);
$result = $this->make_endpoint()->seller_status();
$this->assertSame($seller_status, $result);
}
/**
* GIVEN the transient cache is empty
* AND the PayPal API returns a non-200 status code
* WHEN seller_status() is called
* THEN the failure is registered in the failure registry
* AND the result is NOT stored in the cache
* AND a PayPalApiException is thrown
*/
public function testSellerStatusOnApiErrorRegistersFailureAndThrows(): void
{
$raw_response = $this->make_raw_response(500);
$this->cache
->shouldReceive('get')
->once()
->with(PartnersEndpoint::SELLER_STATUS_CACHE_KEY)
->andReturn(false);
$this->cache
->shouldReceive('set')
->never();
$this->failure_registry
->shouldReceive('add_failure')
->once()
->with(FailureRegistry::SELLER_STATUS_KEY);
$this->stub_failed_http_round_trip($raw_response, 500);
$this->expectException(PayPalApiException::class);
$this->make_endpoint()->seller_status();
}
// -----------------------------------------------------------------------
// Tests: seller_status() fallback filter
// -----------------------------------------------------------------------
/**
* GIVEN the transient cache is empty
* AND the PayPal API returns a non-200 status code
* AND the fallback filter returns a SellerStatus
* WHEN seller_status() is called
* THEN the fallback is used instead of throwing
* AND the result is stored in the cache
*/
public function testSellerStatusOnApiErrorUsesFallbackFilter(): void
{
$fallback = Mockery::mock(SellerStatus::class);
$raw_response = $this->make_raw_response(500);
$this->cache
->shouldReceive('get')
->once()
->with(PartnersEndpoint::SELLER_STATUS_CACHE_KEY)
->andReturn(false);
$this->cache
->shouldReceive('set')
->once()
->with(
PartnersEndpoint::SELLER_STATUS_CACHE_KEY,
$fallback,
PartnersEndpoint::SELLER_STATUS_CACHE_TTL
);
$this->failure_registry
->shouldReceive('add_failure')
->once()
->with(FailureRegistry::SELLER_STATUS_KEY);
when('apply_filters')->alias(function (string $hook, ...$args) use ($fallback) {
if ($hook === 'woocommerce_paypal_payments_seller_status_fallback') {
return $fallback;
}
return $args[0] ?? null;
});
when('wp_remote_get')->justReturn($raw_response);
when('is_wp_error')->justReturn(false);
when('wp_remote_retrieve_response_code')->justReturn(500);
$result = $this->make_endpoint()->seller_status();
$this->assertSame($fallback, $result);
}
// -----------------------------------------------------------------------
// Tests: clear_seller_status_cache()
// -----------------------------------------------------------------------
/**
* GIVEN a cached seller status entry exists
* WHEN clear_seller_status_cache() is called
* THEN the cache entry is deleted using the correct key
*/
public function testClearSellerStatusCacheDeletesCacheEntry(): void
{
$this->cache
->shouldReceive('delete')
->once()
->with(PartnersEndpoint::SELLER_STATUS_CACHE_KEY);
$this->make_endpoint()->clear_seller_status_cache();
// Mockery will assert the expectation above during tearDown.
$this->addToAssertionCount(1);
}
// -----------------------------------------------------------------------
// HTTP stubs — allow (not assert) internal HTTP mechanics
// -----------------------------------------------------------------------
private function stub_successful_http_round_trip(array $raw_response): void
{
when('apply_filters')->alias(function (string $hook, ...$args) {
return $args[0] ?? null;
});
when('wp_remote_get')->justReturn($raw_response);
when('is_wp_error')->justReturn(false);
when('wp_remote_retrieve_response_code')->justReturn(200);
}
private function stub_failed_http_round_trip(array $raw_response, int $status_code): void
{
when('apply_filters')->alias(function (string $hook, ...$args) {
return $args[0] ?? null;
});
when('wp_remote_get')->justReturn($raw_response);
when('is_wp_error')->justReturn(false);
when('wp_remote_retrieve_response_code')->justReturn($status_code);
}
}