Completed
Pull Request — master (#467)
by
unknown
04:30
created

TodoistTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 164
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructCorrectInterfaceWithoutCustomUri() 0 10 1
A testConstructCorrectInstanceWithoutCustomUri() 0 10 1
A testConstructCorrectInstanceWithCustomUri() 0 12 1
A testGetAuthorizationEndpoint() 0 10 1
A testGetAccessTokenEndpoint() 0 10 1
A testGetAuthorizationMethod() 0 23 1
A testParseAccessTokenResponseThrowsExceptionOnNulledResponse() 0 15 1
A testParseAccessTokenResponseThrowsExceptionOnError() 0 15 1
A testParseAccessTokenResponseValidWithoutRefreshToken() 0 15 1
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
6
use OAuth\OAuth2\Service\Todoist;
7
use OAuth\OAuth2\Token\TokenInterface;
8
9
class TodoistTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @covers OAuth\OAuth2\Service\Todoist::__construct
13
     */
14
    public function testConstructCorrectInterfaceWithoutCustomUri()
15
    {
16
        $service = new Todoist(
17
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
18
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
19
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
20
        );
21
22
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service);
23
    }
24
25
    /**
26
     * @covers OAuth\OAuth2\Service\Todoist::__construct
27
     */
28
    public function testConstructCorrectInstanceWithoutCustomUri()
29
    {
30
        $service = new Todoist(
31
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
32
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
33
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
34
        );
35
36
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
37
    }
38
39
    /**
40
     * @covers OAuth\OAuth2\Service\Todoist::__construct
41
     */
42
    public function testConstructCorrectInstanceWithCustomUri()
43
    {
44
        $service = new Todoist(
45
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
46
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
47
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
48
            array(),
49
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
50
        );
51
52
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
53
    }
54
55
    /**
56
     * @covers OAuth\OAuth2\Service\Todoist::__construct
57
     * @covers OAuth\OAuth2\Service\Todoist::getAuthorizationEndpoint
58
     */
59
    public function testGetAuthorizationEndpoint()
60
    {
61
        $service = new Todoist(
62
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
63
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
64
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
65
        );
66
67
        $this->assertSame('https://todoist.com/oauth/authorize', $service->getAuthorizationEndpoint()->getAbsoluteUri());
68
    }
69
70
    /**
71
     * @covers OAuth\OAuth2\Service\Todoist::__construct
72
     * @covers OAuth\OAuth2\Service\Todoist::getAccessTokenEndpoint
73
     */
74
    public function testGetAccessTokenEndpoint()
75
    {
76
        $service = new Todoist(
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://todoist.com/oauth/access_token', $service->getAccessTokenEndpoint()->getAbsoluteUri());
83
    }
84
85
    /**
86
     * @covers OAuth\OAuth2\Service\Todoist::__construct
87
     * @covers OAuth\OAuth2\Service\Todoist::getAuthorizationMethod
88
     */
89
    public function testGetAuthorizationMethod()
90
    {
91
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
92
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(0));
93
94
        $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
95
        $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
96
        $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
97
98
        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
99
        $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token));
100
101
        $service = new Todoist(
102
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
103
            $client,
104
            $storage
105
        );
106
107
        $uri         = $service->request('https://pieterhordijk.com/my/awesome/path');
108
        $absoluteUri = parse_url($uri->getAbsoluteUri());
0 ignored issues
show
Bug introduced by
The method getAbsoluteUri cannot be called on $uri (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
109
110
        $this->assertSame('token=foo', $absoluteUri['query']);
111
    }
112
113
    /**
114
     * @covers OAuth\OAuth2\Service\Todoist::__construct
115
     * @covers OAuth\OAuth2\Service\Todoist::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 Todoist(
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\Todoist::__construct
135
     * @covers OAuth\OAuth2\Service\Todoist::parseAccessTokenResponse
136
     */
137
    public function testParseAccessTokenResponseThrowsExceptionOnError()
138
    {
139
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
140
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"error":"some_error"}'));
141
142
        $service = new Todoist(
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\Todoist::__construct
155
     * @covers OAuth\OAuth2\Service\Todoist::parseAccessTokenResponse
156
     */
157
    public function testParseAccessTokenResponseValidWithoutRefreshToken()
158
    {
159
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
160
        $client->expects($this->once())
161
               ->method('retrieveResponse')
162
               ->will($this->returnValue('{"access_token":"foo","expires_in":"bar"}'));
163
164
        $service = new Todoist(
165
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
166
            $client,
167
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
168
        );
169
170
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
171
    }
172
}
173