1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MercadoLibre PHP SDK |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* For full copyright and license information, please see the LICENSE file |
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) 2018 Lucas Banegas <[email protected]> |
10
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
11
|
|
|
* @link https://github.com/docta/mercadolibre |
12
|
|
|
*/ |
13
|
|
|
namespace Docta\MercadoLibre\Test; |
14
|
|
|
|
15
|
|
|
use Docta\MercadoLibre\Client; |
16
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
17
|
|
|
use Mockery; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* MercadoLibre test class. |
21
|
|
|
*/ |
22
|
|
|
class ClientTest extends \PHPUnit_Framework_TestCase |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string Auth site |
26
|
|
|
*/ |
27
|
|
|
protected $authSite = 'MLA'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string Client ID |
31
|
|
|
*/ |
32
|
|
|
protected $clientId; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string Cliente secret key |
36
|
|
|
*/ |
37
|
|
|
protected $clientSecret; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string Redirect URI |
41
|
|
|
*/ |
42
|
|
|
protected $redirectUri = 'https://example.com/oauth'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string Code |
46
|
|
|
*/ |
47
|
|
|
protected $code; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string Token type |
51
|
|
|
*/ |
52
|
|
|
protected $tokenType = 'bearer'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string Access token |
56
|
|
|
*/ |
57
|
|
|
protected $accessToken; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string Refresh token |
61
|
|
|
*/ |
62
|
|
|
protected $refreshToken; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string User id |
66
|
|
|
*/ |
67
|
|
|
protected $userId; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var integer Token expires in |
71
|
|
|
*/ |
72
|
|
|
protected $expiresIn = 10800; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var string Token scope |
76
|
|
|
*/ |
77
|
|
|
protected $scope = 'write read'; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var string Token json |
81
|
|
|
*/ |
82
|
|
|
protected $jsonToken; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var string User json |
86
|
|
|
*/ |
87
|
|
|
protected $jsonUser; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var array Headers |
91
|
|
|
*/ |
92
|
|
|
protected $headers = ['content-type' => 'json']; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var Client Client instance |
96
|
|
|
*/ |
97
|
|
|
protected $mercadolibre; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Setup test. |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
protected function setUp() |
105
|
|
|
{ |
106
|
|
|
$this->clientId = uniqid('id_', true); |
107
|
|
|
$this->clientSecret = uniqid('sk_', true); |
108
|
|
|
$this->code = uniqid('cd_', true); |
109
|
|
|
$this->accessToken = uniqid('at_', true); |
110
|
|
|
$this->refreshToken = uniqid('rt_', true); |
111
|
|
|
$this->userId = uniqid('ui_', true); |
112
|
|
|
|
113
|
|
|
$this->jsonToken = json_encode([ |
114
|
|
|
'token_type' => $this->tokenType, |
115
|
|
|
'access_token' => $this->accessToken, |
116
|
|
|
'refresh_token' => $this->refreshToken, |
117
|
|
|
'user_id' => $this->userId, |
118
|
|
|
'expires_in' => $this->expiresIn, |
119
|
|
|
'scope' => $this->scope |
120
|
|
|
]); |
121
|
|
|
|
122
|
|
|
$this->jsonUser = json_encode([ |
123
|
|
|
'id' => $this->userId |
124
|
|
|
]); |
125
|
|
|
|
126
|
|
|
$this->mercadolibre = new Client([ |
127
|
|
|
'authSite' => $this->authSite, |
128
|
|
|
'clientId' => $this->clientId, |
129
|
|
|
'clientSecret' => $this->clientSecret, |
130
|
|
|
'redirectUri' => $this->redirectUri |
131
|
|
|
]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Tear down test. |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
|
|
public function tearDown() |
140
|
|
|
{ |
141
|
|
|
parent::tearDown(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* testGet method |
146
|
|
|
* |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
|
|
public function testGet() |
150
|
|
|
{ |
151
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
152
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
153
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonToken); |
154
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
155
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
156
|
|
|
$this->mercadolibre->setClient($client); |
157
|
|
|
$this->mercadolibre->getToken($this->code); |
158
|
|
|
|
159
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
160
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
161
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonUser); |
162
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
163
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
164
|
|
|
$this->mercadolibre->setClient($client); |
165
|
|
|
|
166
|
|
|
$expected = json_decode($this->jsonUser, true); |
167
|
|
|
$actual = $this->mercadolibre->get('/mock/path'); |
168
|
|
|
$this->assertEquals($expected, $actual); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* testPost method |
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
|
|
public function testPost() |
177
|
|
|
{ |
178
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
179
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
180
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonToken); |
181
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
182
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
183
|
|
|
$this->mercadolibre->setClient($client); |
184
|
|
|
$this->mercadolibre->getToken($this->code); |
185
|
|
|
|
186
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
187
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
188
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonUser); |
189
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
190
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
191
|
|
|
$this->mercadolibre->setClient($client); |
192
|
|
|
|
193
|
|
|
$expected = json_decode($this->jsonUser, true); |
194
|
|
|
$actual = $this->mercadolibre->post('/mock/path'); |
195
|
|
|
$this->assertEquals($expected, $actual); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* testPut method |
200
|
|
|
* |
201
|
|
|
* @return void |
202
|
|
|
*/ |
203
|
|
|
public function testPut() |
204
|
|
|
{ |
205
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
206
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
207
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonToken); |
208
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
209
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
210
|
|
|
$this->mercadolibre->setClient($client); |
211
|
|
|
$this->mercadolibre->getToken($this->code); |
212
|
|
|
|
213
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
214
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
215
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonUser); |
216
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
217
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
218
|
|
|
$this->mercadolibre->setClient($client); |
219
|
|
|
|
220
|
|
|
$expected = json_decode($this->jsonUser, true); |
221
|
|
|
$actual = $this->mercadolibre->put('/mock/path'); |
222
|
|
|
$this->assertEquals($expected, $actual); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* testDelete method |
227
|
|
|
* |
228
|
|
|
* @return void |
229
|
|
|
*/ |
230
|
|
|
public function testDelete() |
231
|
|
|
{ |
232
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
233
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
234
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonToken); |
235
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
236
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
237
|
|
|
$this->mercadolibre->setClient($client); |
238
|
|
|
$this->mercadolibre->getToken($this->code); |
239
|
|
|
|
240
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
241
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
242
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonUser); |
243
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
244
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
245
|
|
|
$this->mercadolibre->setClient($client); |
246
|
|
|
|
247
|
|
|
$expected = json_decode($this->jsonUser, true); |
248
|
|
|
$actual = $this->mercadolibre->delete('/mock/path'); |
249
|
|
|
$this->assertEquals($expected, $actual); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* testOptions method |
254
|
|
|
* |
255
|
|
|
* @return void |
256
|
|
|
*/ |
257
|
|
|
public function testOptions() |
258
|
|
|
{ |
259
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
260
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
261
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonToken); |
262
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
263
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
264
|
|
|
$this->mercadolibre->setClient($client); |
265
|
|
|
$this->mercadolibre->getToken($this->code); |
266
|
|
|
|
267
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
268
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
269
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonUser); |
270
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
271
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
272
|
|
|
$this->mercadolibre->setClient($client); |
273
|
|
|
|
274
|
|
|
$expected = json_decode($this->jsonUser, true); |
275
|
|
|
$actual = $this->mercadolibre->options('/mock/path'); |
276
|
|
|
$this->assertEquals($expected, $actual); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* testGetAuthUrl method |
281
|
|
|
* |
282
|
|
|
* @return void |
283
|
|
|
*/ |
284
|
|
|
public function testGetAuthUrl() |
285
|
|
|
{ |
286
|
|
|
$actual = $this->mercadolibre->getAuthUrl(); |
287
|
|
|
$actual = parse_url($actual); |
288
|
|
|
$this->assertArrayHasKey('scheme', $actual); |
289
|
|
|
$this->assertArrayHasKey('host', $actual); |
290
|
|
|
$this->assertArrayHasKey('path', $actual); |
291
|
|
|
$this->assertArrayHasKey('query', $actual); |
292
|
|
|
$this->assertEquals('https', $actual['scheme']); |
293
|
|
|
$this->assertEquals('auth.mercadolibre.com.ar', $actual['host']); |
294
|
|
|
$this->assertEquals('/authorization', $actual['path']); |
295
|
|
|
|
296
|
|
|
parse_str($actual['query'], $query); |
297
|
|
|
$this->assertArrayHasKey('response_type', $query); |
298
|
|
|
$this->assertArrayHasKey('client_id', $query); |
299
|
|
|
$this->assertArrayHasKey('redirect_uri', $query); |
300
|
|
|
$this->assertArrayHasKey('state', $query); |
301
|
|
|
$this->assertEquals('code', $query['response_type']); |
302
|
|
|
$this->assertEquals($this->clientId, $query['client_id']); |
303
|
|
|
$this->assertEquals($this->redirectUri, $query['redirect_uri']); |
304
|
|
|
$this->assertEquals($this->mercadolibre->getState(), $query['state']); |
305
|
|
|
$this->assertArrayNotHasKey('approval_prompt', $query); |
306
|
|
|
$this->assertArrayNotHasKey('scope', $query); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* testGetOwner method |
311
|
|
|
* |
312
|
|
|
* @return void |
313
|
|
|
*/ |
314
|
|
|
public function testGetOwner() |
315
|
|
|
{ |
316
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
317
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
318
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonToken); |
319
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
320
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
321
|
|
|
$this->mercadolibre->setClient($client); |
322
|
|
|
$this->mercadolibre->getToken($this->code); |
323
|
|
|
|
324
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
325
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
326
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonUser); |
327
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
328
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
329
|
|
|
$this->mercadolibre->setClient($client); |
330
|
|
|
|
331
|
|
|
$owner = $this->mercadolibre->getOwner(); |
332
|
|
|
$this->assertEquals($this->userId, $owner->getId()); |
333
|
|
|
$this->assertEquals(json_decode($this->jsonUser, true), $owner->toArray()); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* testGetState method |
338
|
|
|
* |
339
|
|
|
* @return void |
340
|
|
|
*/ |
341
|
|
|
public function testGetState() |
342
|
|
|
{ |
343
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
344
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
345
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonToken); |
346
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
347
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
348
|
|
|
$this->mercadolibre->setClient($client); |
349
|
|
|
$this->mercadolibre->getAuthUrl(); |
350
|
|
|
|
351
|
|
|
$state = $this->mercadolibre->getState(); |
352
|
|
|
$this->assertInternalType('string', $state); |
353
|
|
|
$this->assertNotEmpty($state); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* testGetToken method |
358
|
|
|
* |
359
|
|
|
* @return void |
360
|
|
|
*/ |
361
|
|
|
public function testGetToken() |
362
|
|
|
{ |
363
|
|
|
$this->assertNull($this->mercadolibre->getToken()); |
364
|
|
|
|
365
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
366
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
367
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonToken); |
368
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
369
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
370
|
|
|
$this->mercadolibre->setClient($client); |
371
|
|
|
|
372
|
|
|
$token = $this->mercadolibre->getToken($this->code); |
373
|
|
|
$this->assertInstanceOf(AccessToken::class, $token); |
374
|
|
|
|
375
|
|
|
$token = $this->mercadolibre->getToken($token); |
376
|
|
|
$this->assertInstanceOf(AccessToken::class, $token); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* testSetToken method |
381
|
|
|
* |
382
|
|
|
* @return void |
383
|
|
|
*/ |
384
|
|
|
public function testSetToken() |
385
|
|
|
{ |
386
|
|
|
$response = Mockery::mock('GuzzleHttp\Psr7\Response'); |
387
|
|
|
$response->shouldReceive('getHeader')->andReturn($this->headers); |
388
|
|
|
$response->shouldReceive('getBody')->andReturn($this->jsonToken); |
389
|
|
|
$client = Mockery::mock('GuzzleHttp\Client'); |
390
|
|
|
$client->shouldReceive('send')->times(1)->andReturn($response); |
391
|
|
|
$this->mercadolibre->setClient($client); |
392
|
|
|
|
393
|
|
|
$token = $this->mercadolibre->getToken($this->code); |
394
|
|
|
$token = $this->mercadolibre->setToken($token); |
395
|
|
|
$this->assertInstanceOf(AccessToken::class, $token); |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|