testConstructCorrectInstanceWithCustomUri()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 11
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\Common\Token\TokenInterface;
6
use OAuth\OAuth2\Service\Reddit;
7
use PHPUnit\Framework\TestCase;
8
9
class RedditTest extends TestCase
10
{
11
    /**
12
     * @covers \OAuth\OAuth2\Service\Reddit::__construct
13
     */
14
    public function testConstructCorrectInterfaceWithoutCustomUri(): void
15
    {
16
        $service = new Reddit(
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\Reddit::__construct
27
     */
28
    public function testConstructCorrectInstanceWithoutCustomUri(): void
29
    {
30
        $service = new Reddit(
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\Reddit::__construct
41
     */
42
    public function testConstructCorrectInstanceWithCustomUri(): void
43
    {
44
        $service = new Reddit(
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\Reddit::__construct
57
     * @covers \OAuth\OAuth2\Service\Reddit::getAuthorizationEndpoint
58
     */
59
    public function testGetAuthorizationEndpoint(): void
60
    {
61
        $service = new Reddit(
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://ssl.reddit.com/api/v1/authorize',
69
            $service->getAuthorizationEndpoint()->getAbsoluteUri()
70
        );
71
    }
72
73
    /**
74
     * @covers \OAuth\OAuth2\Service\Reddit::__construct
75
     * @covers \OAuth\OAuth2\Service\Reddit::getAccessTokenEndpoint
76
     */
77
    public function testGetAccessTokenEndpoint(): void
78
    {
79
        $service = new Reddit(
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://ssl.reddit.com/api/v1/access_token',
87
            $service->getAccessTokenEndpoint()->getAbsoluteUri()
88
        );
89
    }
90
91
    /**
92
     * @covers \OAuth\OAuth2\Service\Reddit::__construct
93
     * @covers \OAuth\OAuth2\Service\Reddit::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(2);
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 Reddit(
108
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
109
            $client,
110
            $storage
111
        );
112
113
        $headers = $service->request('https://pieterhordijk.com/my/awesome/path');
114
115
        self::assertArrayHasKey('Authorization', $headers);
0 ignored issues
show
Bug introduced by
$headers of type string is incompatible with the type ArrayAccess|array expected by parameter $array of PHPUnit\Framework\Assert::assertArrayHasKey(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

115
        self::assertArrayHasKey('Authorization', /** @scrutinizer ignore-type */ $headers);
Loading history...
116
        self::assertTrue(in_array('Bearer foo', $headers, true));
0 ignored issues
show
Bug introduced by
$headers of type string is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

116
        self::assertTrue(in_array('Bearer foo', /** @scrutinizer ignore-type */ $headers, true));
Loading history...
117
    }
118
119
    /**
120
     * @covers \OAuth\OAuth2\Service\Reddit::__construct
121
     * @covers \OAuth\OAuth2\Service\Reddit::parseAccessTokenResponse
122
     */
123
    public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse(): void
124
    {
125
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
126
        $client->expects(self::once())->method('retrieveResponse')->willReturn(null);
127
128
        $service = new Reddit(
129
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
130
            $client,
131
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
132
        );
133
134
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
135
136
        $service->requestAccessToken('foo');
137
    }
138
139
    /**
140
     * @covers \OAuth\OAuth2\Service\Reddit::__construct
141
     * @covers \OAuth\OAuth2\Service\Reddit::parseAccessTokenResponse
142
     */
143
    public function testParseAccessTokenResponseThrowsExceptionOnError(): void
144
    {
145
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
146
        $client->expects(self::once())->method('retrieveResponse')->willReturn('error=some_error');
147
148
        $service = new Reddit(
149
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
150
            $client,
151
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
152
        );
153
154
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
155
156
        $service->requestAccessToken('foo');
157
    }
158
159
    /**
160
     * @covers \OAuth\OAuth2\Service\Reddit::__construct
161
     * @covers \OAuth\OAuth2\Service\Reddit::parseAccessTokenResponse
162
     */
163
    public function testParseAccessTokenResponseValidWithoutRefreshToken(): void
164
    {
165
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
166
        $client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar"}');
167
168
        $service = new Reddit(
169
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
170
            $client,
171
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
172
        );
173
174
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
175
    }
176
177
    /**
178
     * @covers \OAuth\OAuth2\Service\Reddit::__construct
179
     * @covers \OAuth\OAuth2\Service\Reddit::parseAccessTokenResponse
180
     */
181
    public function testParseAccessTokenResponseValidWithRefreshToken(): void
182
    {
183
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
184
        $client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}');
185
186
        $service = new Reddit(
187
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
188
            $client,
189
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
190
        );
191
192
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
193
    }
194
}
195