Overview

Namespaces

  • Docta
    • MercadoLibre
      • Exception
      • OAuth2
        • Client
          • Test
  • GuzzleHttp
    • Cookie
    • Exception
    • Handler
    • Promise
      • Test
      • Tests
    • Psr7
    • Test
      • Handler
    • Tests
      • CookieJar
      • Event
      • Exception
      • Handler
      • Promise
      • Psr7
  • League
    • OAuth2
      • Client
        • Grant
          • Exception
        • Provider
          • Exception
        • Test
          • Grant
          • Provider
            • Exception
            • Fake
          • Token
          • Tool
        • Token
        • Tool
  • None
  • Psr
    • Http
      • Message

Classes

  • Docta\MercadoLibre\Client
  • Docta\MercadoLibre\OAuth2\Client\Provider
  • Docta\MercadoLibre\OAuth2\Client\ResourceGeneric
  • Docta\MercadoLibre\OAuth2\Client\ResourceOwner
  • Docta\MercadoLibre\OAuth2\Client\Test\ProviderTest

Exceptions

  • Docta\MercadoLibre\Exception\ClientException
  • Overview
  • Namespace
  • Class
  • Download
  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: 
<?php
/**
 * MercadoLibre Provider for OAuth 2.0 Client
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE file
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright 2018 Lucas Banegas <lucasconobanegas@gmail.com>
 * @license https://opensource.org/licenses/MIT MIT License
 * @author Lucas Banegas <lucasconobanegas@gmail.com>
 * @link https://github.com/docta/oauth2-mercadolibre Repository
 * @link https://docta.github.io/oauth2-mercadolibre Documentation
 */
namespace Docta\MercadoLibre\OAuth2\Client\Test;

use Docta\MercadoLibre\OAuth2\Client\Provider;
use Docta\MercadoLibre\OAuth2\Client\ResourceOwner;
use InvalidArgumentException;
use League\OAuth2\Client\Grant\AbstractGrant;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;

/**
 * Represents a service provider that can be used to interact with the MercadoLibre
 * OAuth 2.0 service provider, using bearer token authentication.
 */
class ProviderTest extends TestCase
{
    /**
     * @var array Options
     */
    protected $options = [
        'authSite' => 'MLA',
        'clientId' => 'mockClientId',
        'clientSecret' => 'mockClientSecret',
        'redirectUri' => 'http://mockRedirectUri.com/'
    ];

    /**
     * @var Provider Provider
     */
    protected $provider;

    /**
     * Setup test.
     *
     * @return void
     */
    protected function setUp()
    {
        $this->provider = new Provider($this->options);
    }

    /**
     * Tear down test.
     *
     * @return void
     */
    public function tearDown()
    {
        Mockery::close();
        parent::tearDown();
    }

    /**
     * testAssertRequiredOptions
     *
     * @return void
     */
    public function testAssertRequiredOptions()
    {
        $msg = 'Required options not defined: authSite, clientId, clientSecret, redirectUri';

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage($msg);
        $this->provider = new Provider();
    }

    /**
     * testAssertAuthSite
     *
     * @return void
     */
    public function testAssertAuthSite()
    {
        $msg = 'Valid values for authSite are only: MLA, MLB, MCO, MCR, MEC, MLC, MLM, MLU, MLV, MPA, MPE, MPT, MRD';

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage($msg);
        $this->options['authSite'] = 'invalidAuthSite';
        $this->provider = new Provider($this->options);
    }

    /**
     * testGetAuthorizationUrl
     *
     * @return void
     */
    public function testGetAuthorizationUrl()
    {
        $url = $this->provider->getAuthorizationUrl();
        $this->assertInternalType('string', $url);

        $url = parse_url($url);
        $this->assertArrayHasKey('scheme', $url);
        $this->assertArrayHasKey('host', $url);
        $this->assertArrayHasKey('path', $url);
        $this->assertArrayHasKey('query', $url);
        $this->assertSame('https', $url['scheme']);
        $this->assertSame('auth.mercadolibre.com.ar', $url['host']);
        $this->assertSame('/authorization', $url['path']);

        parse_str($url['query'], $query);
        $this->assertArrayNotHasKey('approval_prompt', $query);
        $this->assertArrayHasKey('response_type', $query);
        $this->assertArrayHasKey('client_id', $query);
        $this->assertArrayHasKey('redirect_uri', $query);
        $this->assertArrayHasKey('state', $query);
        $this->assertSame('code', $query['response_type']);
        $this->assertSame($this->options['clientId'], $query['client_id']);
        $this->assertSame($this->options['redirectUri'], $query['redirect_uri']);
        $this->assertSame($this->provider->getState(), $query['state']);
    }

    /**
     * testGetApiUrl
     *
     * @return void
     */
    public function testGetApiUrl()
    {
        $expected = 'https://api.mercadolibre.com/';
        $this->assertSame($expected, $this->provider->getApiUrl());
    }

    /**
     * testGetApiUrlWithPath
     *
     * @return void
     */
    public function testGetApiUrlWithPath()
    {
        $expected = 'https://api.mercadolibre.com/test';
        $this->assertSame($expected, $this->provider->getApiUrl('/test'));
    }

    /**
     * testGetApiUrlWithPathAndQuery
     *
     * @return void
     */
    public function testGetApiUrlWithPathAndQuery()
    {
        $expected = 'https://api.mercadolibre.com/test?key=value';
        $this->assertSame($expected, $this->provider->getApiUrl('/test', ['key' => 'value']));
    }

