Completed
Pull Request — master (#20)
by Harry
08:55
created

GigyaCodeGrantTest::testGetTokenWithUserKey()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 28

Duplication

Lines 36
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 36
loc 36
rs 8.8571
cc 1
eloc 28
nc 1
nop 0
1
<?php
2
3
namespace Graze\Gigya\Test\Unit\Auth\OAuth2;
4
5
use DateInterval;
6
use DateTime;
7
use Graze\Gigya\Auth\OAuth2\GigyaCodeGrant;
8
use Graze\Gigya\Auth\OAuth2\GrantInterface;
9
use Graze\Gigya\Gigya;
10
use Graze\Gigya\Response\ErrorCode;
11
use Graze\Gigya\Response\ResponseInterface;
12
use Graze\Gigya\Test\TestCase;
13
use Illuminate\Support\Collection;
14
use Mockery as m;
15
16
class GigyaCodeGrantTest extends TestCase
17
{
18
    /** @var mixed */
19
    private $gigya;
20
21
    /**
22
     * @param string      $code
23
     * @param string      $uri
24
     * @param string      $key
25
     * @param string      $secret
26
     * @param string|null $user
27
     *
28
     * @return GigyaCodeGrant
29
     */
30
    private function getGrant($code, $uri, $key = 'key', $secret = 'secret', $user = null)
31
    {
32
        $this->gigya = m::mock(Gigya::class);
33
        return new GigyaCodeGrant($this->gigya, $code, $uri, $key, $secret, $user);
34
    }
35
36
    public function testInstanceOf()
37
    {
38
        $grant = $this->getGrant('code', 'uri');
39
        static::assertInstanceOf(GrantInterface::class, $grant);
40
    }
41
42 View Code Duplication
    public function testGetToken()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
    {
44
        $grant = $this->getGrant('code', 'uri');
45
46
        $response = m::mock(ResponseInterface::class);
47
        $this->gigya->shouldReceive('socialize->getToken')
48
                    ->with([
49
                        'client_id'     => 'key',
50
                        'client_secret' => 'secret',
51
                        'code'          => 'code',
52
                        'redirect_uri'  => 'uri',
53
                        'grant_type'    => 'authorization_code',
54
                    ], ['auth' => 'none'])
55
                    ->andReturn($response);
56
57
        $response->shouldReceive('getErrorCode')
58
                 ->andReturn(ErrorCode::OK);
59
        $data = m::mock(Collection::class);
60
        $response->shouldReceive('getData')
61
                 ->andReturn($data);
62
63
        $data->shouldReceive('get')
64
             ->with('access_token')
65
             ->andReturn('some_access_token');
66
        $data->shouldReceive('get')
67
             ->with('expires_in', null)
68
             ->andReturn(3600);
69
70
        $token = $grant->getToken();
71
72
        $expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', 3600)));
73
74
        static::assertEquals('some_access_token', $token->getToken());
75
        static::assertLessThanOrEqual($expires, $token->getExpires());
76
        static::assertFalse($token->isExpired());
77
    }
78
79
    public function testGetTokenWithNoExpiresReturnsAnUnExpiredToken()
80
    {
81
        $grant = $this->getGrant('code', 'uri');
82
83
        $response = m::mock(ResponseInterface::class);
84
        $this->gigya->shouldReceive('socialize->getToken')
85
                    ->with([
86
                        'client_id'     => 'key',
87
                        'client_secret' => 'secret',
88
                        'code'          => 'code',
89
                        'redirect_uri'  => 'uri',
90
                        'grant_type'    => 'authorization_code',
91
                    ], ['auth' => 'none'])
92
                    ->andReturn($response);
93
94
        $response->shouldReceive('getErrorCode')
95
                 ->andReturn(ErrorCode::OK);
96
        $data = m::mock(Collection::class);
97
        $response->shouldReceive('getData')
98
                 ->andReturn($data);
99
100
        $data->shouldReceive('get')
101
             ->with('access_token')
102
             ->andReturn('some_access_token');
103
        $data->shouldReceive('get')
104
             ->with('expires_in', null)
105
             ->andReturn(null);
106
107
        $token = $grant->getToken();
108
109
        static::assertEquals('some_access_token', $token->getToken());
110
        static::assertNull($token->getExpires());
111
        static::assertFalse($token->isExpired());
112
    }
113
114
    public function testGetTokenCalledTwiceWillAlwaysReturnTheOriginalToken()
115
    {
116
        $grant = $this->getGrant('code', 'uri');
117
118
        $response = m::mock(ResponseInterface::class);
119
        $this->gigya->shouldReceive('socialize->getToken')
120
                    ->with([
121
                        'client_id'     => 'key',
122
                        'client_secret' => 'secret',
123
                        'code'          => 'code',
124
                        'redirect_uri'  => 'uri',
125
                        'grant_type'    => 'authorization_code',
126
                    ], ['auth' => 'none'])
127
                    ->andReturn($response);
128
129
        $response->shouldReceive('getErrorCode')
130
                 ->andReturn(ErrorCode::OK);
131
        $data = m::mock(Collection::class);
132
        $response->shouldReceive('getData')
133
                 ->andReturn($data);
134
135
        $data->shouldReceive('get')
136
             ->with('access_token')
137
             ->andReturn('some_access_token');
138
        $data->shouldReceive('get')
139
             ->with('expires_in', null)
140
             ->andReturn(0);
141
142
        $token = $grant->getToken();
143
144
        $expires = new DateTime();
145
146
        static::assertEquals('some_access_token', $token->getToken());
147
        static::assertLessThanOrEqual($expires, $token->getExpires());
148
        static::assertTrue($token->isExpired());
149
150
        static::assertSame($token, $grant->getToken());
151
    }
152
153 View Code Duplication
    public function testGetTokenWithUserKey()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        $grant = $this->getGrant('code', 'uri', 'siteKey', 'siteSecret', 'userKey');
156
157
        $response = m::mock(ResponseInterface::class);
158
        $this->gigya->shouldReceive('socialize->getToken')
159
                    ->with([
160
                        'client_id'     => 'userKey',
161
                        'client_secret' => 'siteSecret',
162
                        'code'          => 'code',
163
                        'redirect_uri'  => 'uri',
164
                        'grant_type'    => 'authorization_code',
165
                    ], ['auth' => 'none'])
166
                    ->andReturn($response);
167
168
        $response->shouldReceive('getErrorCode')
169
                 ->andReturn(ErrorCode::OK);
170
        $data = m::mock(Collection::class);
171
        $response->shouldReceive('getData')
172
                 ->andReturn($data);
173
174
        $data->shouldReceive('get')
175
             ->with('access_token')
176
             ->andReturn('some_access_token');
177
        $data->shouldReceive('get')
178
             ->with('expires_in', null)
179
             ->andReturn(3600);
180
181
        $token = $grant->getToken();
182
183
        $expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', 3600)));
184
185
        static::assertEquals('some_access_token', $token->getToken());
186
        static::assertLessThanOrEqual($expires, $token->getExpires());
187
        static::assertFalse($token->isExpired());
188
    }
189
}
190