1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SocialConnect project |
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Test\Providers; |
8
|
|
|
|
9
|
|
|
use SocialConnect\Auth\Provider\Exception\InvalidAccessToken; |
10
|
|
|
use SocialConnect\Auth\Consumer; |
11
|
|
|
use SocialConnect\OAuth2\AccessToken; |
12
|
|
|
use SocialConnect\Common\Http\Client\ClientInterface; |
13
|
|
|
use Test\TestCase; |
14
|
|
|
|
15
|
|
|
class VkTest extends AbstractProviderTestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param ClientInterface|null $httpClient |
19
|
|
|
* @return \SocialConnect\Auth\Provider\Vk |
20
|
|
|
*/ |
21
|
|
|
protected function getProvider(ClientInterface $httpClient = null) |
22
|
|
|
{ |
23
|
|
|
$service = new \SocialConnect\Auth\Service( |
24
|
|
|
[ |
25
|
|
|
'redirectUri' => 'http://localhost:8000/' |
26
|
|
|
] |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
if ($httpClient) { |
30
|
|
|
$service->setHttpClient($httpClient); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return new \SocialConnect\Auth\Provider\Vk( |
34
|
|
|
$service, |
35
|
|
|
new Consumer( |
36
|
|
|
'unknown', |
37
|
|
|
'unkwown' |
38
|
|
|
) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testMakeAuthUrl() |
43
|
|
|
{ |
44
|
|
|
parent::assertSame( |
45
|
|
|
'https://oauth.vk.com/authorize?client_id=unknown&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2F%2Fvk%2F&response_type=code', |
46
|
|
|
$this->getProvider()->makeAuthUrl() |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @expectedException \InvalidArgumentException |
52
|
|
|
* @expectedExceptionMessage Parameter $code must be a string |
53
|
|
|
*/ |
54
|
|
|
public function testGetAccessTokenFail() |
55
|
|
|
{ |
56
|
|
|
$this->getProvider()->getAccessToken(null); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
public function testMakeAccessTokenRequest() |
61
|
|
|
{ |
62
|
|
|
$expectedCode = 'djsflkSdjflskdfjFlsd9'; |
63
|
|
|
|
64
|
|
|
$provider = $this->getProvider(); |
65
|
|
|
|
66
|
|
|
/** @var \SocialConnect\Common\Http\Request $request */ |
67
|
|
|
$request = parent::callProtectedMethod( |
68
|
|
|
$provider, |
69
|
|
|
'makeAccessTokenRequest', |
70
|
|
|
[ |
71
|
|
|
$expectedCode |
72
|
|
|
] |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
parent::assertInstanceOf(\SocialConnect\Common\Http\Request::class, $request); |
76
|
|
|
parent::assertSame($provider->getRequestTokenUri(), $request->getUri()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function testParseTokenSuccess() |
80
|
|
|
{ |
81
|
|
|
$expectedToken = 'XXXXXXXX'; |
82
|
|
|
$expectedUserId = 123456; |
83
|
|
|
|
84
|
|
|
$accessToken = $this->getProvider()->parseToken( |
85
|
|
|
json_encode( |
86
|
|
|
[ |
87
|
|
|
'access_token' => $expectedToken, |
88
|
|
|
'user_id' => $expectedUserId |
89
|
|
|
] |
90
|
|
|
) |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
parent::assertInstanceOf(AccessToken::class, $accessToken); |
94
|
|
|
parent::assertSame($expectedToken, $accessToken->getToken()); |
95
|
|
|
parent::assertSame($expectedUserId, $accessToken->getUserId()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function testParseTokenNotToken() |
99
|
|
|
{ |
100
|
|
|
$this->setExpectedException(InvalidAccessToken::class); |
101
|
|
|
|
102
|
|
|
$accessToken = $this->getProvider()->parseToken( |
103
|
|
|
json_encode([]) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testParseTokenNotValidJSON() |
108
|
|
|
{ |
109
|
|
|
$this->setExpectedException(InvalidAccessToken::class); |
110
|
|
|
|
111
|
|
|
$accessToken = $this->getProvider()->parseToken( |
112
|
|
|
'lelelelel' |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testGetIdentitySuccess() |
117
|
|
|
{ |
118
|
|
|
$mockedHttpClient = $this->makeIdentityClientResponse( |
119
|
|
|
json_encode( |
120
|
|
|
[ |
121
|
|
|
'response' => [ |
122
|
|
|
[ |
123
|
|
|
'id' => $expectedId = 12321312312312, |
124
|
|
|
'first_name' => $expectedFirstname = 'Dmitry', |
125
|
|
|
'last_name' => $expectedLastname = 'Patsura', |
126
|
|
|
'sex' => 1, |
127
|
|
|
] |
128
|
|
|
] |
129
|
|
|
] |
130
|
|
|
) |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
$result = $this->getProvider($mockedHttpClient)->getIdentity( |
134
|
|
|
new AccessToken( |
135
|
|
|
[ |
136
|
|
|
'access_token' => '123456789' |
137
|
|
|
] |
138
|
|
|
) |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
parent::assertInstanceOf(\SocialConnect\Common\Entity\User::class, $result); |
142
|
|
|
parent::assertSame($expectedId, $result->id); |
143
|
|
|
parent::assertSame($expectedFirstname, $result->firstname); |
144
|
|
|
parent::assertSame($expectedLastname, $result->lastname); |
145
|
|
|
parent::assertSame('female', $result->sex); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @expectedException \SocialConnect\Auth\Provider\Exception\InvalidResponse |
150
|
|
|
* @expectedExceptionMessage API response with error code |
151
|
|
|
*/ |
152
|
|
|
public function testGetIdentityInternalServerError() |
153
|
|
|
{ |
154
|
|
|
$mockedHttpClient = $this->makeIdentityClientResponse( |
155
|
|
|
[], |
156
|
|
|
500 |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
$result = $this->getProvider($mockedHttpClient)->getIdentity( |
160
|
|
|
new AccessToken( |
161
|
|
|
[ |
162
|
|
|
'access_token' => '123456789' |
163
|
|
|
] |
164
|
|
|
) |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @expectedException \SocialConnect\Auth\Provider\Exception\InvalidResponse |
170
|
|
|
* @expectedExceptionMessage API response is not a valid JSON object |
171
|
|
|
*/ |
172
|
|
|
public function testGetIdentityNotData() |
173
|
|
|
{ |
174
|
|
|
$mockedHttpClient = $this->makeIdentityClientResponse( |
175
|
|
|
[], |
176
|
|
|
200 |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
$result = $this->getProvider($mockedHttpClient)->getIdentity( |
180
|
|
|
new AccessToken( |
181
|
|
|
[ |
182
|
|
|
'access_token' => '123456789' |
183
|
|
|
] |
184
|
|
|
) |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|