Completed
Pull Request — master (#4)
by Laurens
02:47
created

RequestNewAccessTokenTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 2
c 4
b 0
f 1
lcom 1
cbo 10
dl 0
loc 56
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testReturnedDataAccessTokenEquals() 0 35 1
A testClientException() 0 14 1
1
<?php
2
3
namespace Tests\Werkspot\BingAdsApiBundle\Guzzle;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Handler\MockHandler;
8
use GuzzleHttp\HandlerStack;
9
use GuzzleHttp\Psr7\Response;
10
use Mockery;
11
use PHPUnit_Framework_TestCase;
12
use Werkspot\BingAdsApiBundle\Guzzle\OauthTokenService;
13
use Werkspot\BingAdsApiBundle\Model\AccessToken;
14
15
class RequestNewAccessTokenTest extends PHPUnit_Framework_TestCase
16
{
17
    private $accessToken = '2ec09aeccaf634d982eec793037e37fe';
18
    private $refreshToken = '0c59f7e609b0cc467067e39d523116ce';
19
20
    public function testReturnedDataAccessTokenEquals()
21
    {
22
        $data = [
23
            'token_type' => 'bearer',
24
            'expires_in' => 3600,
25
            'scope' => 'bingads.manage',
26
            'access_token' => $this->accessToken,
27
            'refresh_token' => $this->refreshToken,
28
            'user_id' => '9fae2e51d79512efe5fe139a8ae9f885'
29
        ];
30
31
        $clientMock = Mockery::mock(Client::class);
32
        $clientMock
33
            ->shouldReceive('request')
34
            ->with('POST', OauthTokenService::URL, [
35
                'headers' => [
36
                    'Content-Type' => 'application/x-www-form-urlencoded'
37
                ],
38
                'form_params' => [
39
                    'client_id' => 'client_id',
40
                    'client_secret' => 'client_secret',
41
                    'grant_type' => OauthTokenService::GRANTTYPE,
42
                    'redirect_uri' => 'redirect_uri',
43
                    'refresh_token' => $this->refreshToken
44
                ]
45
            ])
46
            ->once()
47
            ->andReturn(new Response(200, [], json_encode($data)));
48
49
        $guzzleClient = new OauthTokenService($clientMock);
50
        $response = $guzzleClient->refreshToken('client_id', 'client_secret', 'redirect_uri', new AccessToken(null, $this->refreshToken));
51
52
        $this->assertEquals($response->getAccessToken(), $this->accessToken);
53
        $this->assertEquals($response->getRefreshToken(), $this->refreshToken);
54
    }
55
56
    public function testClientException()
57
    {
58
        $this->expectException(ClientException::class);
59
60
        $data = [
61
            'error' => 'invalid_grant',
62
            'error_description' => "The provided value for the input parameter 'refresh_token' is not valid."
63
        ];
64
        $mock = new MockHandler([new Response(400, [], json_encode($data))]);
65
        $handler = HandlerStack::create($mock);
66
        $client = new Client(['handler' => $handler]);
67
        $guzzleClient = new OauthTokenService($client);
68
        $guzzleClient->refreshToken(null, null, null, new AccessToken(null, null));
69
    }
70
}
71