1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Boozt Platform |
5
|
|
|
* and belongs to Boozt Fashion AB. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
namespace OAuthTest\Unit\OAuth2\Service; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
use OAuth\OAuth2\Service\Todoist; |
17
|
|
|
use OAuth\OAuth2\Token\TokenInterface; |
18
|
|
|
|
19
|
|
|
class TodoistTest extends \PHPUnit_Framework_TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::__construct |
23
|
|
|
*/ |
24
|
|
|
public function testConstructCorrectInterfaceWithoutCustomUri() |
25
|
|
|
{ |
26
|
|
|
$service = new Todoist( |
27
|
|
|
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
28
|
|
|
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
29
|
|
|
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
$this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::__construct |
37
|
|
|
*/ |
38
|
|
|
public function testConstructCorrectInstanceWithoutCustomUri() |
39
|
|
|
{ |
40
|
|
|
$service = new Todoist( |
41
|
|
|
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
42
|
|
|
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
43
|
|
|
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
$this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::__construct |
51
|
|
|
*/ |
52
|
|
|
public function testConstructCorrectInstanceWithCustomUri() |
53
|
|
|
{ |
54
|
|
|
$service = new Todoist( |
55
|
|
|
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
56
|
|
|
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
57
|
|
|
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'), |
58
|
|
|
array(), |
59
|
|
|
$this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface') |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::__construct |
67
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::getAuthorizationEndpoint |
68
|
|
|
*/ |
69
|
|
|
public function testGetAuthorizationEndpoint() |
70
|
|
|
{ |
71
|
|
|
$service = new Todoist( |
72
|
|
|
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
73
|
|
|
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
74
|
|
|
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$this->assertSame('https://todoist.com/oauth/authorize', $service->getAuthorizationEndpoint()->getAbsoluteUri()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::__construct |
82
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::getAccessTokenEndpoint |
83
|
|
|
*/ |
84
|
|
|
public function testGetAccessTokenEndpoint() |
85
|
|
|
{ |
86
|
|
|
$service = new Todoist( |
87
|
|
|
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
88
|
|
|
$this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
89
|
|
|
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$this->assertSame('https://todoist.com/oauth/access_token', $service->getAccessTokenEndpoint()->getAbsoluteUri()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::__construct |
97
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::getAuthorizationMethod |
98
|
|
|
*/ |
99
|
|
|
public function testGetAuthorizationMethod() |
100
|
|
|
{ |
101
|
|
|
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
102
|
|
|
$client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(0)); |
103
|
|
|
|
104
|
|
|
$token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); |
105
|
|
|
$token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES)); |
106
|
|
|
$token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo')); |
107
|
|
|
|
108
|
|
|
$storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); |
109
|
|
|
$storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token)); |
110
|
|
|
|
111
|
|
|
$service = new Todoist( |
112
|
|
|
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
113
|
|
|
$client, |
114
|
|
|
$storage |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$uri = $service->request('https://pieterhordijk.com/my/awesome/path'); |
118
|
|
|
$absoluteUri = parse_url($uri->getAbsoluteUri()); |
|
|
|
|
119
|
|
|
|
120
|
|
|
$this->assertSame('token=foo', $absoluteUri['query']); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::__construct |
125
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::parseAccessTokenResponse |
126
|
|
|
*/ |
127
|
|
|
public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse() |
128
|
|
|
{ |
129
|
|
|
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
130
|
|
|
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null)); |
131
|
|
|
|
132
|
|
|
$service = new Todoist( |
133
|
|
|
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
134
|
|
|
$client, |
135
|
|
|
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); |
139
|
|
|
|
140
|
|
|
$service->requestAccessToken('foo'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::__construct |
145
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::parseAccessTokenResponse |
146
|
|
|
*/ |
147
|
|
|
public function testParseAccessTokenResponseThrowsExceptionOnError() |
148
|
|
|
{ |
149
|
|
|
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
150
|
|
|
$client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"error":"some_error"}')); |
151
|
|
|
|
152
|
|
|
$service = new Todoist( |
153
|
|
|
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
154
|
|
|
$client, |
155
|
|
|
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); |
159
|
|
|
|
160
|
|
|
$service->requestAccessToken('foo'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::__construct |
165
|
|
|
* @covers OAuth\OAuth2\Service\Todoist::parseAccessTokenResponse |
166
|
|
|
*/ |
167
|
|
|
public function testParseAccessTokenResponseValidWithoutRefreshToken() |
168
|
|
|
{ |
169
|
|
|
$client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
170
|
|
|
$client->expects($this->once()) |
171
|
|
|
->method('retrieveResponse') |
172
|
|
|
->will($this->returnValue('{"access_token":"foo","expires_in":"bar"}')); |
173
|
|
|
|
174
|
|
|
$service = new Todoist( |
175
|
|
|
$this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
176
|
|
|
$client, |
177
|
|
|
$this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
$this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.