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

GigyaCodeGrantTest::testGetToken()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 26

Duplication

Lines 34
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 34
loc 34
rs 8.8571
cc 1
eloc 26
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
     *
25
     * @return GigyaCodeGrant
26
     */
27
    private function getGrant($code, $uri)
28
    {
29
        $this->gigya = m::mock(Gigya::class);
30
        return new GigyaCodeGrant($this->gigya, $code, $uri);
31
    }
32
33
    public function testInstanceOf()
34
    {
35
        $grant = $this->getGrant('code', 'uri');
36
        static::assertInstanceOf(GrantInterface::class, $grant);
37
    }
38
39 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...
40
    {
41
        $grant = $this->getGrant('code', 'uri');
42
43
        $response = m::mock(ResponseInterface::class);
44
        $this->gigya->shouldReceive('socialize->getToken')
45
                    ->with([
46
                        'authorization_code' => 'code',
47
                        'redirect_uri'       => 'uri',
48
                        'grant_type'         => 'code',
49
                    ], ['auth' => 'none'])
50
                    ->andReturn($response);
51
52
        $response->shouldReceive('getErrorCode')
53
                 ->andReturn(ErrorCode::OK);
54
        $data = m::mock(Collection::class);
55
        $response->shouldReceive('getData')
56
                 ->andReturn($data);
57
58
        $data->shouldReceive('get')
59
             ->with('access_token')
60
             ->andReturn('some_access_token');
61
        $data->shouldReceive('get')
62
             ->with('expires_in', null)
63
             ->andReturn(3600);
64
65
        $token = $grant->getToken();
66
67
        $expires = (new DateTime())->add(new DateInterval(sprintf('PT%dS', 3600)));
68
69
        static::assertEquals('some_access_token', $token->getToken());
70
        static::assertLessThanOrEqual($expires, $token->getExpires());
71
        static::assertFalse($token->isExpired());
72
    }
73
74
    public function testGetTokenWithNoExpiresReturnsAnUnExpiredToken()
75
    {
76
        $grant = $this->getGrant('code', 'uri');
77
78
        $response = m::mock(ResponseInterface::class);
79
        $this->gigya->shouldReceive('socialize->getToken')
80
                    ->with([
81
                        'authorization_code' => 'code',
82
                        'redirect_uri'       => 'uri',
83
                        'grant_type'         => 'code',
84
                    ], ['auth' => 'none'])
85
                    ->andReturn($response);
86
87
        $response->shouldReceive('getErrorCode')
88
                 ->andReturn(ErrorCode::OK);
89
        $data = m::mock(Collection::class);
90
        $response->shouldReceive('getData')
91
                 ->andReturn($data);
92
93
        $data->shouldReceive('get')
94
             ->with('access_token')
95
             ->andReturn('some_access_token');
96
        $data->shouldReceive('get')
97
             ->with('expires_in', null)
98
             ->andReturn(null);
99
100
        $token = $grant->getToken();
101
102
        static::assertEquals('some_access_token', $token->getToken());
103
        static::assertNull($token->getExpires());
104
        static::assertFalse($token->isExpired());
105
    }
106
107 View Code Duplication
    public function testGetTokenCalledTwiceWillAlwaysReturnTheOriginalToken()
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...
108
    {
109
        $grant = $this->getGrant('code', 'uri');
110
111
        $response = m::mock(ResponseInterface::class);
112
        $this->gigya->shouldReceive('socialize->getToken')
113
                    ->with([
114
                        'authorization_code' => 'code',
115
                        'redirect_uri'       => 'uri',
116
                        'grant_type'         => 'code',
117
                    ], ['auth' => 'none'])
118
                    ->andReturn($response);
119
120
        $response->shouldReceive('getErrorCode')
121
                 ->andReturn(ErrorCode::OK);
122
        $data = m::mock(Collection::class);
123
        $response->shouldReceive('getData')
124
                 ->andReturn($data);
125
126
        $data->shouldReceive('get')
127
             ->with('access_token')
128
             ->andReturn('some_access_token');
129
        $data->shouldReceive('get')
130
             ->with('expires_in', null)
131
             ->andReturn(0);
132
133
        $token = $grant->getToken();
134
135
        $expires = new DateTime();
136
137
        static::assertEquals('some_access_token', $token->getToken());
138
        static::assertLessThanOrEqual($expires, $token->getExpires());
139
        static::assertTrue($token->isExpired());
140
141
        static::assertSame($token, $grant->getToken());
142
    }
143
}
144