Passed
Push — master ( 392727...30baef )
by Lucas
03:25
created

ClientBaseTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 279
rs 10
c 0
b 0
f 0
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 Mockery;
17
18
/**
19
 * MercadoLibre test class.
20
 */
21
class ClientBaseTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @var string Auth site
25
     */
26
    protected $authSite = 'MLA';
27
28
    /**
29
     * @var string Client ID
30
     */
31
    protected $clientId;
32
33
    /**
34
     * @var string Cliente secret key
35
     */
36
    protected $clientSecret;
37
38
    /**
39
     * @var string Redirect URI
40
     */
41
    protected $redirectUri = 'https://example.com/oauth';
42
43
    /**
44
     * @var string Code
45
     */
46
    protected $code;
47
48
    /**
49
     * @var string Token type
50
     */
51
    protected $tokenType = 'bearer';
52
53
    /**
54
     * @var string Access token
55
     */
56
    protected $accessToken;
57
58
    /**
59
     * @var string Refresh token
60
     */
61
    protected $refreshToken;
62
63
    /**
64
     * @var string User id
65
     */
66
    protected $userId;
67
68
    /**
69
     * @var integer Token expires in
70
     */
71
    protected $expiresIn = 10800;
72
73
    /**
74
     * @var string Token scope
75
     */
76
    protected $scope = 'write read';
77
78
    /**
79
     * @var string Token json
80
     */
81
    protected $jsonToken;
82
83
    /**
84
     * @var string User json
85
     */
86
    protected $jsonUser;
87
88
    /**
89
     * @var array Headers
90
     */
91
    protected $headers = ['content-type' => 'json'];
92
93
    /**
94
     * @var Client Client instance
95
     */
96
    protected $mercadolibre;
97
98
    /**
99
     * Setup test.
100
     *
101
     * @return void
102
     */
103
    protected function setUp()
104
    {
105
        $this->clientId = uniqid('id_', true);
106
        $this->clientSecret = uniqid('sk_', true);
107
        $this->code = uniqid('cd_', true);
108
        $this->accessToken = uniqid('at_', true);
109
        $this->refreshToken = uniqid('rt_', true);
110
        $this->userId = uniqid('ui_', true);
111
112
        $this->jsonToken = json_encode([
113
            'token_type' => $this->tokenType,
114
            'access_token' => $this->accessToken,
115
            'refresh_token' => $this->refreshToken,
116
            'user_id' => $this->userId,
117
            'expires_in' => $this->expiresIn,
118
            'scope' => $this->scope
119
        ]);
120
121
        $this->jsonUser = json_encode([
122
            'id' => $this->userId
123
        ]);
124
125
        $this->mercadolibre = new Client([
126
            'authSite' => $this->authSite,
127
            'clientId' => $this->clientId,
128
            'clientSecret' => $this->clientSecret,
129
            'redirectUri' => $this->redirectUri
130
        ]);
131
    }
132
133
    /**
134
     * Tear down test.
135
     *
136
     * @return void
137
     */
138
    public function tearDown()
139
    {
140
        parent::tearDown();
141
    }
142
143
    /**
144
     * testAuthorize method
145
     *
146
     * @return void
147
     */
148
    public function testAuthorize()
149
    {
150
        $response = Mockery::mock('GuzzleHttp\Psr7\Response');
151
        $response->shouldReceive('getHeader')->andReturn($this->headers);
152
        $response->shouldReceive('getBody')->andReturn($this->jsonToken);
153
        $client = Mockery::mock('GuzzleHttp\Client');
154
        $client->shouldReceive('send')->times(1)->andReturn($response);
155
        $this->mercadolibre->setClient($client);
156
157
        $token = $this->mercadolibre->authorize($this->code);
158
        $this->assertEquals($this->accessToken, $token->getToken());
159
        $this->assertEquals($this->refreshToken, $token->getRefreshToken());
160
        $this->assertEquals($this->userId, $token->getResourceOwnerId());
161
        $this->assertInternalType('int', $token->getExpires());
162
        $this->assertGreaterThan(time(), $token->getExpires());
163
    }
164
165
    /**
166
     * testRefresh method
167
     *
168
     * @return void
169
     */
170
    public function testRefresh()
171
    {
172
        $response = Mockery::mock('GuzzleHttp\Psr7\Response');
173
        $response->shouldReceive('getHeader')->andReturn($this->headers);
174
        $response->shouldReceive('getBody')->andReturn($this->jsonToken);
175
        $client = Mockery::mock('GuzzleHttp\Client');
176
        $client->shouldReceive('send')->times(1)->andReturn($response);
177
        $this->mercadolibre->setClient($client);
178
179
        $token = $this->mercadolibre->getToken($this->code);
180
        $token = $this->mercadolibre->refresh($token);
181
        $this->assertEquals($this->accessToken, $token->getToken());
182
        $this->assertEquals($this->refreshToken, $token->getRefreshToken());
183
        $this->assertEquals($this->userId, $token->getResourceOwnerId());
184
        $this->assertInternalType('int', $token->getExpires());
185
        $this->assertGreaterThan(time(), $token->getExpires());
186
    }
