|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* SocialConnect project |
|
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Test\OAuth2\Provider; |
|
8
|
|
|
|
|
9
|
|
|
use SocialConnect\Provider\Consumer; |
|
10
|
|
|
use SocialConnect\OAuth2\AccessToken; |
|
11
|
|
|
use SocialConnect\Common\Http\Client\ClientInterface; |
|
12
|
|
|
use SocialConnect\Provider\Session\SessionInterface; |
|
13
|
|
|
|
|
14
|
|
|
class VkTest extends AbstractProviderTestCase |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* {@inheritdoc} |
|
18
|
|
|
*/ |
|
19
|
|
|
protected function getProviderClassName() |
|
20
|
|
|
{ |
|
21
|
|
|
return \SocialConnect\OAuth2\Provider\Vk::class; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param ClientInterface|null $httpClient |
|
26
|
|
|
* @return \SocialConnect\OAuth2\Provider\Vk |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function getProvider(ClientInterface $httpClient = null, SessionInterface $session = null) |
|
29
|
|
|
{ |
|
30
|
|
|
if (!$httpClient) { |
|
31
|
|
|
$httpClient = $this->getMockBuilder(\SocialConnect\Common\Http\Client\Curl::class) |
|
32
|
|
|
->disableOriginalConstructor() |
|
33
|
|
|
->disableProxyingToOriginalMethods() |
|
34
|
|
|
->getMock(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if (!$session) { |
|
38
|
|
|
$session = $this->getMockBuilder(\SocialConnect\Provider\Session\Session::class) |
|
39
|
|
|
->disableOriginalConstructor() |
|
40
|
|
|
->disableProxyingToOriginalMethods() |
|
41
|
|
|
->getMock(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return new \SocialConnect\OAuth2\Provider\Vk( |
|
45
|
|
|
$httpClient, |
|
46
|
|
|
$session, |
|
47
|
|
|
new Consumer( |
|
48
|
|
|
'unknown', |
|
49
|
|
|
'unkwown' |
|
50
|
|
|
), |
|
51
|
|
|
[ |
|
52
|
|
|
'redirectUri' => 'http://localhost:8000/' |
|
53
|
|
|
] |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testMakeAccessTokenRequest() |
|
58
|
|
|
{ |
|
59
|
|
|
$expectedCode = 'djsflkSdjflskdfjFlsd9'; |
|
60
|
|
|
|
|
61
|
|
|
$provider = $this->getProvider(); |
|
62
|
|
|
|
|
63
|
|
|
/** @var \SocialConnect\Common\Http\Request $request */ |
|
64
|
|
|
$request = parent::callProtectedMethod( |
|
65
|
|
|
$provider, |
|
66
|
|
|
'makeAccessTokenRequest', |
|
67
|
|
|
[ |
|
68
|
|
|
$expectedCode |
|
69
|
|
|
] |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
parent::assertInstanceOf(\SocialConnect\Common\Http\Request::class, $request); |
|
73
|
|
|
parent::assertSame($provider->getRequestTokenUri(), $request->getUri()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testGetIdentitySuccess() |
|
77
|
|
|
{ |
|
78
|
|
|
$mockedHttpClient = $this->mockClientResponse( |
|
79
|
|
|
json_encode( |
|
80
|
|
|
[ |
|
81
|
|
|
'response' => [ |
|
82
|
|
|
[ |
|
83
|
|
|
'id' => $expectedId = 12321312312312, |
|
84
|
|
|
'first_name' => $expectedFirstname = 'Dmitry', |
|
85
|
|
|
'last_name' => $expectedLastname = 'Patsura', |
|
86
|
|
|
'sex' => 1, |
|
87
|
|
|
] |
|
88
|
|
|
] |
|
89
|
|
|
] |
|
90
|
|
|
) |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
$result = $this->getProvider($mockedHttpClient)->getIdentity( |
|
94
|
|
|
new AccessToken( |
|
95
|
|
|
[ |
|
96
|
|
|
'access_token' => '123456789' |
|
97
|
|
|
] |
|
98
|
|
|
) |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
parent::assertInstanceOf(\SocialConnect\Common\Entity\User::class, $result); |
|
102
|
|
|
parent::assertSame($expectedId, $result->id); |
|
103
|
|
|
parent::assertSame($expectedFirstname, $result->firstname); |
|
104
|
|
|
parent::assertSame($expectedLastname, $result->lastname); |
|
105
|
|
|
parent::assertSame('female', $result->sex); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @expectedException \SocialConnect\Provider\Exception\InvalidResponse |
|
110
|
|
|
* @expectedExceptionMessage API response is not a valid JSON object |
|
111
|
|
|
*/ |
|
112
|
|
|
public function testGetIdentityNotData() |
|
113
|
|
|
{ |
|
114
|
|
|
$mockedHttpClient = $this->mockClientResponse( |
|
115
|
|
|
[], |
|
116
|
|
|
200 |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
|
|
$result = $this->getProvider($mockedHttpClient)->getIdentity( |
|
|
|
|
|
|
120
|
|
|
new AccessToken( |
|
121
|
|
|
[ |
|
122
|
|
|
'access_token' => '123456789' |
|
123
|
|
|
] |
|
124
|
|
|
) |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|