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