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