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\Stripe;
7
use PHPUnit\Framework\TestCase;
8
9
class StripeTest extends TestCase
10
{
11
    /**
12
     * @covers \OAuth\OAuth2\Service\Stripe::__construct
13
     */
14
    public function testConstructCorrectInterfaceWithoutCustomUri(): void
15
    {
16
        $service = new Stripe(
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\Stripe::__construct
27
     */
28
    public function testConstructCorrectInstanceWithoutCustomUri(): void
29
    {
30
        $service = new Stripe(
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\Stripe::__construct
41
     */
42
    public function testConstructCorrectInstanceWithCustomUri(): void
43
    {
44
        $service = new Stripe(
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\Stripe::__construct
57
     * @covers \OAuth\OAuth2\Service\Stripe::getAuthorizationEndpoint
58
     */
59
    public function testGetAuthorizationEndpoint(): void
60
    {
61
        $service = new Stripe(
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://connect.stripe.com/oauth/authorize',
69
            $service->getAuthorizationEndpoint()->getAbsoluteUri()
70
        );
71
    }
72
73
    /**
74
     * @covers \OAuth\OAuth2\Service\Stripe::__construct
75
     * @covers \OAuth\OAuth2\Service\Stripe::getAccessTokenEndpoint
76
     */
77
    public function testGetAccessTokenEndpoint(): void
78
    {
79
        $service = new Stripe(
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://connect.stripe.com/oauth/token',
87
            $service->getAccessTokenEndpoint()->getAbsoluteUri()
88
        );
89
    }
90
91
    /**
92
     * @covers \OAuth\OAuth2\Service\Stripe::__construct
93
     * @covers \OAuth\OAuth2\Service\Stripe::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 Stripe(
108
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
109
            $client,
110
            $storage
111
        );
112
113
        $uri = $service->request('https://luisrpalanca.com/my/awesome/path');
114
        $absoluteUri = parse_url($uri->getAbsoluteUri());
115
116
        self::assertSame('access_token=foo', $absoluteUri['query']);
117
    }
118
119
    /**
120
     * @covers \OAuth\OAuth2\Service\Stripe::__construct
121
     * @covers \OAuth\OAuth2\Service\Stripe::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 Stripe(
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\Stripe::__construct
141
     * @covers \OAuth\OAuth2\Service\Stripe::parseAccessTokenResponse
142
     */
143
    public function testParseAccessTokenResponseThrowsExceptionOnError(): void
144
    {
145
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
146
        $client->expects(self::once())
147
            ->method('retrieveResponse')->willReturn('error=some_error');
148
149
        $service = new Stripe(
150
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
151
            $client,
152
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
153
        );
154
155
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
156
157
        $service->requestAccessToken('foo');
158
    }
159
160
    /**
161
     * @covers \OAuth\OAuth2\Service\Stripe::__construct
162
     * @covers \OAuth\OAuth2\Service\Stripe::parseAccessTokenResponse
163
     */
164
    public function testParseAccessTokenResponseValidWithoutRefreshToken(): void
165
    {
166
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
167
        $client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar"}');
168
169
        $service = new Stripe(
170
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
171
            $client,
172
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
173
        );
174
175
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
176
    }
177
178
    /**
179
     * @covers \OAuth\OAuth2\Service\Stripe::__construct
180
     * @covers \OAuth\OAuth2\Service\Stripe::parseAccessTokenResponse
181
     */
182
    public function testParseAccessTokenResponseValidWithRefreshToken(): 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","refresh_token":"baz"}');
186
187
        $service = new Stripe(
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