Completed
Pull Request — master (#479)
by Andrey
03:22
created

QuickBooksTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
lcom 1
cbo 3
dl 0
loc 203
rs 10
c 2
b 0
f 0

16 Methods

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