1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuthTest\Unit\OAuth2\Service; |
4
|
|
|
|
5
|
|
|
use OAuth\OAuth2\Service\Xing; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class XingTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @covers \OAuth\OAuth2\Service\Xing::__construct |
12
|
|
|
*/ |
13
|
|
|
public function testConstructCorrectInterfaceWithoutCustomUri(): void |
14
|
|
|
{ |
15
|
|
|
$service = new Xing( |
16
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
17
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
|
|
|
|
18
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @covers \OAuth\OAuth2\Service\Xing::__construct |
26
|
|
|
*/ |
27
|
|
|
public function testConstructCorrectInstanceWithoutCustomUri(): void |
28
|
|
|
{ |
29
|
|
|
$service = new Xing( |
30
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
31
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
|
|
|
|
32
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @covers \OAuth\OAuth2\Service\Xing::__construct |
40
|
|
|
*/ |
41
|
|
|
public function testConstructCorrectInstanceWithCustomUri(): void |
42
|
|
|
{ |
43
|
|
|
$service = new Xing( |
44
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
45
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
|
|
|
|
46
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'), |
|
|
|
|
47
|
|
|
[], |
48
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface') |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @covers \OAuth\OAuth2\Service\Xing::__construct |
56
|
|
|
* @covers \OAuth\OAuth2\Service\Xing::getAuthorizationEndpoint |
57
|
|
|
*/ |
58
|
|
|
public function testGetAuthorizationEndpoint(): void |
59
|
|
|
{ |
60
|
|
|
$service = new Xing( |
61
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
62
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
|
|
|
|
63
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
self::assertSame('https://api.xing.com/auth/oauth2/authorize', $service->getAuthorizationEndpoint()->getAbsoluteUri()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @covers \OAuth\OAuth2\Service\Xing::__construct |
71
|
|
|
* @covers \OAuth\OAuth2\Service\Xing::getAccessTokenEndpoint |
72
|
|
|
*/ |
73
|
|
|
public function testGetAccessTokenEndpoint(): void |
74
|
|
|
{ |
75
|
|
|
$service = new Xing( |
76
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
77
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
|
|
|
|
78
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
self::assertSame('https://api.xing.com/auth/oauth2/token', $service->getAccessTokenEndpoint()->getAbsoluteUri()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @covers \OAuth\OAuth2\Service\Xing::__construct |
86
|
|
|
* @covers \OAuth\OAuth2\Service\Xing::parseAccessTokenResponse |
87
|
|
|
*/ |
88
|
|
|
public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse(): void |
89
|
|
|
{ |
90
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
91
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn(null); |
92
|
|
|
|
93
|
|
|
$service = new Xing( |
94
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
95
|
|
|
$client, |
|
|
|
|
96
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
$this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); |
100
|
|
|
|
101
|
|
|
$service->requestAccessToken('foo'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @covers \OAuth\OAuth2\Service\Xing::__construct |
106
|
|
|
* @covers \OAuth\OAuth2\Service\Xing::parseAccessTokenResponse |
107
|
|
|
*/ |
108
|
|
|
public function testParseAccessTokenResponseThrowsExceptionOnError(): void |
109
|
|
|
{ |
110
|
|
|
$error = [ |
111
|
|
|
'error' => 'some_error', |
112
|
|
|
'error_description' => 'something went very wrong', |
113
|
|
|
'error_uri' => 'this imaginary link contains more information', |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
117
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn(json_encode($error)); |
118
|
|
|
|
119
|
|
|
$service = new Xing( |
120
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
121
|
|
|
$client, |
|
|
|
|
122
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); |
126
|
|
|
|
127
|
|
|
$service->requestAccessToken('foo'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @covers \OAuth\OAuth2\Service\Xing::__construct |
132
|
|
|
* @covers \OAuth\OAuth2\Service\Xing::parseAccessTokenResponse |
133
|
|
|
*/ |
134
|
|
|
public function testParseAccessTokenResponseValidWithRefreshToken(): void |
135
|
|
|
{ |
136
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
137
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}'); |
138
|
|
|
|
139
|
|
|
$service = new Xing( |
140
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
141
|
|
|
$client, |
|
|
|
|
142
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: