-
Notifications
You must be signed in to change notification settings - Fork 66
/
BaseApiTest.php
executable file
·398 lines (341 loc) · 10.9 KB
/
BaseApiTest.php
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
namespace OhMyBrew;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use ReflectionClass;
class BaseApiTest extends \PHPUnit\Framework\TestCase
{
/**
* @test
*
* Should set API to private mode
*/
public function itShouldSetApiToPrivateMode()
{
$api = new BasicShopifyAPI(true);
$this->assertEquals(true, $api->isPrivate());
$this->assertEquals(false, $api->isPublic());
}
/**
* @test
*
* Should set API to public mode
*/
public function itShouldSetApiToPublicMode()
{
$api = new BasicShopifyAPI();
$this->assertEquals(false, $api->isPrivate());
$this->assertEquals(true, $api->isPublic());
}
/**
* @test
*
* Should set shop
*/
public function itShouldSetShop()
{
$api = new BasicShopifyAPI();
$api->setShop('example.myshopify.com');
$this->assertEquals('example.myshopify.com', $api->getShop());
}
/**
* @test
*
* Should set access token
*/
public function itShouldSetAccessToken()
{
$api = new BasicShopifyAPI();
$api->setAccessToken('123');
$this->assertEquals('123', $api->getAccessToken());
}
/**
* @test
*
* Should set API key and API password and API shared secret
*/
public function itShouldSetApiKeyAndPassword()
{
$api = new BasicShopifyAPI();
$api->setApiKey('123');
$api->setApiPassword('abc');
$api->setApiSecret('!@#');
$reflected = new ReflectionClass($api);
$apiKeyProperty = $reflected->getProperty('apiKey');
$apiKeyProperty->setAccessible(true);
$apiPasswordProperty = $reflected->getProperty('apiPassword');
$apiPasswordProperty->setAccessible(true);
$apiSecretProperty = $reflected->getProperty('apiSecret');
$apiSecretProperty->setAccessible(true);
$this->assertEquals('123', $apiKeyProperty->getValue($api));
$this->assertEquals('abc', $apiPasswordProperty->getValue($api));
$this->assertEquals('!@#', $apiSecretProperty->getValue($api));
}
/**
* @test
*
* Should allow for own client injection
*/
public function itShouldAllowForOwnClient()
{
$api = new BasicShopifyAPI();
$api->setClient(new Client(['handler' => new MockHandler()]));
$reflected = new ReflectionClass($api);
$clientProperty = $reflected->getProperty('client');
$clientProperty->setAccessible(true);
$value = $clientProperty->getValue($api);
$this->assertEquals('GuzzleHttp\Handler\MockHandler', get_class($value->getConfig('handler')));
}
/**
* @test
*
* Check verify with no params
*/
public function itShouldFailRequestVerifyWithNoParams()
{
$api = new BasicShopifyAPI();
$api->setApiSecret('hush');
$this->assertEquals(false, $api->verifyRequest([]));
}
/**
* @test
*
* @expectedException TypeError
*
* Check verify with no params
*/
public function itShouldFailRequestVerifyWithNoParamsAgain()
{
$api = new BasicShopifyAPI();
$api->setApiSecret('hush');
$this->assertEquals(false, $api->verifyRequest(null));
}
/**
* @test
*
* @expectedException Exception
* @expectedExceptionMessage API secret is missing
*
* Check verify without api secret
*/
public function itShouldThrowErrorOnVerifyWithoutApiSecret()
{
$api = new BasicShopifyAPI();
$api->verifyRequest([]);
}
/**
* @test
*
* Check verify with params
*/
public function itShouldPassRequestVerifyWithParams()
{
$hmac = '4712bf92ffc2917d15a2f5a273e39f0116667419aa4b6ac0b3baaf26fa3c4d20';
$params = [
'code' => '0907a61c0c8d55e99db179b68161bc00',
'hmac' => $hmac,
'shop' => 'some-shop.myshopify.com',
'timestamp' => '1337178173',
];
$api = new BasicShopifyAPI();
$api->setApiSecret('hush');
$this->assertEquals(true, $api->verifyRequest($params));
}
/**
* @test
*
* Check verify with bad params
*/
public function itShouldPassRequestVerifyWithBadParams()
{
$hmac = '4712bf92ffc2917d15a2f5a273e39f0116667419aa4b6ac0b3baaf26fa3c4d20';
$params = [
'code' => '0907a61c0c8d55e99db179b68161bc00-OOPS',
'hmac' => $hmac,
'shop' => 'some-shop.myshopify.com',
];
$api = new BasicShopifyAPI();
$api->setApiSecret('hush');
$this->assertEquals(false, $api->verifyRequest($params));
}
/**
* @test
*
* Should set shop and access tokeb via quick method
*/
public function itShouldSetSession()
{
$api = new BasicShopifyAPI();
$api->setSession('example.myshopify.com', '1234');
$this->assertEquals('example.myshopify.com', $api->getShop());
$this->assertEquals('1234', $api->getAccessToken());
}
/**
* @test
*
* Should isolate API session
*/
public function itShouldWithSession()
{
$self = $this;
$api = new BasicShopifyAPI();
// Isolated for a shop
$api->withSession('example.myshopify.com', '1234', function () use (&$self) {
$self->assertEquals('example.myshopify.com', $this->getShop());
$self->assertEquals('1234', $this->getAccessToken());
$self->assertInstanceOf(BasicShopifyAPI::class, $this);
});
// Isolated for a shop
$api->withSession('example2.myshopify.com', '12345', function () use (&$self) {
$self->assertEquals('example2.myshopify.com', $this->getShop());
$self->assertEquals('12345', $this->getAccessToken());
$self->assertInstanceOf(BasicShopifyAPI::class, $this);
});
// Isolated for a shop and returns a value
$valueReturn = $api->withSession('example2.myshopify.com', '12345', function () use (&$self) {
return $this->getAccessToken();
});
$this->assertEquals($valueReturn, '12345');
// Should remain untouched
$this->assertEquals($api->getShop(), null);
$this->assertEquals($api->getAccessToken(), null);
}
/**
* @test
* @expectedException TypeError
*
* Ensure a closure is passed to withSession
*/
public function itShouldThrowExceptionForSessionWithNoClosure()
{
$api = new BasicShopifyAPI();
$api->withSession('example.myshopify.com', '1234', null);
}
/**
* @test
*
* Should get access token from Shopify
*/
public function itShouldGetAccessTokenFromShopify()
{
$response = new Response(
200,
[],
file_get_contents(__DIR__.'/fixtures/admin__oauth__access_token.json')
);
$mock = new MockHandler([$response]);
$client = new Client(['handler' => $mock]);
$api = new BasicShopifyAPI();
$api->setShop('example.myshopify.com');
$api->setApiKey('123');
$api->setApiSecret('abc');
$api->setClient($client);
$code = '!@#';
$token = $api->requestAccessToken($code);
$data = json_decode($mock->getLastRequest()->getBody());
$this->assertEquals('f85632530bf277ec9ac6f649fc327f17', $token);
$this->assertEquals('abc', $data->client_secret);
$this->assertEquals('123', $data->client_id);
$this->assertEquals($code, $data->code);
}
/**
* @test
* @expectedException Exception
* @expectedExceptionMessage Shopify domain missing for API calls
*
* Ensure Shopify domain is there for grabbing the access tokens
*/
public function itShouldThrowExceptionForMissingShopOnAccessTokenRequest()
{
$api = new BasicShopifyAPI(true);
$api->requestAccessToken('123');
}
/**
* @test
* @expectedException Exception
* @expectedExceptionMessage API key or secret is missing
*
* Ensure Shopify API secret is there for grabbing the access tokens
*/
public function itShouldThrowExceptionForMissingApiSecretOnAccessTokenRequest()
{
$api = new BasicShopifyAPI(true);
$api->setShop('example.myshopify.com');
$api->requestAccessToken('123');
}
/**
* @test
*
* Should get auth URL
*/
public function itShouldReturnAuthUrl()
{
$api = new BasicShopifyAPI();
$api->setShop('example.myshopify.com');
$api->setApiKey('123');
$this->assertEquals(
'https://example.myshopify.com/admin/oauth/authorize?client_id=123&scope=read_products,write_products&redirect_uri=https://localapp.local/',
$api->getAuthUrl(['read_products', 'write_products'], 'https://localapp.local/')
);
}
/**
* @test
*
* @expectedException Exception
* @expectedExceptionMessage Shopify domain missing for API calls
*
* Should throw error for missing shop on auth call
*/
public function itShouldThrowErrorForMissingShopDomainOnAuthCall()
{
$api = new BasicShopifyAPI();
$api->getAuthUrl(['read_products', 'write_products'], 'https://localapp.local/');
}
/**
* @test
*
* @expectedException Exception
* @expectedExceptionMessage API key is missing
*
* Should throw error for missing API key on auth call
*/
public function itShouldThrowErrorForMissingApiKeyOnAuthCall()
{
$api = new BasicShopifyAPI();
$api->setShop('example.myshopify.com');
$api->getAuthUrl(['read_products', 'write_products'], 'https://localapp.local/');
}
/**
* @test
*
* Should set rate limiting to enabled
*/
public function itShouldSetRateLimitingToEnabled()
{
$api = new BasicShopifyAPI();
$this->assertFalse($api->isRateLimitingEnabled());
$api->enableRateLimiting(0.25 * 1000, 0);
$reflected = new ReflectionClass($api);
$rateLimitCycleProperty = $reflected->getProperty('rateLimitCycle');
$rateLimitCycleProperty->setAccessible(true);
$rateLimitCycleBufferProperty = $reflected->getProperty('rateLimitCycleBuffer');
$rateLimitCycleBufferProperty->setAccessible(true);
$this->assertEquals(0.25 * 1000, $rateLimitCycleProperty->getValue($api));
$this->assertEquals(0, $rateLimitCycleBufferProperty->getValue($api));
$this->assertTrue($api->isRateLimitingEnabled());
}
/**
* @test
*
* Should set rate limiting to disabled
*/
public function itShouldSetRateLimitingToDisabled()
{
$api = new BasicShopifyAPI();
$api->enableRateLimiting();
$this->assertTrue($api->isRateLimitingEnabled());
$api->disableRateLimiting();
$this->assertFalse($api->isRateLimitingEnabled());
}
}