testParseAccessTokenResponseThrowsExceptionOnError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 14
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use OAuth\OAuth2\Service\Netatmo;
6
use PHPUnit\Framework\TestCase;
7
8
class NetatmoTest extends TestCase
9
{
10
    /**
11
     * @covers \OAuth\OAuth2\Service\Netatmo::__construct
12
     */
13
    public function testConstructCorrectInterfaceWithoutCustomUri(): void
14
    {
15
        $service = new Netatmo(
16
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
17
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
18
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
19
        );
20
21
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service);
22
    }
23
24
    /**
25
     * @covers \OAuth\OAuth2\Service\Netatmo::__construct
26
     */
27
    public function testConstructCorrectInstanceWithoutCustomUri(): void
28
    {
29
        $service = new Netatmo(
30
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
31
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
32
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
33
        );
34
35
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
36
    }
37
38
    /**
39
     * @covers \OAuth\OAuth2\Service\Netatmo::__construct
40
     */
41
    public function testConstructCorrectInstanceWithCustomUri(): void
42
    {
43
        $service = new Netatmo(
44
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
45
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
46
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
47
            [],
48
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
49
        );
50
51
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
52
    }
53
54
    /**
55
     * @covers \OAuth\OAuth2\Service\Netatmo::__construct
56
     * @covers \OAuth\OAuth2\Service\Netatmo::getAuthorizationEndpoint
57
     */
58
    public function testGetAuthorizationEndpoint(): void
59
    {
60
        $service = new Netatmo(
61
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
62
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
63
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
64
        );
65
66
        self::assertSame(
67
            'https://api.netatmo.net/oauth2/authorize',
68
            $service->getAuthorizationEndpoint()->getAbsoluteUri()
69
        );
70
    }
71
72
    /**
73
     * @covers \OAuth\OAuth2\Service\Netatmo::__construct
74
     * @covers \OAuth\OAuth2\Service\Netatmo::getAccessTokenEndpoint
75
     */
76
    public function testGetAccessTokenEndpoint(): void
77
    {
78
        $service = new Netatmo(
79
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
80
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
81
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
82
        );
83
84
        self::assertSame(
85
            'https://api.netatmo.net/oauth2/token',
86
            $service->getAccessTokenEndpoint()->getAbsoluteUri()
87
        );
88
    }
89
90
    /**
91
     * @covers \OAuth\OAuth2\Service\Netatmo::__construct
92
     * @covers \OAuth\OAuth2\Service\Netatmo::parseAccessTokenResponse
93
     */
94
    public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse(): void
95
    {
96
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
97
        $client->expects(self::once())->method('retrieveResponse')->willReturn(null);
98
99
        $service = new Netatmo(
100
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
101
            $client,
102
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
103
        );
104
105
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
106
107
        $service->requestAccessToken('foo');
108
    }
109
110
    /**
111
     * @covers \OAuth\OAuth2\Service\Netatmo::__construct
112
     * @covers \OAuth\OAuth2\Service\Netatmo::parseAccessTokenResponse
113
     */
114
    public function testParseAccessTokenResponseThrowsExceptionOnError(): void
115
    {
116
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
117
        $client->expects(self::once())->method('retrieveResponse')->willReturn('error=some_error');
118
119
        $service = new Netatmo(
120
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
121
            $client,
122
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
123
        );
124
125
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
126
127
        $service->requestAccessToken('foo');
128
    }
129
130
    /**
131
     * @covers \OAuth\OAuth2\Service\Netatmo::__construct
132
     * @covers \OAuth\OAuth2\Service\Netatmo::parseAccessTokenResponse
133
     */
134
    public function testParseAccessTokenResponseValidWithoutRefreshToken(): void
135
    {
136
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
137
        $client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar"}');
138
139
        $service = new Netatmo(
140
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
141
            $client,
142
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
143
        );
144
145
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
146
    }
147
148
    /**
149
     * @covers \OAuth\OAuth2\Service\Netatmo::__construct
150
     * @covers \OAuth\OAuth2\Service\Netatmo::parseAccessTokenResponse
151
     */
152
    public function testParseAccessTokenResponseValidWithRefreshToken(): void
153
    {
154
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
155
        $client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}');
156
157
        $service = new Netatmo(
158
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
159
            $client,
160
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
161
        );
162
163
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
164
    }
165
}
166