187
188
    /**
189
     * testGetOptions method
190
     *
191
     * @return void
192
     */
193
    public function testGetOptions()
194
    {
195
        $options = $this->mercadolibre->getOptions([]);
196
        $this->assertEmpty($options);
197
198
        $options = $this->mercadolibre->getOptions(['data_key' => 'data_value']);
199
        $this->assertArrayHasKey('body', $options);
200
        $this->assertArrayHasKey('headers', $options);
201
        $this->assertArrayHasKey('content-type', $options['headers']);
202
        $this->assertEquals('{"data_key":"data_value"}', $options['body']);
203
        $this->assertEquals('application/json', $options['headers']['content-type']);
204
    }
205
206
    /**
207
     * testGetPath method
208
     *
209
     * @return void
210
     */
211
    public function testGetPath()
212
    {
213
        $expected = sprintf('/users/me?access_token=%s', $this->accessToken);
214
        $actual = $this->mercadolibre->getPath('/users/me', ['access_token' => $this->accessToken]);
215
        $this->assertEquals($expected, $actual);
216
    }
217
218
    /**
219
     * testGetRequest method
220
     *
221
     * @return void
222
     */
223
    public function testGetRequest()
224
    {
225
        $response = Mockery::mock('GuzzleHttp\Psr7\Response');
226
        $response->shouldReceive('getHeader')->andReturn($this->headers);
227
        $response->shouldReceive('getBody')->andReturn($this->jsonToken);
228
        $client = Mockery::mock('GuzzleHttp\Client');
229
        $client->shouldReceive('send')->times(1)->andReturn($response);
230
        $this->mercadolibre->setClient($client);
231
        $this->mercadolibre->getToken($this->code);
232
233
        $requestQuery = ['query_key' => 'query_value'];
234
        $requestData = ['data_key' => 'data_value'];
235
        $request = $this->mercadolibre->getRequest('POST', '/users/me', $requestQuery, $requestData);
236
237
        $this->assertContains('application/json', $request->getHeader('content-type'));
238
        $this->assertEquals('{"data_key":"data_value"}', (string) $request->getBody());
239
240
        $uri = (string) $request->getUri();
241
        $uri = parse_url($uri);
242
        $this->assertArrayHasKey('scheme', $uri);
243
        $this->assertArrayHasKey('host', $uri);
244
        $this->assertArrayHasKey('path', $uri);
245
        $this->assertArrayHasKey('query', $uri);
246
        $this->assertEquals('https', $uri['scheme']);
247
        $this->assertEquals('api.mercadolibre.com', $uri['host']);
248
        $this->assertEquals('/users/me', $uri['path']);
249
250
        parse_str($uri['query'], $query);
251
        $this->assertArrayHasKey('access_token', $query);
252
        $this->assertArrayHasKey('query_key', $query);
253
        $this->assertEquals($this->accessToken, $query['access_token']);
254
        $this->assertEquals('query_value', $query['query_key']);
255
    }
256
257
    /**
258
     * testGetRequest method
259
     *
260
     * @return void
261
     */
262
    public function testGetResponse()
263
    {
264
        $response = Mockery::mock('GuzzleHttp\Psr7\Response');
265
        $response->shouldReceive('getHeader')->andReturn($this->headers);
266
        $response->shouldReceive('getBody')->andReturn($this->jsonToken);
267
        $client = Mockery::mock('GuzzleHttp\Client');
268
        $client->shouldReceive('send')->times(1)->andReturn($response);
269
        $this->mercadolibre->setClient($client);
270
        $this->mercadolibre->getToken($this->code);
271
272
        $response = Mockery::mock('GuzzleHttp\Psr7\Response');
273
        $response->shouldReceive('getHeader')->andReturn($this->headers);
274
        $response->shouldReceive('getBody')->andReturn($this->jsonUser);
275
        $client = Mockery::mock('GuzzleHttp\Client');
276
        $client->shouldReceive('send')->times(1)->andReturn($response);
277
        $this->mercadolibre->setClient($client);
278
279
        $response = $this->mercadolibre->getResponse('POST', '/users/me');
280
        $this->assertArrayHasKey('id', $response);
281
        $this->assertEquals($this->userId, $response['id']);
282
    }
283
284
    /**
285
     * testSetClient method
286
     *
287
     * @return void
288
     */
289
    public function testSetClient()
290
    {
291
        $response = Mockery::mock('GuzzleHttp\Psr7\Response');
292
        $response->shouldReceive('getHeader')->andReturn($this->headers);
293
        $response->shouldReceive('getBody')->andReturn($this->jsonToken);
294
        $client = Mockery::mock('GuzzleHttp\Client');
295
        $client->shouldReceive('send')->times(1)->andReturn($response);
296
        $provider = $this->mercadolibre->setClient($client);
297
        $this->assertEquals($client, $provider->getHttpClient());
298
    }
299
}
300