Completed
Pull Request — master (#20)
by Harry
13:19
created

testGetTokenCalledTwiceWillAlwaysReturnTheOriginalToken()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 39
rs 8.8571
cc 1
eloc 30
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Test\Unit\Auth\OAuth2;
15
16
use DateInterval;
17
use DateTime;
18
use Graze\Gigya\Auth\OAuth2\GigyaCodeGrant;
19
use Graze\Gigya\Auth\OAuth2\GrantInterface;
20
use Graze\Gigya\Gigya;
21
use Graze\Gigya\Response\ErrorCode;
22
use Graze\Gigya\Response\ResponseInterface;
23
use Graze\Gigya\Test\TestCase;
24
use Illuminate\Support\Collection;
25
use Mockery as m;
26
27
class GigyaCodeGrantTest extends TestCase
28
{
29
    /** @var mixed */
30
    private $gigya;
31
32
    /**
33
     * @param string $code
34
     * @param string $uri
35
     *
36
     * @return GigyaCodeGrant
37
     */
38
    private function getGrant($code, $uri)
39
    {
40
        $this->gigya = m::mock(Gigya::class);
41
        return new GigyaCodeGrant($this->gigya, $code, $uri);
42
    }
43
44
    public function testInstanceOf()
45
    {
46
        $grant = $this->getGrant('code', 'uri');
47
        static::assertInstanceOf(GrantInterface::class, $grant);
48
    }
49
50 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...
51
    {
52
        $grant = $this->getGrant('code', 'uri');
53
54
        $response = m::mock(ResponseInterface::class);
55
        $this->gigya->shouldReceive('socialize->getToken')
56
                    ->with([
57
                        'code'         => 'code',
58
                        'redirect_uri' => 'uri',
59
                        'grant_type'   => 'authorization_code',
60
                    ], ['auth' => 'credentials'])
61
                    ->andReturn($response);
62
63
        $response->shouldReceive('getErrorCode')
64
                 ->andReturn(ErrorCode::OK);
65
        $data = m::mock(Collection::class);
66
        $response->shouldReceive('getData')
67
                 ->andReturn($data);
68
69
        $data->shouldReceive('get')
70
             ->with('access_token')
71
             ->andReturn('some_access_token');
72
        $data->shouldReceive('has')
73
             ->with('expires_in')
74
             ->andReturn(true);
75
        $data->shouldReceive('get')
76
             ->with('expires_in', 0)
77
             ->andReturn(3600);
78
79
        $token = $grant->getToken();
80
81
        $expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', 3600)));
82
83
        static::assertEquals('some_access_token', $token->getToken());
84
        static::assertLessThanOrEqual($expires, $token->getExpires());
85
        static::assertFalse($token->isExpired());
86
    }
87
88
    public function testGetTokenWithNoExpiresReturnsAnUnExpiredToken()
89
    {
90
        $grant = $this->getGrant('code', 'uri');
91
92
        $response = m::mock(ResponseInterface::class);
93
        $this->gigya->shouldReceive('socialize->getToken')
94
                    ->with([
95
                        'code'         => 'code',
96
                        'redirect_uri' => 'uri',
97
                        'grant_type'   => 'authorization_code',
98
                    ], ['auth' => 'credentials'])
99
                    ->andReturn($response);
100
101
        $response->shouldReceive('getErrorCode')
102
                 ->andReturn(ErrorCode::OK);
103
        $data = m::mock(Collection::class);
104
        $response->shouldReceive('getData')
105
                 ->andReturn($data);
106
107
        $data->shouldReceive('get')
108
             ->with('access_token')
109
             ->andReturn('some_access_token');
110
        $data->shouldReceive('has')
111
             ->with('expires_in')
112
             ->andReturn(false);
113
114
        $token = $grant->getToken();
115
116
        static::assertEquals('some_access_token', $token->getToken());
117
        static::assertNull($token->getExpires());
118
        static::assertFalse($token->isExpired());
119
    }
120
121
    public function testGetTokenCalledTwiceWillAlwaysReturnTheOriginalToken()
122
    {
123
        $grant = $this->getGrant('code', 'uri');
124
125
        $response = m::mock(ResponseInterface::class);
126
        $this->gigya->shouldReceive('socialize->getToken')
127
                    ->with([
128
                        'code'         => 'code',
129
                        'redirect_uri' => 'uri',
130
                        'grant_type'   => 'authorization_code',
131
                    ], ['auth' => 'credentials'])
132
                    ->andReturn($response);
133
134
        $response->shouldReceive('getErrorCode')
135
                 ->andReturn(ErrorCode::OK);
136
        $data = m::mock(Collection::class);
137
        $response->shouldReceive('getData')
138
                 ->andReturn($data);
139
140
        $data->shouldReceive('get')
141
             ->with('access_token')
142
             ->andReturn('some_access_token');
143
        $data->shouldReceive('has')
144
             ->with('expires_in')
145
             ->andReturn(true);
146
        $data->shouldReceive('get')
147
             ->with('expires_in', 0)
148
             ->andReturn(0);
149
150
        $token = $grant->getToken();
151
152
        $expires = new DateTime();
153
154
        static::assertEquals('some_access_token', $token->getToken());
155
        static::assertLessThanOrEqual($expires, $token->getExpires());
156
        static::assertTrue($token->isExpired());
157
158
        static::assertSame($token, $grant->getToken());
159
    }
160
161 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...
162
    {
163
        $grant = $this->getGrant('code', 'uri', 'siteKey', 'siteSecret', 'userKey');
0 ignored issues
show
Unused Code introduced by
The call to GigyaCodeGrantTest::getGrant() has too many arguments starting with 'siteKey'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
164
165
        $response = m::mock(ResponseInterface::class);
166
        $this->gigya->shouldReceive('socialize->getToken')
167
                    ->with([
168
                        'code'         => 'code',
169
                        'redirect_uri' => 'uri',
170
                        'grant_type'   => 'authorization_code',
171
                    ], ['auth' => 'credentials'])
172
                    ->andReturn($response);
173
174
        $response->shouldReceive('getErrorCode')
175
                 ->andReturn(ErrorCode::OK);
176
        $data = m::mock(Collection::class);
177
        $response->shouldReceive('getData')
178
                 ->andReturn($data);
179
180
        $data->shouldReceive('get')
181
             ->with('access_token')
182
             ->andReturn('some_access_token');
183
        $data->shouldReceive('has')
184
             ->with('expires_in')
185
             ->andReturn(true);
186
        $data->shouldReceive('get')
187
             ->with('expires_in', null)
188
             ->andReturn(3600);
189
190
        $token = $grant->getToken();
191
192
        $expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', 3600)));
193
194
        static::assertEquals('some_access_token', $token->getToken());
195
        static::assertLessThanOrEqual($expires, $token->getExpires());
196
        static::assertFalse($token->isExpired());
197
    }
198
}
199