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