1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Nord\Lumen\OAuth2\OAuth2Service; |
4
|
|
|
|
5
|
|
|
class OAuth2ServiceTest extends \Codeception\TestCase\Test |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
use Codeception\Specify; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var \UnitTester |
11
|
|
|
*/ |
12
|
|
|
protected $tester; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected static $token = [ |
18
|
|
|
'access_token' => 'mF_9.B5f-4.1JqM', |
19
|
|
|
'token_type' => 'Bearer', |
20
|
|
|
'expires_in' => 3600, |
21
|
|
|
'refresh_token' => 'tGzv3JOkF0XG5Qx2TlKWIA' |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
|
|
protected function setup() |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$_SERVER['HTTP_AUTHORIZATION'] = 'Bearer mF_9.B5f-4.1JqM'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
public function testAssertIssueAccessToken() |
36
|
|
|
{ |
37
|
|
|
$this->specify('verify service issueAccessToken', function () { |
38
|
|
|
$authorizationServer = $this->createAuthorizationServer(); |
39
|
|
|
$authorizationServer->expects($this->once()) |
|
|
|
|
40
|
|
|
->method('issueAccessToken') |
41
|
|
|
->will($this->returnValue(self::$token)); |
42
|
|
|
|
43
|
|
|
$service = new OAuth2Service($authorizationServer, $this->createResourceServer()); |
44
|
|
|
verify($service->issueAccessToken())->equals(self::$token); |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function testAssertValidateAccessToken() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this->specify('verify service validateAccessToken', function () { |
54
|
|
|
$service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer()); |
55
|
|
|
verify($service->validateAccessToken(true, self::$token['access_token']))->true(); |
56
|
|
|
}); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function testAssertGetResourceOwnerId() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$this->specify('verify service can getResourceOwnerId', function () { |
65
|
|
|
$service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer()); |
66
|
|
|
$service->validateAccessToken(true, self::$token['access_token']); |
67
|
|
|
verify($service->getResourceOwnerId())->equals('test'); |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function testAssertGetResourceOwnerType() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$this->specify('verify service can getResourceOwnerType', function () { |
77
|
|
|
$service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer()); |
78
|
|
|
$service->validateAccessToken(true, self::$token['access_token']); |
79
|
|
|
verify($service->getResourceOwnerType())->equals('test'); |
80
|
|
|
}); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function testAssertGetClientId() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
$this->specify('verify service can getClientId', function () { |
89
|
|
|
$service = new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer()); |
90
|
|
|
$service->validateAccessToken(true, self::$token['access_token']); |
91
|
|
|
verify($service->getClientId())->equals('test'); |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return League\OAuth2\Server\AuthorizationServer |
97
|
|
|
*/ |
98
|
|
|
private function createAuthorizationServer() |
99
|
|
|
{ |
100
|
|
|
$authorizationServer = $this->getMockBuilder(League\OAuth2\Server\AuthorizationServer::class) |
101
|
|
|
->disableOriginalConstructor() |
102
|
|
|
->getMock(); |
103
|
|
|
|
104
|
|
|
return $authorizationServer; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return League\OAuth2\Server\ResourceServer |
109
|
|
|
*/ |
110
|
|
|
private function createResourceServer() |
111
|
|
|
{ |
112
|
|
|
return new League\OAuth2\Server\ResourceServer( |
113
|
|
|
new MockSessionStorage(), |
114
|
|
|
new MockAccessTokenStorage(), |
115
|
|
|
new MockClientStorage(), |
116
|
|
|
new MockScopeStorage() |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.