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\Exception\InvalidAccessToken; |
10
|
|
|
use SocialConnect\Auth\Provider\Consumer; |
11
|
|
|
use SocialConnect\Auth\Provider\OAuth2\AccessToken; |
12
|
|
|
use SocialConnect\Common\Http\Client\ClientInterface; |
13
|
|
|
use Test\TestCase; |
14
|
|
|
|
15
|
|
|
class VkTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param ClientInterface|null $httpClient |
19
|
|
|
* @return \SocialConnect\Vk\Provider |
20
|
|
|
*/ |
21
|
|
|
protected function getProvider(ClientInterface $httpClient = null) |
22
|
|
|
{ |
23
|
|
|
$service = new \SocialConnect\Auth\Service([]); |
24
|
|
|
|
25
|
|
|
if ($httpClient) { |
26
|
|
|
$service->setHttpClient($httpClient); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return new \SocialConnect\Vk\Provider( |
30
|
|
|
$service, |
31
|
|
|
new Consumer( |
32
|
|
|
'unknown', |
33
|
|
|
'unkwown' |
34
|
|
|
) |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testParseTokenSuccess() |
39
|
|
|
{ |
40
|
|
|
$expectedToken = 'XXXXXXXX'; |
41
|
|
|
|
42
|
|
|
$accessToken = $this->getProvider()->parseToken( |
43
|
|
|
json_encode( |
44
|
|
|
[ |
45
|
|
|
'access_token' => $expectedToken |
46
|
|
|
] |
47
|
|
|
) |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
parent::assertInstanceOf(AccessToken::class, $accessToken); |
51
|
|
|
parent::assertSame($expectedToken, $accessToken->getToken()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testParseTokenNotToken() |
55
|
|
|
{ |
56
|
|
|
$this->setExpectedException(InvalidAccessToken::class); |
57
|
|
|
|
58
|
|
|
$accessToken = $this->getProvider()->parseToken( |
59
|
|
|
json_encode([]) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testParseTokenNotValidJSON() |
64
|
|
|
{ |
65
|
|
|
$this->setExpectedException(InvalidAccessToken::class); |
66
|
|
|
|
67
|
|
|
$accessToken = $this->getProvider()->parseToken( |
68
|
|
|
'lelelelel' |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testGetIdentitySuccess() |
73
|
|
|
{ |
74
|
|
|
$mockedHttpClient = $this->getMockBuilder(\SocialConnect\Common\Http\Client\Curl::class) |
75
|
|
|
->disableProxyingToOriginalMethods() |
76
|
|
|
->getMock(); |
77
|
|
|
|
78
|
|
|
$response = new \SocialConnect\Common\Http\Response( |
79
|
|
|
200, |
80
|
|
|
json_encode( |
81
|
|
|
[ |
82
|
|
|
'response' => [ |
83
|
|
|
[ |
84
|
|
|
'id' => $expectedId = 12321312312312, |
85
|
|
|
'first_name' => $expectedFirstname = 'Dmitry', |
86
|
|
|
'last_name' => $expectedLastname = 'Patsura', |
87
|
|
|
] |
88
|
|
|
] |
89
|
|
|
] |
90
|
|
|
), |
91
|
|
|
[] |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
$mockedHttpClient->expects($this->once()) |
95
|
|
|
->method('request') |
96
|
|
|
->willReturn($response); |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
$result = $this->getProvider($mockedHttpClient)->getIdentity( |
100
|
|
|
new AccessToken('unknown') |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
parent::assertInstanceOf(\SocialConnect\Common\Entity\User::class, $result); |
104
|
|
|
parent::assertSame($expectedId, $result->id); |
105
|
|
|
parent::assertSame($expectedFirstname, $result->firstname); |
106
|
|
|
parent::assertSame($expectedLastname, $result->lastname); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|