NestTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 83
dl 0
loc 203
c 1
b 0
f 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructCorrectInterfaceWithoutCustomUri() 0 9 1
A testGetAuthorizationMethod() 0 21 1
A testGetAccessTokenEndpoint() 0 11 1
A testParseAccessTokenResponseThrowsExceptionOnError() 0 14 1
A testParseAccessTokenResponseValidWithoutRefreshToken() 0 12 1
A testParseAccessTokenResponseThrowsExceptionOnNulledResponse() 0 14 1
A testGetAuthorizationEndpoint() 0 11 1
A testParseAccessTokenResponseThrowsExceptionOnErrorDescription() 0 14 1
A testConstructCorrectInstanceWithoutCustomUri() 0 9 1
A testParseAccessTokenResponseValidWithRefreshToken() 0 12 1
A testConstructCorrectInstanceWithCustomUri() 0 11 1
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use OAuth\Common\Token\TokenInterface;
6
use OAuth\OAuth2\Service\Nest;
7
use PHPUnit\Framework\TestCase;
8
9
class NestTest extends TestCase
10
{
11
    /**
12
     * @covers \OAuth\OAuth2\Service\Nest::__construct
13
     */
14
    public function testConstructCorrectInterfaceWithoutCustomUri(): void
15
    {
16
        $service = new Nest(
17
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
18
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
19
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
20
        );
21
22
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service);
23
    }
24
25
    /**
26
     * @covers \OAuth\OAuth2\Service\Nest::__construct
27
     */
28
    public function testConstructCorrectInstanceWithoutCustomUri(): void
29
    {
30
        $service = new Nest(
31
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
32
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
33
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
34
        );
35
36
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
37
    }
38
39
    /**
40
     * @covers \OAuth\OAuth2\Service\Nest::__construct
41
     */
42
    public function testConstructCorrectInstanceWithCustomUri(): void
43
    {
44
        $service = new Nest(
45
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
46
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
47
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
48
            [],
49
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
50
        );
51
52
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
53
    }
54
55
    /**
56
     * @covers \OAuth\OAuth2\Service\Nest::__construct
57
     * @covers \OAuth\OAuth2\Service\Nest::getAuthorizationEndpoint
58
     */
59
    public function testGetAuthorizationEndpoint(): void
60
    {
61
        $service = new Nest(
62
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
63
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
64
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
65
        );
66
67
        self::assertSame(
68
            'https://home.nest.com/login/oauth2',
69
            $service->getAuthorizationEndpoint()->getAbsoluteUri()
70
        );
71
    }
72
73
    /**
74
     * @covers \OAuth\OAuth2\Service\Nest::__construct
75
     * @covers \OAuth\OAuth2\Service\Nest::getAccessTokenEndpoint
76
     */
77
    public function testGetAccessTokenEndpoint(): void
78
    {
79
        $service = new Nest(
80
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
81
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
82
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
83
        );
84
85
        self::assertSame(
86
            'https://api.home.nest.com/oauth2/access_token',
87
            $service->getAccessTokenEndpoint()->getAbsoluteUri()
88
        );
89
    }
90
91
    /**
92
     * @covers \OAuth\OAuth2\Service\Nest::__construct
93
     * @covers \OAuth\OAuth2\Service\Nest::getAuthorizationMethod
94
     */
95
    public function testGetAuthorizationMethod(): void
96
    {
97
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
98
        $client->expects(self::once())->method('retrieveResponse')->willReturnArgument(0);
99
100
        $token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
101
        $token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES);
102
        $token->expects(self::once())->method('getAccessToken')->willReturn('foo');
103
104
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
105
        $storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token);
106
107
        $service = new Nest(
108
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
109
            $client,
110
            $storage
111
        );
112
113
        $uri = $service->request('https://pieterhordijk.com/my/awesome/path');
114
        $absoluteUri = parse_url($uri->getAbsoluteUri());
115
        self::assertSame('auth=foo', $absoluteUri['query']);
116
    }
117
118
    /**
119
     * @covers \OAuth\OAuth2\Service\Nest::__construct
120
     * @covers \OAuth\OAuth2\Service\Nest::parseAccessTokenResponse
121
     */
122
    public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse(): void
123
    {
124
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
125
        $client->expects(self::once())->method('retrieveResponse')->willReturn(null);
126
127
        $service = new Nest(
128
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
129
            $client,
130
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
131
        );
132
133
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
134
135
        $service->requestAccessToken('foo');
136
    }
137
138
    /**
139
     * @covers \OAuth\OAuth2\Service\Nest::__construct
140
     * @covers \OAuth\OAuth2\Service\Nest::parseAccessTokenResponse
141
     */
142
    public function testParseAccessTokenResponseThrowsExceptionOnErrorDescription(): void
143
    {
144
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
145
        $client->expects(self::once())->method('retrieveResponse')->willReturn('error_description=some_error');
146
147
        $service = new Nest(
148
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
149
            $client,
150
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
151
        );
152
153
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
154
155
        $service->requestAccessToken('foo');
156
    }
157
158
    /**
159
     * @covers \OAuth\OAuth2\Service\Nest::__construct
160
     * @covers \OAuth\OAuth2\Service\Nest::parseAccessTokenResponse
161
     */
162
    public function testParseAccessTokenResponseThrowsExceptionOnError(): void
163
    {
164
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
165
        $client->expects(self::once())->method('retrieveResponse')->willReturn('error=some_error');
166
167
        $service = new Nest(
168
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
169
            $client,
170
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
171
        );
172
173
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
174
175
        $service->requestAccessToken('foo');
176
    }
177
178
    /**
179
     * @covers \OAuth\OAuth2\Service\Nest::__construct
180
     * @covers \OAuth\OAuth2\Service\Nest::parseAccessTokenResponse
181
     */
182
    public function testParseAccessTokenResponseValidWithoutRefreshToken(): void
183
    {
184
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
185
        $client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar"}');
186
187
        $service = new Nest(
188
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
189
            $client,
190
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
191
        );
192
193
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
194
    }
195
196
    /**
197
     * @covers \OAuth\OAuth2\Service\Nest::__construct
198
     * @covers \OAuth\OAuth2\Service\Nest::parseAccessTokenResponse
199
     */
200
    public function testParseAccessTokenResponseValidWithRefreshToken(): void
201
    {
202
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
203
        $client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}');
204
205
        $service = new Nest(
206
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
207
            $client,
208
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
209
        );
210
211
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
212
    }
213
}
214