    /**
     * testGetBaseAuthorizationUrl
     *
     * @return void
     */
    public function testGetBaseAuthorizationUrl()
    {
        $expected = 'https://auth.mercadolibre.com.ar/authorization';
        $this->assertSame($expected, $this->provider->getBaseAuthorizationUrl());
    }

    /**
     * testGetBaseAccessTokenUrl
     *
     * @return void
     */
    public function testGetBaseAccessTokenUrl()
    {
        $expected = 'https://api.mercadolibre.com/oauth/token';
        $this->assertSame($expected, $this->provider->getBaseAccessTokenUrl());
    }

    /**
     * testGetResourceOwnerDetailsUrl
     *
     * @return void
     */
    public function testGetResourceOwnerDetailsUrl()
    {
        $expected = 'https://api.mercadolibre.com/users/me';
        $this->assertSame($expected, $this->provider->getResourceOwnerDetailsUrl());
    }

    /**
     * testGetDefaultScopes
     *
     * @return void
     */
    public function testGetDefaultScopes()
    {
        $this->assertNull($this->provider->getDefaultScopes());
    }

    /**
     * testGetAuthenticatedRequest
     *
     * @return void
     */
    public function testGetAuthenticatedRequest()
    {
        $url = $this->provider->getResourceOwnerDetailsUrl();
        $req = $this->provider->getAuthenticatedRequest('POST', $url, 'mockToken');
        $this->assertInstanceOf(RequestInterface::class, $req);
        $this->assertSame('POST', $req->getMethod());
        $this->assertSame('https', $req->getUri()->getScheme());
        $this->assertSame('api.mercadolibre.com', $req->getUri()->getHost());
        $this->assertSame('/users/me', $req->getUri()->getPath());
        $this->assertContains('access_token=mockToken', $req->getUri()->getQuery());
    }

    /**
     * testCheckResponse
     *
     * @return void
     */
    public function testCheckResponse()
    {
        $body = [
            'token_type' => 'bearer',
            'access_token' => 'mockAccessToken',
            'refresh_token' => 'mockRefreshToken',
            'expires' => time() + (6 * 60 * 60),
            'user_id' => 'mockUserId'
        ];

        $response = Mockery::mock('Psr\Http\Message\ResponseInterface');
        $response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
        $response->shouldReceive('getBody')->andReturn(json_encode($body));
        $client = Mockery::mock('GuzzleHttp\ClientInterface');
        $client->shouldReceive('send')->times(1)->andReturn($response);
        $this->provider->setHttpClient($client);

        $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mockCode']);
        $this->assertSame($body['access_token'], $token->getToken());
        $this->assertSame($body['refresh_token'], $token->getRefreshToken());
        $this->assertSame($body['expires'], $token->getExpires());
        $this->assertSame($body['user_id'], $token->getResourceOwnerId());
        $this->assertFalse($token->hasExpired());
    }

    /**
     * testCheckResponseException
     *
     * @return void
     */
    public function testCheckResponseException()
    {
        $error = [
            'error' => 'mockError',
            'status' => 400
        ];

        $response = Mockery::mock('Psr\Http\Message\ResponseInterface');
        $response->shouldReceive('getStatusCode')->andReturn($error['status']);
        $response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
        $response->shouldReceive('getBody')->andReturn(json_encode($error));
        $client = Mockery::mock('GuzzleHttp\ClientInterface');
        $client->shouldReceive('send')->times(1)->andReturn($response);
        $this->provider->setHttpClient($client);

        $this->expectException(IdentityProviderException::class);
        $this->expectExceptionMessage($error['error']);
        $this->expectExceptionCode($error['status']);
        $this->provider->getAccessToken('authorization_code', ['code' => 'mockCode']);
    }

    /**
     * testCreateResourceOwner
     *
     * @return void
     */
    public function testCreateResourceOwner()
    {
        $tokenBody = [
            'token_type' => 'bearer',
            'access_token' => 'mockAccessToken',
            'refresh_token' => 'mockRefreshToken',
            'expires' => time() + (6 * 60 * 60),
            'user_id' => 'mockUserId'
        ];

        $ownerBody = [
            'id' => 'mockId',
            'first_name' => 'mockFirstName',
            'last_name' => 'mockLastName',
            'address' => [
                'address' => 'mockAddress',
                'country' => 'mockCountry'
            ]
        ];

        $response = Mockery::mock('Psr\Http\Message\ResponseInterface');
        $response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
        $response->shouldReceive('getBody')->andReturn(json_encode($tokenBody));
        $client = Mockery::mock('GuzzleHttp\ClientInterface');
        $client->shouldReceive('send')->times(1)->andReturn($response);
        $this->provider->setHttpClient($client);
        $token = $this->provider->getAccessToken('authorization_code', ['code' => 'mockCode']);

        $response = Mockery::mock('Psr\Http\Message\ResponseInterface');
        $response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
        $response->shouldReceive('getBody')->andReturn(json_encode($ownerBody));
        $client = Mockery::mock('GuzzleHttp\ClientInterface');
        $client->shouldReceive('send')->times(1)->andReturn($response);
        $this->provider->setHttpClient($client);
        $owner = $this->provider->getResourceOwner($token);

        $this->assertInstanceOf(ResourceOwner::class, $owner);
        $this->assertSame($ownerBody['id'], $owner->getId());
        $this->assertSame($ownerBody['first_name'], $owner->get('first_name'));
        $this->assertSame($ownerBody['last_name'], $owner->get('last_name'));
        $this->assertSame($ownerBody['address']['address'], $owner->get('address.address'));
        $this->assertSame($ownerBody['address']['country'], $owner->get('address.country'));
        $this->assertSame($ownerBody, $owner->toArray());
    }
}
MercadoLibre PHP SDK API documentation generated by ApiGen