Completed
Push — master ( 3624f7...34c464 )
by Winfred
15s
created

RequestNewAccessTokenTest::testReturnedDataAccessTokenEquals()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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