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
|
6 |
|
protected function getProvider(ClientInterface $httpClient = null) |
22
|
|
|
{ |
23
|
6 |
|
$service = new \SocialConnect\Auth\Service( |
24
|
|
|
[ |
25
|
|
|
'redirectUri' => 'http://localhost:8000/' |
26
|
6 |
|
] |
27
|
6 |
|
); |
28
|
|
|
|
29
|
6 |
|
if ($httpClient) { |
30
|
1 |
|
$service->setHttpClient($httpClient); |
31
|
1 |
|
} |
32
|
|
|
|
33
|
6 |
|
return new \SocialConnect\Auth\Provider\Vk( |
34
|
6 |
|
$service, |
35
|
6 |
|
new Consumer( |
36
|
6 |
|
'unknown', |
37
|
|
|
'unkwown' |
38
|
6 |
|
) |
39
|
6 |
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function testMakeAuthUrl() |
43
|
|
|
{ |
44
|
1 |
|
parent::assertSame( |
|
|
|
|
45
|
1 |
|
'https://oauth.vk.com/authorize?client_id=unknown&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2F%2Fvk%2F&response_type=code', |
46
|
1 |
|
$this->getProvider()->makeAuthUrl() |
47
|
1 |
|
); |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @expectedException \InvalidArgumentException |
52
|
|
|
* @expectedExceptionMessage Parameter $code must be a string |
53
|
|
|
*/ |
54
|
1 |
|
public function testGetAccessTokenFail() |
55
|
|
|
{ |
56
|
1 |
|
$this->getProvider()->getAccessToken(null); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
1 |
|
public function testParseTokenSuccess() |
61
|
|
|
{ |
62
|
1 |
|
$expectedToken = 'XXXXXXXX'; |
63
|
1 |
|
$expectedUserId = 123456; |
64
|
|
|
|
65
|
1 |
|
$accessToken = $this->getProvider()->parseToken( |
66
|
1 |
|
json_encode( |
67
|
|
|
[ |
68
|
1 |
|
'access_token' => $expectedToken, |
69
|
|
|
'user_id' => $expectedUserId |
70
|
1 |
|
] |
71
|
1 |
|
) |
72
|
1 |
|
); |
73
|
|
|
|
74
|
1 |
|
parent::assertInstanceOf(AccessToken::class, $accessToken); |
|
|
|
|
75
|
1 |
|
parent::assertSame($expectedToken, $accessToken->getToken()); |
|
|
|
|
76
|
1 |
|
parent::assertSame($expectedUserId, $accessToken->getUserId()); |
|
|
|
|
77
|
1 |
|
} |
78
|
|
|
|
79
|
1 |
|
public function testParseTokenNotToken() |
80
|
|
|
{ |
81
|
1 |
|
$this->setExpectedException(InvalidAccessToken::class); |
82
|
|
|
|
83
|
1 |
|
$accessToken = $this->getProvider()->parseToken( |
|
|
|
|
84
|
1 |
|
json_encode([]) |
85
|
1 |
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function testParseTokenNotValidJSON() |
89
|
|
|
{ |
90
|
1 |
|
$this->setExpectedException(InvalidAccessToken::class); |
91
|
|
|
|
92
|
1 |
|
$accessToken = $this->getProvider()->parseToken( |
|
|
|
|
93
|
|
|
'lelelelel' |
94
|
1 |
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public function testGetIdentitySuccess() |
98
|
|
|
{ |
99
|
1 |
|
$mockedHttpClient = $this->makeIdentityClientResponse( |
100
|
|
|
[ |
101
|
|
|
'response' => [ |
102
|
|
|
[ |
103
|
1 |
|
'id' => $expectedId = 12321312312312, |
104
|
1 |
|
'first_name' => $expectedFirstname = 'Dmitry', |
105
|
1 |
|
'last_name' => $expectedLastname = 'Patsura', |
106
|
|
|
] |
107
|
1 |
|
] |
108
|
1 |
|
] |
109
|
1 |
|
); |
110
|
|
|
|
111
|
1 |
|
$result = $this->getProvider($mockedHttpClient)->getIdentity( |
112
|
1 |
|
new AccessToken( |
113
|
|
|
[ |
114
|
|
|
'access_token' => '123456789' |
115
|
1 |
|
] |
116
|
1 |
|
) |
117
|
1 |
|
); |
118
|
|
|
|
119
|
1 |
|
parent::assertInstanceOf(\SocialConnect\Common\Entity\User::class, $result); |
|
|
|
|
120
|
1 |
|
parent::assertSame($expectedId, $result->id); |
|
|
|
|
121
|
1 |
|
parent::assertSame($expectedFirstname, $result->firstname); |
|
|
|
|
122
|
1 |
|
parent::assertSame($expectedLastname, $result->lastname); |
|
|
|
|
123
|
1 |
|
} |
124
|
|
|
} |
125
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.