1<?php
2
3namespace OAuthTest\Unit\OAuth1\Service;
4
5use OAuthTest\Mocks\OAuth1\Service\Mock;
6
7class AbstractServiceTest extends \PHPUnit_Framework_TestCase
8{
9    /**
10     * @covers OAuth\OAuth1\Service\AbstractService::__construct
11     */
12    public function testConstructCorrectInterface()
13    {
14        $service = $this->getMockForAbstractClass(
15            '\\OAuth\\OAuth1\\Service\\AbstractService',
16            array(
17                $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
18                $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
19                $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
20                $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
21                $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),
22            )
23        );
24
25        $this->assertInstanceOf('\\OAuth\\OAuth1\\Service\\ServiceInterface', $service);
26    }
27
28    /**
29     * @covers OAuth\OAuth1\Service\AbstractService::__construct
30     */
31    public function testConstructCorrectParent()
32    {
33        $service = $this->getMockForAbstractClass(
34            '\\OAuth\\OAuth1\\Service\\AbstractService',
35            array(
36                $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
37                $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
38                $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
39                $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
40                $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),
41            )
42        );
43
44        $this->assertInstanceOf('\\OAuth\\Common\\Service\\AbstractService', $service);
45    }
46
47    /**
48     * @covers OAuth\OAuth1\Service\AbstractService::requestRequestToken
49     * @covers OAuth\OAuth1\Service\AbstractService::buildAuthorizationHeaderForTokenRequest
50     * @covers OAuth\OAuth1\Service\AbstractService::getBasicAuthorizationHeaderInfo
51     * @covers OAuth\OAuth1\Service\AbstractService::generateNonce
52     * @covers OAuth\OAuth1\Service\AbstractService::getSignatureMethod
53     * @covers OAuth\OAuth1\Service\AbstractService::getVersion
54     * @covers OAuth\OAuth1\Service\AbstractService::getExtraOAuthHeaders
55     * @covers OAuth\OAuth1\Service\AbstractService::parseRequestTokenResponse
56     */
57    public function testRequestRequestTokenBuildAuthHeaderTokenRequestWithoutParams()
58    {
59        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
60        $client->expects($this->once())->method('retrieveResponse')->will($this->returnCallback(function($endpoint, $array, $headers) {
61            \PHPUnit_Framework_Assert::assertSame('http://pieterhordijk.com/token', $endpoint->getAbsoluteUri());
62        }));
63
64        $service = new Mock(
65            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
66            $client,
67            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
68            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
69            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
70        );
71
72        $this->assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestRequestToken());
73    }
74
75    /**
76     * @covers OAuth\OAuth1\Service\AbstractService::getAuthorizationUri
77     * @covers OAuth\OAuth1\Service\AbstractService::getAuthorizationEndpoint
78     */
79    public function testGetAuthorizationUriWithoutParameters()
80    {
81        $service = new Mock(
82            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
83            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
84            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
85            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
86            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
87        );
88
89        $this->assertSame('http://pieterhordijk.com/auth', $service->getAuthorizationUri()->getAbsoluteUri());
90    }
91
92    /**
93     * @covers OAuth\OAuth1\Service\AbstractService::getAuthorizationUri
94     * @covers OAuth\OAuth1\Service\AbstractService::getAuthorizationEndpoint
95     */
96    public function testGetAuthorizationUriWithParameters()
97    {
98        $service = new Mock(
99            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
100            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
101            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
102            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
103            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
104        );
105
106        $this->assertSame('http://pieterhordijk.com/auth?foo=bar&baz=beer', $service->getAuthorizationUri(array(
107            'foo' => 'bar',
108            'baz' => 'beer',
109        ))->getAbsoluteUri());
110    }
111
112    /**
113     * @covers OAuth\OAuth1\Service\AbstractService::requestAccessToken
114     * @covers OAuth\OAuth1\Service\AbstractService::service
115     * @covers OAuth\OAuth1\Service\AbstractService::buildAuthorizationHeaderForAPIRequest
116     * @covers OAuth\OAuth1\Service\AbstractService::getBasicAuthorizationHeaderInfo
117     * @covers OAuth\OAuth1\Service\AbstractService::generateNonce
118     * @covers OAuth\OAuth1\Service\AbstractService::getSignatureMethod
119     * @covers OAuth\OAuth1\Service\AbstractService::getVersion
120     * @covers OAuth\OAuth1\Service\AbstractService::getAccessTokenEndpoint
121     * @covers OAuth\OAuth1\Service\AbstractService::getExtraOAuthHeaders
122     * @covers OAuth\OAuth1\Service\AbstractService::parseAccessTokenResponse
123     */
124    public function testRequestAccessTokenWithoutSecret()
125    {
126        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
127        $client->expects($this->once())->method('retrieveResponse')->will($this->returnCallback(function($endpoint, $array, $headers) {
128            \PHPUnit_Framework_Assert::assertSame('http://pieterhordijk.com/access', $endpoint->getAbsoluteUri());
129        }));
130
131        $token = $this->getMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
132        $token->expects($this->once())->method('getRequestTokenSecret')->will($this->returnValue('baz'));
133
134        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
135        $storage->expects($this->any())->method('retrieveAccessToken')->will($this->returnValue($token));
136
137        $service = new Mock(
138            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
139            $client,
140            $storage,
141            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
142            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
143        );
144
145        $this->assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestAccessToken('foo', 'bar'));
146    }
147
148    /**
149     * @covers OAuth\OAuth1\Service\AbstractService::requestAccessToken
150     * @covers OAuth\OAuth1\Service\AbstractService::service
151     * @covers OAuth\OAuth1\Service\AbstractService::buildAuthorizationHeaderForAPIRequest
152     * @covers OAuth\OAuth1\Service\AbstractService::getBasicAuthorizationHeaderInfo
153     * @covers OAuth\OAuth1\Service\AbstractService::generateNonce
154     * @covers OAuth\OAuth1\Service\AbstractService::getSignatureMethod
155     * @covers OAuth\OAuth1\Service\AbstractService::getVersion
156     * @covers OAuth\OAuth1\Service\AbstractService::getAccessTokenEndpoint
157     * @covers OAuth\OAuth1\Service\AbstractService::getExtraOAuthHeaders
158     * @covers OAuth\OAuth1\Service\AbstractService::parseAccessTokenResponse
159     */
160    public function testRequestAccessTokenWithSecret()
161    {
162        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
163        $client->expects($this->once())->method('retrieveResponse')->will($this->returnCallback(function($endpoint, $array, $headers) {
164            \PHPUnit_Framework_Assert::assertSame('http://pieterhordijk.com/access', $endpoint->getAbsoluteUri());
165        }));
166
167        $token = $this->getMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
168
169        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
170        $storage->expects($this->any())->method('retrieveAccessToken')->will($this->returnValue($token));
171
172        $service = new Mock(
173            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
174            $client,
175            $storage,
176            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
177            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
178        );
179
180        $this->assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestAccessToken('foo', 'bar', $token));
181    }
182
183    /**
184     * @covers OAuth\OAuth1\Service\AbstractService::request
185     * @covers OAuth\OAuth1\Service\AbstractService::determineRequestUriFromPath
186     * @covers OAuth\OAuth1\Service\AbstractService::service
187     * @covers OAuth\OAuth1\Service\AbstractService::getExtraApiHeaders
188     * @covers OAuth\OAuth1\Service\AbstractService::buildAuthorizationHeaderForAPIRequest
189     * @covers OAuth\OAuth1\Service\AbstractService::getBasicAuthorizationHeaderInfo
190     * @covers OAuth\OAuth1\Service\AbstractService::generateNonce
191     * @covers OAuth\OAuth1\Service\AbstractService::getSignatureMethod
192     * @covers OAuth\OAuth1\Service\AbstractService::getVersion
193     */
194    public function testRequest()
195    {
196        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
197        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('response!'));
198
199        $token = $this->getMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
200        //$token->expects($this->once())->method('getRequestTokenSecret')->will($this->returnValue('baz'));
201
202        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
203        $storage->expects($this->any())->method('retrieveAccessToken')->will($this->returnValue($token));
204
205        $service = new Mock(
206            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
207            $client,
208            $storage,
209            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
210            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
211        );
212
213        $this->assertSame('response!', $service->request('/my/awesome/path'));
214    }
215}
216