Completed
Pull Request — master (#551)
by
unknown
01:58
created

MyobTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 183
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructCorrectInterfaceWithoutCustomUri() 0 10 1
A testConstructCorrectInstanceWithoutCustomUri() 0 10 1
A testConstructCorrectInstanceWithCustomUri() 0 12 1
A testGetAuthorizationEndpoint() 0 11 1
A testGetAccessTokenEndpoint() 0 11 1
A testGetAuthorizationMethod() 0 22 1
A testParseAccessTokenResponseThrowsExceptionOnNulledResponse() 0 15 1
A testParseAccessTokenResponseThrowsExceptionOnErrorDescription() 0 15 1
A testParseAccessTokenResponseThrowsExceptionOnError() 0 15 1
A testParseAccessTokenResponseValidWithoutRefreshToken() 0 13 1
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use OAuth\Common\Token\TokenInterface;
6
use OAuth\OAuth2\Service\Myob;
7
8
class MyobTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @covers OAuth\OAuth2\Service\Myob::__construct
12
     */
13
    public function testConstructCorrectInterfaceWithoutCustomUri()
14
    {
15
        $service = new Myob(
16
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
17
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
18
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
19
        );
20
21
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service);
22
    }
23
24
    /**
25
     * @covers OAuth\OAuth2\Service\Myob::__construct
26
     */
27
    public function testConstructCorrectInstanceWithoutCustomUri()
28
    {
29
        $service = new Myob(
30
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
31
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
32
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
33
        );
34
35
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
36
    }
37
38
    /**
39
     * @covers OAuth\OAuth2\Service\Myob::__construct
40
     */
41
    public function testConstructCorrectInstanceWithCustomUri()
42
    {
43
        $service = new Myob(
44
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
45
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
46
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
47
            array(),
48
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
49
        );
50
51
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
52
    }
53
54
    /**
55
     * @covers OAuth\OAuth2\Service\Myob::__construct
56
     * @covers OAuth\OAuth2\Service\Myob::getAuthorizationEndpoint
57
     */
58
    public function testGetAuthorizationEndpoint()
59
    {
60
        $service = new Myob(
61
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
62
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
63
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
64
        );
65
66
        $this->assertSame('https://secure.myob.com/oauth2/account/authorize',
67
            $service->getAuthorizationEndpoint()->getAbsoluteUri());
68
    }
69
70
    /**
71
     * @covers OAuth\OAuth2\Service\Myob::__construct
72
     * @covers OAuth\OAuth2\Service\Myob::getAccessTokenEndpoint
73
     */
74
    public function testGetAccessTokenEndpoint()
75
    {
76
        $service = new Myob(
77
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
78
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
79
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
80
        );
81
82
        $this->assertSame('https://secure.myob.com/oauth2/v1/authorize',
83
            $service->getAccessTokenEndpoint()->getAbsoluteUri());
84
    }
85
86
    /**
87
     * @covers OAuth\OAuth2\Service\Myob::__construct
88
     * @covers OAuth\OAuth2\Service\Myob::getAuthorizationMethod
89
     */
90
    public function testGetAuthorizationMethod()
91
    {
92
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
93
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(2));
94
95
        $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
96
        $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
97
        $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
98
99
        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
100
        $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token));
101
102
        $service = new Myob(
103
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
104
            $client,
105
            $storage
106
        );
107
108
        $headers = $service->request('https://pieterhordijk.com/my/awesome/path');
109
110
        $this->assertTrue(array_key_exists('Authorization', $headers));
111
    }
112
113
    /**
114
     * @covers OAuth\OAuth2\Service\Myob::__construct
115
     * @covers OAuth\OAuth2\Service\Myob::parseAccessTokenResponse
116
     */
117
    public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse()
118
    {
119
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
120
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null));
121
122
        $service = new Myob(
123
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
124
            $client,
125
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
126
        );
127
128
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
129
130
        $service->requestAccessToken('foo');
131
    }
132
133
    /**
134
     * @covers OAuth\OAuth2\Service\Myob::__construct
135
     * @covers OAuth\OAuth2\Service\Myob::parseAccessTokenResponse
136
     */
137
    public function testParseAccessTokenResponseThrowsExceptionOnErrorDescription()
138
    {
139
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
140
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error_description=some_error'));
141
142
        $service = new Myob(
143
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
144
            $client,
145
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
146
        );
147
148
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
149
150
        $service->requestAccessToken('foo');
151
    }
152
153
    /**
154
     * @covers OAuth\OAuth2\Service\Myob::__construct
155
     * @covers OAuth\OAuth2\Service\Myob::parseAccessTokenResponse
156
     */
157
    public function testParseAccessTokenResponseThrowsExceptionOnError()
158
    {
159
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
160
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=some_error'));
161
162
        $service = new Myob(
163
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
164
            $client,
165
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
166
        );
167
168
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
169
170
        $service->requestAccessToken('foo');
171
    }
172
173
    /**
174
     * @covers OAuth\OAuth2\Service\Myob::__construct
175
     * @covers OAuth\OAuth2\Service\Myob::parseAccessTokenResponse
176
     */
177
    public function testParseAccessTokenResponseValidWithoutRefreshToken()
178
    {
179
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
180
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar"}'));
181
182
        $service = new Myob(
183
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
184
            $client,
185
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
186
        );
187
188
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
189
    }
190
}
191