QuickBooksTest   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
eloc 95
dl 0
loc 194
c 2
b 0
f 0
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A getQuickBooksForRequestingAccessToken() 0 13 1
A testGetAuthorizationEndpoint() 0 6 1
A testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotSet() 0 8 1
A testConstructCorrectInterfaceWithoutCustomUri() 0 6 1
A testParseRequestTokenResponseThrowsExceptionOnResponseNotAnArray() 0 8 1
A testConstructCorrectInstanceWithCustomUri() 0 13 1
A testParseRequestTokenResponseThrowsExceptionOnNulledResponse() 0 8 1
A testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotTrue() 0 10 1
A testParseAccessTokenResponseValid() 0 11 1
A testParseRequestTokenResponseValid() 0 9 1
A testConstructCorrectInstanceWithoutCustomUri() 0 6 1
A getClientInterfaceMockThatReturns() 0 10 1
A testGetAccessTokenEndpoint() 0 6 1
A testGetRequestTokenEndpoint() 0 6 1
A testParseAccessTokenResponseThrowsExceptionOnError() 0 12 1
A getQuickBooks() 0 21 3
1
<?php
2
3
namespace OAuthTest\Unit\OAuth1\Service;
4
5
use OAuth\Common\Http\Client\ClientInterface;
6
use OAuth\Common\Storage\TokenStorageInterface;
7
use OAuth\Common\Token\TokenInterface;
8
use OAuth\OAuth1\Service\QuickBooks;
9
use PHPUnit\Framework\TestCase;
10
11
class QuickBooksTest extends TestCase
12
{
13
    public function testConstructCorrectInterfaceWithoutCustomUri(): void
14
    {
15
        $service = $this->getQuickBooks();
16
        self::assertInstanceOf(
17
            '\\OAuth\\OAuth1\\Service\\ServiceInterface',
18
            $service
19
        );
20
    }
21
22
    public function testConstructCorrectInstanceWithoutCustomUri(): void
23
    {
24
        $service = $this->getQuickBooks();
25
        self::assertInstanceOf(
26
            '\\OAuth\\OAuth1\\Service\\AbstractService',
27
            $service
28
        );
29
    }
30
31
    public function testConstructCorrectInstanceWithCustomUri(): void
32
    {
33
        $service = new QuickBooks(
34
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
35
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
36
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
37
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
38
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
39
        );
40
41
        self::assertInstanceOf(
42
            '\\OAuth\\OAuth1\\Service\\AbstractService',
43
            $service
44
        );
45
    }
46
47
    public function testGetRequestTokenEndpoint(): void
48
    {
49
        $service = $this->getQuickBooks();
50
        self::assertSame(
51
            'https://oauth.intuit.com/oauth/v1/get_request_token',
52
            $service->getRequestTokenEndpoint()->getAbsoluteUri()
53
        );
54
    }
55
56
    public function testGetAuthorizationEndpoint(): void
57
    {
58
        $service = $this->getQuickBooks();
59
        self::assertSame(
60
            'https://appcenter.intuit.com/Connect/Begin',
61
            $service->getAuthorizationEndpoint()->getAbsoluteUri()
62
        );
63
    }
64
65
    public function testGetAccessTokenEndpoint(): void
66
    {
67
        $service = $this->getQuickBooks();
68
        self::assertSame(
69
            'https://oauth.intuit.com/oauth/v1/get_access_token',
70
            $service->getAccessTokenEndpoint()->getAbsoluteUri()
71
        );
72
    }
73
74
    public function testParseRequestTokenResponseThrowsExceptionOnNulledResponse(): void
75
    {
76
        $this->expectException(\OAuth\Common\Http\Exception\TokenResponseException::class);
77
        $this->expectExceptionMessage('Error in retrieving token.');
78
79
        $client = $this->getClientInterfaceMockThatReturns(null);
80
        $service = $this->getQuickBooks($client);
81
        $service->requestRequestToken();
82
    }
83
84
    public function testParseRequestTokenResponseThrowsExceptionOnResponseNotAnArray(): void
85
    {
86
        $this->expectException(\OAuth\Common\Http\Exception\TokenResponseException::class);
87
        $this->expectExceptionMessage('Error in retrieving token.');
88
89
        $client = $this->getClientInterfaceMockThatReturns('notanarray');
90
        $service = $this->getQuickBooks($client);
91
        $service->requestRequestToken();
92
    }
93
94
    public function testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotSet(): void
95
    {
96
        $this->expectException(\OAuth\Common\Http\Exception\TokenResponseException::class);
97
        $this->expectExceptionMessage('Error in retrieving token.');
98
99
        $client = $this->getClientInterfaceMockThatReturns('foo=bar');
100
        $service = $this->getQuickBooks($client);
101
        $service->requestRequestToken();
102
    }
103
104
    public function testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotTrue(): void
105
    {
106
        $this->expectException(\OAuth\Common\Http\Exception\TokenResponseException::class);
107
        $this->expectExceptionMessage('Error in retrieving token.');
108
109
        $client = $this->getClientInterfaceMockThatReturns(
110
            'oauth_callback_confirmed=false'
111
        );
112
        $service = $this->getQuickBooks($client);
113
        $service->requestRequestToken();
114
    }
115
116
    public function testParseRequestTokenResponseValid(): void
117
    {
118
        $client = $this->getClientInterfaceMockThatReturns(
119
            'oauth_callback_confirmed=true&oauth_token=foo&oauth_token_secret=bar'
120
        );
121
        $service = $this->getQuickBooks($client);
122
        self::assertInstanceOf(
123
            '\\OAuth\\OAuth1\\Token\\StdOAuth1Token',
124
            $service->requestRequestToken()
125
        );
126
    }
127
128
    public function testParseAccessTokenResponseThrowsExceptionOnError(): void
129
    {
130
        $this->expectException(\OAuth\Common\Http\Exception\TokenResponseException::class);
131
        $this->expectExceptionMessage('Error in retrieving token: "bar"');
132
133
        $token = $this->createMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
134
        $service = $this->getQuickBooksForRequestingAccessToken(
135
            $token,
136
            'error=bar'
137
        );
138
139
        $service->requestAccessToken('foo', 'bar', $token);
140
    }
141
142
    public function testParseAccessTokenResponseValid(): void
143
    {
144
        $token = $this->createMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
145
        $service = $this->getQuickBooksForRequestingAccessToken(
146
            $token,
147
            'oauth_token=foo&oauth_token_secret=bar'
148
        );
149
150
        self::assertInstanceOf(
151
            '\\OAuth\\OAuth1\\Token\\StdOAuth1Token',
152
            $service->requestAccessToken('foo', 'bar', $token)
153
        );
154
    }
155
156
    protected function getQuickBooks(
157
        ?ClientInterface $client = null,
158
        ?TokenStorageInterface $storage = null
159
    ) {
160
        if (!$client) {
161
            $client = $this->createMock(
162
                '\\OAuth\\Common\\Http\\Client\\ClientInterface'
163
            );
164
        }
165
166
        if (!$storage) {
167
            $storage = $this->createMock(
168
                '\\OAuth\\Common\\Storage\\TokenStorageInterface'
169
            );
170
        }
171
172
        return new QuickBooks(
173
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
174
            $client,
175
            $storage,
176
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
177
        );
178
    }
179
180
    protected function getQuickBooksForRequestingAccessToken(
181
        TokenInterface $token,
182
        $response
183
    ) {
184
        $client = $this->getClientInterfaceMockThatReturns($response);
185
        $storage = $this->createMock(
186
            '\\OAuth\\Common\\Storage\\TokenStorageInterface'
187
        );
188
        $storage->expects(self::any())
189
            ->method('retrieveAccessToken')
190
            ->willReturn($token);
191
192
        return $this->getQuickBooks($client, $storage);
193
    }
194
195
    protected function getClientInterfaceMockThatReturns($returnValue)
196
    {
197
        $client = $this->createMock(
198
            '\\OAuth\\Common\\Http\\Client\\ClientInterface'
199
        );
200
        $client->expects(self::once())
201
            ->method('retrieveResponse')
202
            ->willReturn($returnValue);
203
204
        return $client;
205
    }
206
}
207