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