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

GigyaGrantTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 153
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testInstanceOf() 0 4 1
B testGetTokenWhenOneHasNotBeenSet() 0 35 1
B testGetTokenWithNoExpiry() 0 30 1
B testGetTokenThatExpiresCallGetTokenAgain() 0 46 1
A testNonOkResponseReturnsANullToken() 0 17 1
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\GigyaGrant;
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 GigyaGrantTest extends TestCase
28
{
29
    /** @var mixed */
30
    private $gigya;
31
    /** @var GigyaGrant */
32
    private $grant;
33
34
    /**
35
     * @return GigyaGrant
0 ignored issues
show
Documentation introduced by
Should the return type not be GigyaGrant|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
Coding Style introduced by
Function return type is not void, but function has no return statement
Loading history...
36
     */
37
    public function setUp()
38
    {
39
        $this->gigya = m::mock(Gigya::class);
40
        $this->grant = new GigyaGrant($this->gigya);
41
    }
42
43
    public function testInstanceOf()
44
    {
45
        static::assertInstanceOf(GrantInterface::class, $this->grant);
46
    }
47
48
    public function testGetTokenWhenOneHasNotBeenSet()
49
    {
50
        $response = m::mock(ResponseInterface::class);
51
52
        $this->gigya->shouldReceive('socialize->getToken')
53
                    ->with([
54
                        'grant_type' => 'none',
55
                    ], ['auth' => 'credentials'])
56
                    ->andReturn($response);
57
58
        $response->shouldReceive('getErrorCode')
59
                 ->andReturn(ErrorCode::OK);
60
        $data = m::mock(Collection::class);
61
        $response->shouldReceive('getData')
62
                 ->andReturn($data);
63
        $data->shouldReceive('get')
64
             ->with('access_token')
65
             ->andReturn('some_token');
66
        $data->shouldReceive('has')
67
             ->with('expires_in')
68
             ->andReturn(true);
69
        $data->shouldReceive('get')
70
             ->with('expires_in', 0)
71
             ->andReturn(3600);
72
73
        $token = $this->grant->getToken();
74
75
        $expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', 3600)));
76
77
        static::assertEquals('some_token', $token->getToken());
78
        static::assertLessThanOrEqual($expires, $token->getExpires());
79
        static::assertFalse($token->isExpired());
80
81
        static::assertSame($token, $this->grant->getToken(), 'Calling getToken again, should return the same token');
82
    }
83
84
    public function testGetTokenWithNoExpiry()
85
    {
86
        $response = m::mock(ResponseInterface::class);
87
88
        $this->gigya->shouldReceive('socialize->getToken')
89
                    ->with([
90
                        'grant_type' => 'none',
91
                    ], ['auth' => 'credentials'])
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
        $data->shouldReceive('get')
100
             ->with('access_token')
101
             ->andReturn('some_token');
102
        $data->shouldReceive('has')
103
             ->with('expires_in')
104
             ->andReturn(false);
105
106
        $token = $this->grant->getToken();
107
108
        static::assertEquals('some_token', $token->getToken());
109
        static::assertNull($token->getExpires());
110
        static::assertFalse($token->isExpired());
111
112
        static::assertSame($token, $this->grant->getToken(), 'Calling getToken again, should return the same token');
113
    }
114
115
    public function testGetTokenThatExpiresCallGetTokenAgain()
116
    {
117
        $response = m::mock(ResponseInterface::class);
118
119
        $this->gigya->shouldReceive('socialize->getToken')
120
                    ->with([
121
                        'grant_type' => 'none',
122
                    ], ['auth' => 'credentials'])
123
                    ->twice()
124
                    ->andReturn($response);
125
126
        $response->shouldReceive('getErrorCode')
127
                 ->twice()
128
                 ->andReturn(ErrorCode::OK);
129
        $data = m::mock(Collection::class);
130
        $response->shouldReceive('getData')
131
                 ->andReturn($data);
132
        $data->shouldReceive('get')
133
             ->with('access_token')
134
             ->twice()
135
             ->andReturn('some_token');
136
        $data->shouldReceive('has')
137
             ->with('expires_in')
138
             ->twice()
139
             ->andReturn(true);
140
        $data->shouldReceive('get')
141
             ->with('expires_in', 0)
142
             ->twice()
143
             ->andReturn(0);
144
145
        $token = $this->grant->getToken();
146
147
        $expires = new DateTime();
148
149
        static::assertEquals('some_token', $token->getToken());
150
        static::assertLessThanOrEqual($expires, $token->getExpires());
151
        static::assertTrue($token->isExpired());
152
153
        $newToken = $this->grant->getToken();
154
155
        static::assertNotSame($token, $newToken, 'Calling getToken again, should return the same token');
156
157
        static::assertEquals('some_token', $token->getToken());
158
        static::assertLessThanOrEqual($expires, $token->getExpires());
159
        static::assertTrue($token->isExpired());
160
    }
161
162
    public function testNonOkResponseReturnsANullToken()
163
    {
164
        $response = m::mock(ResponseInterface::class);
165
166
        $this->gigya->shouldReceive('socialize->getToken')
167
                    ->with([
168
                        'grant_type' => 'none',
169
                    ], ['auth' => 'credentials'])
170
                    ->andReturn($response);
171
172
        $response->shouldReceive('getErrorCode')
173
                 ->andReturn(ErrorCode::ERROR_GENERAL_SERVER_ERROR);
174
175
        $token = $this->grant->getToken();
176
177
        static::assertNull($token);
178
    }
179
}
180