1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nord\Lumen\OAuth2\Tests; |
4
|
|
|
|
5
|
|
|
use Nord\Lumen\OAuth2\OAuth2Service; |
6
|
|
|
|
7
|
|
|
class OAuth2ServiceTest extends \Codeception\Test\Unit |
8
|
|
|
{ |
9
|
|
|
use \Codeception\Specify; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @inheritdoc |
13
|
|
|
*/ |
14
|
|
|
protected function setup() |
15
|
|
|
{ |
16
|
|
|
\Helper\Unit::setAuthorizationHeader(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
public function testAssertIssueAccessToken() |
23
|
|
|
{ |
24
|
|
|
$this->specify('verify service issueAccessToken', function () { |
25
|
|
|
$service = $this->createService(); |
26
|
|
|
verify($service->issueAccessToken())->equals(MockAccessToken::toArray()); |
27
|
|
|
}); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* |
32
|
|
|
*/ |
33
|
|
|
public function testAssertValidateAccessToken() |
34
|
|
|
{ |
35
|
|
|
$this->specify('verify service validateAccessToken', function () { |
36
|
|
|
$service = $this->createService(); |
37
|
|
|
verify($this->validateAccessToken($service))->true(); |
38
|
|
|
}); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function testAssertGetResourceOwnerId() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$this->specify('verify service can getResourceOwnerId', function () { |
47
|
|
|
$service = $this->createService(); |
48
|
|
|
$this->validateAccessToken($service); |
49
|
|
|
verify($service->getResourceOwnerId())->equals('test'); |
50
|
|
|
}); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function testAssertGetResourceOwnerType() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$this->specify('verify service can getResourceOwnerType', function () { |
59
|
|
|
$service = $this->createService(); |
60
|
|
|
$this->validateAccessToken($service); |
61
|
|
|
verify($service->getResourceOwnerType())->equals('test'); |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function testAssertGetClientId() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$this->specify('verify service can getClientId', function () { |
71
|
|
|
$service = $this->createService(); |
72
|
|
|
$this->validateAccessToken($service); |
73
|
|
|
verify($service->getClientId())->equals('test'); |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param OAuth2Service $service |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
private function validateAccessToken($service) |
82
|
|
|
{ |
83
|
|
|
return $service->validateAccessToken(true, MockAccessToken::$accessToken); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return OAuth2Service |
88
|
|
|
*/ |
89
|
|
|
private function createService() |
90
|
|
|
{ |
91
|
|
|
return new OAuth2Service($this->createAuthorizationServer(), $this->createResourceServer()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return MockAuthorizationServer |
96
|
|
|
*/ |
97
|
|
|
private function createAuthorizationServer() |
98
|
|
|
{ |
99
|
|
|
return new MockAuthorizationServer(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return \League\OAuth2\Server\ResourceServer |
104
|
|
|
*/ |
105
|
|
|
private function createResourceServer() |
106
|
|
|
{ |
107
|
|
|
return new \League\OAuth2\Server\ResourceServer( |
108
|
|
|
new MockSessionStorage(), |
109
|
|
|
new MockAccessTokenStorage(), |
110
|
|
|
new MockClientStorage(), |
111
|
|
|
new MockScopeStorage() |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.