-
Notifications
You must be signed in to change notification settings - Fork 66
/
RestApiTest.php
executable file
·322 lines (280 loc) · 10.7 KB
/
RestApiTest.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
<?php
namespace OhMyBrew;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use ReflectionClass;
class RestApiTest extends \PHPUnit\Framework\TestCase
{
/**
* @test
*
* Checking base URL for API calls on private
*/
public function itShouldReturnPrivateBaseUrl()
{
$response = new Response(
200,
['http_x_shopify_shop_api_call_limit' => '2/80'],
file_get_contents(__DIR__.'/fixtures/rest/admin__shop.json')
);
$mock = new MockHandler([$response]);
$client = new Client(['handler' => $mock]);
$api = new BasicShopifyAPI(true);
$api->setClient($client);
$api->setShop('example.myshopify.com');
$api->setApiKey('123');
$api->setApiPassword('abc');
$api->rest('GET', '/admin/shop.json');
$lastRequest = $mock->getLastRequest()->getUri();
$this->assertEquals('https', $lastRequest->getScheme());
$this->assertEquals('example.myshopify.com', $lastRequest->getHost());
$this->assertEquals('123:abc', $lastRequest->getUserInfo());
$this->assertEquals('/admin/shop.json', $lastRequest->getPath());
}
/**
* @test
*
* Checking base URL for API calls on public
*/
public function itShouldReturnPublicBaseUrl()
{
$response = new Response(
200,
['http_x_shopify_shop_api_call_limit' => '2/80'],
file_get_contents(__DIR__.'/fixtures/rest/admin__shop.json')
);
$mock = new MockHandler([$response]);
$client = new Client(['handler' => $mock]);
$api = new BasicShopifyAPI();
$api->setClient($client);
$api->setShop('example.myshopify.com');
$api->rest('GET', '/admin/shop.json');
$lastRequest = $mock->getLastRequest()->getUri();
$this->assertEquals('https', $lastRequest->getScheme());
$this->assertEquals('example.myshopify.com', $lastRequest->getHost());
$this->assertEquals(null, $lastRequest->getUserInfo());
$this->assertEquals('/admin/shop.json', $lastRequest->getPath());
}
/**
* @test
* @expectedException Exception
* @expectedExceptionMessage Shopify domain missing for API calls
*
* Ensure Shopify domain is there for baseURL
*/
public function itShouldThrowExceptionForMissingDomain()
{
$api = new BasicShopifyAPI();
$api->rest('GET', '/admin/shop.json');
}
/**
* @test
* @expectedException Exception
* @expectedExceptionMessage API key and password required for private Shopify REST calls
*
* Ensure Shopify API details is passsed for private API calls
*/
public function itShouldThrowExceptionForMissingApiDetails()
{
$api = new BasicShopifyAPI(true);
$api->setShop('example.myshopify.com');
$api->rest('GET', '/admin/shop.json');
}
/**
* @test
*
* Should get Guzzle response and JSON body
*/
public function itShouldReturnGuzzleResponseAndJsonBody()
{
$response = new Response(
200,
['http_x_shopify_shop_api_call_limit' => '2/80'],
file_get_contents(__DIR__.'/fixtures/rest/admin__shop.json')
);
$mock = new MockHandler([$response]);
$client = new Client(['handler' => $mock]);
$api = new BasicShopifyAPI();
$api->setClient($client);
$api->setShop('example.myshopify.com');
$api->setApiKey('123');
$api->setAccessToken('!@#');
// Fake param just to test it receives it
$request = $api->rest('GET', '/admin/shop.json', ['limit' => 1, 'page' => 1]);
$data = $mock->getLastRequest()->getUri()->getQuery();
$token_header = $mock->getLastRequest()->getHeader('X-Shopify-Access-Token')[0];
$this->assertEquals(true, is_object($request));
$this->assertInstanceOf('GuzzleHttp\Psr7\Response', $request->response);
$this->assertEquals(200, $request->response->getStatusCode());
$this->assertEquals(true, is_object($request->body));
$this->assertEquals('Apple Computers', $request->body->shop->name);
$this->assertEquals('limit=1&page=1', $data);
$this->assertEquals('!@#', $token_header);
}
/**
* @test
* @expectedException Exception
* @expectedExceptionMessage Invalid API call limit key. Valid keys are: left, made, limit
*
* Ensure we pass a valid key to the API calls
*/
public function itShouldThrowExceptionForInvalidApiCallsKey()
{
$api = new BasicShopifyAPI();
$api->getApiCalls('rest', 'oops');
}
/**
* @test
*
* Should get API call limits
*/
public function itShouldReturnApiCallLimits()
{
$response = new Response(200, ['http_x_shopify_shop_api_call_limit' => '2/80'], '{}');
$mock = new MockHandler([$response]);
$client = new Client(['handler' => $mock]);
$api = new BasicShopifyAPI();
$api->setClient($client);
$api->setShop('example.myshopify.com');
$api->setApiKey('123');
$api->setAccessToken('!@#');
$api->rest('GET', '/admin/shop.json');
$this->assertEquals(2, $api->getApiCalls('rest', 'made'));
$this->assertEquals(80, $api->getApiCalls('rest', 'limit'));
$this->assertEquals(80 - 2, $api->getApiCalls('rest', 'left'));
$this->assertEquals(['left' => 80 - 2, 'made' => 2, 'limit' => 80], $api->getApiCalls('rest'));
}
/**
* @test
*
* Check if the API call limit header is missing, we do not error out
* Example: /admin/checkout.json does not apparently return this header
*/
public function itShouldContinueWithoutApiCallLimitHeader()
{
$response = new Response(200, [], '{}');
$mock = new MockHandler([$response]);
$client = new Client(['handler' => $mock]);
$api = new BasicShopifyAPI();
$api->setClient($client);
$api->setShop('example.myshopify.com');
$api->setApiKey('123');
$api->setAccessToken('!@#');
$api->rest('GET', '/admin/shop.json');
$this->assertEquals(0, $api->getApiCalls('rest', 'made'));
$this->assertEquals(40, $api->getApiCalls('rest', 'limit'));
$this->assertEquals(0, $api->getApiCalls('rest', 'left'));
$this->assertEquals(['left' => 0, 'made' => 0, 'limit' => 40], $api->getApiCalls('rest'));
}
/**
* @test
*
* Should use query for GET requests
*/
public function itShouldUseQueryForGetMethod()
{
$response = new Response(200, ['http_x_shopify_shop_api_call_limit' => '2/80'], '{}');
$mock = new MockHandler([$response]);
$client = new Client(['handler' => $mock]);
$api = new BasicShopifyAPI();
$api->setClient($client);
$api->setShop('example.myshopify.com');
$api->setApiKey('123');
$api->setAccessToken('!@#');
$api->rest('GET', '/admin/shop.json', ['limit' => 1, 'page' => 1]);
$this->assertEquals('limit=1&page=1', $mock->getLastRequest()->getUri()->getQuery());
$this->assertNull(json_decode($mock->getLastRequest()->getBody()));
}
/**
* @test
*
* Should use JSON for non-GET methods
*/
public function itShouldUseJsonForNonGetMethods()
{
$response = new Response(200, ['http_x_shopify_shop_api_call_limit' => '2/80'], '{}');
$mock = new MockHandler([$response]);
$client = new Client(['handler' => $mock]);
$api = new BasicShopifyAPI();
$api->setClient($client);
$api->setShop('example.myshopify.com');
$api->setApiKey('123');
$api->setAccessToken('!@#');
$api->rest('POST', '/admin/gift_cards.json', ['gift_cards' => ['initial_value' => 25.00]]);
$this->assertEquals('', $mock->getLastRequest()->getUri()->getQuery());
$this->assertNotNull(json_decode($mock->getLastRequest()->getBody()));
}
/**
* @test
*
* Should alias request to REST method
*/
public function itShouldAliasRequestToRestMethod()
{
$response = new Response(
200,
['http_x_shopify_shop_api_call_limit' => '2/80'],
file_get_contents(__DIR__.'/fixtures/rest/admin__shop.json')
);
$mock = new MockHandler([$response]);
$client = new Client(['handler' => $mock]);
$api = new BasicShopifyAPI();
$api->setClient($client);
$api->setShop('example.myshopify.com');
$request = $api->request('GET', '/admin/shop.json');
$this->assertEquals(true, is_object($request->body));
$this->assertEquals('Apple Computers', $request->body->shop->name);
}
/**
* @test
*
* Should track request timestamps.
*/
public function itShouldTrackRequestTimestamps()
{
$response = new Response(
200,
['http_x_shopify_shop_api_call_limit' => '2/80'],
file_get_contents(__DIR__.'/fixtures/rest/admin__shop.json')
);
$mock = new MockHandler([$response]);
$client = new Client(['handler' => $mock]);
$api = new BasicShopifyAPI();
$api->setClient($client);
$api->setShop('example.myshopify.com');
$reflected = new ReflectionClass($api);
$requestTimestampProperty = $reflected->getProperty('requestTimestamp');
$requestTimestampProperty->setAccessible(true);
$this->assertNull($requestTimestampProperty->getValue($api));
$request = $api->request('GET', '/admin/shop.json');
$this->assertNotNull($requestTimestampProperty->getValue($api));
}
/**
* @test
*
* Should rate limit requests
*/
public function itShouldRateLimitRequests()
{
$response = new Response(
200,
['http_x_shopify_shop_api_call_limit' => '2/80'],
file_get_contents(__DIR__.'/fixtures/rest/admin__shop.json')
);
$mock = new MockHandler([$response, $response]);
$client = new Client(['handler' => $mock]);
$api = new BasicShopifyAPI();
$api->setClient($client);
$api->setShop('example.myshopify.com');
$api->enableRateLimiting();
$timestamps = $api->request('GET', '/admin/shop.json')->timestamps;
$timestamps2 = $api->request('GET', '/admin/shop.json')->timestamps;
// First call should be null for initial, and greater than 0 for current
$this->assertEquals(null, $timestamps[0]);
$this->assertTrue($timestamps[1] > 0);
// This call should have the last call's time for initial, and greater than 0 for current
$this->assertEquals($timestamps[1], $timestamps2[0]);
$this->assertTrue($timestamps2[1] > 0);
}
}