|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Admin\Oauth2\Repository; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Admin\Exception\Database; |
|
8
|
|
|
use AbterPhp\Admin\Oauth2\Entity\Scope as Entity; |
|
9
|
|
|
use AbterPhp\Framework\TestCase\Database\QueryTestCase; |
|
10
|
|
|
use AbterPhp\Framework\TestDouble\Database\MockStatementFactory; |
|
11
|
|
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
|
12
|
|
|
use League\OAuth2\Server\Entities\ScopeEntityInterface; |
|
13
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
14
|
|
|
|
|
15
|
|
|
class ScopeTest extends QueryTestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var Scope - System Under Test */ |
|
18
|
|
|
protected $sut; |
|
19
|
|
|
|
|
20
|
|
|
public function setUp(): void |
|
21
|
|
|
{ |
|
22
|
|
|
parent::setUp(); |
|
23
|
|
|
|
|
24
|
|
|
$this->sut = new Scope($this->connectionPoolMock); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function testGetScopeEntityByIdentifier() |
|
28
|
|
|
{ |
|
29
|
|
|
$id = 'c9f0176b-d6f2-4f31-802f-67ad253f9fe7'; |
|
30
|
|
|
$identifier = 'foo'; |
|
31
|
|
|
|
|
32
|
|
|
$sql0 = 'SELECT ar.id FROM admin_resources AS ar WHERE (ar.deleted_at IS NULL) AND (ar.identifier = :identifier)'; // phpcs:ignore |
|
33
|
|
|
$valuesToBind0 = [ |
|
34
|
|
|
'identifier' => [$identifier, \PDO::PARAM_STR], |
|
35
|
|
|
]; |
|
36
|
|
|
$row0 = ['id' => $id]; |
|
37
|
|
|
$statement0 = MockStatementFactory::createReadRowStatement($this, $valuesToBind0, $row0); |
|
38
|
|
|
|
|
39
|
|
|
$this->readConnectionMock |
|
40
|
|
|
->expects($this->exactly(1)) |
|
41
|
|
|
->method('prepare') |
|
42
|
|
|
->withConsecutive([$sql0]) |
|
43
|
|
|
->willReturnOnConsecutiveCalls($statement0); |
|
44
|
|
|
|
|
45
|
|
|
$actualResult = $this->sut->getScopeEntityByIdentifier($identifier); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertInstanceOf(ScopeEntityInterface::class, $actualResult); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testGetScopeEntityByIdentifierThrowsExceptionOnFailure() |
|
51
|
|
|
{ |
|
52
|
|
|
$expectedCode = 17; |
|
53
|
|
|
$expectedMessage = 'Foo is great before: FROM api_clients AS ac'; |
|
54
|
|
|
|
|
55
|
|
|
$this->expectException(Database::class); |
|
56
|
|
|
$this->expectExceptionCode($expectedCode); |
|
57
|
|
|
$this->expectExceptionMessage($expectedMessage); |
|
58
|
|
|
|
|
59
|
|
|
$id = 'c9f0176b-d6f2-4f31-802f-67ad253f9fe7'; |
|
|
|
|
|
|
60
|
|
|
$identifier = 'foo'; |
|
61
|
|
|
|
|
62
|
|
|
$sql0 = 'SELECT ar.id FROM admin_resources AS ar WHERE (ar.deleted_at IS NULL) AND (ar.identifier = :identifier)'; // phpcs:ignore |
|
63
|
|
|
$valuesToBind0 = [ |
|
64
|
|
|
'identifier' => [$identifier, \PDO::PARAM_STR], |
|
65
|
|
|
]; |
|
66
|
|
|
$errorInfo0 = ['FOO', $expectedCode, $expectedMessage]; |
|
67
|
|
|
$statement0 = MockStatementFactory::createErrorStatement($this, $valuesToBind0, $errorInfo0); |
|
68
|
|
|
|
|
69
|
|
|
$this->readConnectionMock |
|
70
|
|
|
->expects($this->exactly(1)) |
|
71
|
|
|
->method('prepare') |
|
72
|
|
|
->withConsecutive([$sql0]) |
|
73
|
|
|
->willReturnOnConsecutiveCalls($statement0); |
|
74
|
|
|
|
|
75
|
|
|
$this->sut->getScopeEntityByIdentifier($identifier); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testFinalizeScopesWithoutScopes() |
|
79
|
|
|
{ |
|
80
|
|
|
$grantType = 'foo'; |
|
81
|
|
|
$scopes = []; |
|
82
|
|
|
|
|
83
|
|
|
/** @var ClientEntityInterface|MockObject $clientEntityMock */ |
|
84
|
|
|
$clientEntityMock = $this->createMock(ClientEntityInterface::class); |
|
85
|
|
|
|
|
86
|
|
|
$actualResult = $this->sut->finalizeScopes($scopes, $grantType, $clientEntityMock, null); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertSame([], $actualResult); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testFinalizeScopesWithScopes() |
|
92
|
|
|
{ |
|
93
|
|
|
$clientIdentifier = 'quix'; |
|
94
|
|
|
$grantType = 'foo'; |
|
95
|
|
|
|
|
96
|
|
|
$scopeIdentifier0 = 'bar'; |
|
97
|
|
|
$scopeIdentifier1 = 'baz'; |
|
98
|
|
|
$scopes = [ |
|
99
|
|
|
$this->createScopeStub($scopeIdentifier0), |
|
100
|
|
|
$this->createScopeStub($scopeIdentifier1), |
|
101
|
|
|
]; |
|
102
|
|
|
|
|
103
|
|
|
$arId0 = 'cfa57635-bed4-4f59-a7c8-091fb0c05964'; |
|
104
|
|
|
$arId1 = 'ddd844f3-6894-4049-821d-3ed461e2e970'; |
|
105
|
|
|
$arId2 = '3966099c-84ff-48cf-9d65-794519651fe5'; |
|
106
|
|
|
|
|
107
|
|
|
/** @var ClientEntityInterface|MockObject $clientEntityMock */ |
|
108
|
|
|
$clientEntityMock = $this->createMock(ClientEntityInterface::class); |
|
109
|
|
|
$clientEntityMock->expects($this->any())->method('getIdentifier')->willReturn($clientIdentifier); |
|
110
|
|
|
|
|
111
|
|
|
$sql0 = 'SELECT acar.admin_resource_id FROM api_clients_admin_resources AS acar WHERE (acar.api_client_id = ?) AND (acar.admin_resource_id IN (?,?))'; // phpcs:ignore |
|
112
|
|
|
$valuesToBind0 = [ |
|
113
|
|
|
[$clientIdentifier, \PDO::PARAM_STR], |
|
114
|
|
|
[$scopeIdentifier0, \PDO::PARAM_STR], |
|
115
|
|
|
[$scopeIdentifier1, \PDO::PARAM_STR], |
|
116
|
|
|
]; |
|
117
|
|
|
$rows0 = [ |
|
118
|
|
|
['admin_resource_id' => $arId0], |
|
119
|
|
|
['admin_resource_id' => $arId1], |
|
120
|
|
|
['admin_resource_id' => $arId2], |
|
121
|
|
|
]; |
|
122
|
|
|
$statement0 = MockStatementFactory::createReadStatement($this, $valuesToBind0, $rows0); |
|
123
|
|
|
|
|
124
|
|
|
$this->readConnectionMock |
|
125
|
|
|
->expects($this->once()) |
|
126
|
|
|
->method('prepare') |
|
127
|
|
|
->with($sql0) |
|
128
|
|
|
->willReturn($statement0); |
|
129
|
|
|
|
|
130
|
|
|
$actualResult = $this->sut->finalizeScopes($scopes, $grantType, $clientEntityMock, null); |
|
131
|
|
|
|
|
132
|
|
|
$this->assertCount(3, $actualResult); |
|
133
|
|
|
$this->assertSame($arId0, $actualResult[0]->getIdentifier()); |
|
134
|
|
|
$this->assertSame($arId1, $actualResult[1]->getIdentifier()); |
|
135
|
|
|
$this->assertSame($arId2, $actualResult[2]->getIdentifier()); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function testFinalizeScopesWithScopesThrowsExceptionOnFailure() |
|
139
|
|
|
{ |
|
140
|
|
|
$expectedCode = 17; |
|
141
|
|
|
$expectedMessage = 'Foo is great before: FROM api_clients AS ac'; |
|
142
|
|
|
|
|
143
|
|
|
$this->expectException(Database::class); |
|
144
|
|
|
$this->expectExceptionCode($expectedCode); |
|
145
|
|
|
$this->expectExceptionMessage($expectedMessage); |
|
146
|
|
|
|
|
147
|
|
|
$clientIdentifier = 'quix'; |
|
148
|
|
|
$grantType = 'foo'; |
|
149
|
|
|
|
|
150
|
|
|
$scopeIdentifier0 = 'bar'; |
|
151
|
|
|
$scopeIdentifier1 = 'baz'; |
|
152
|
|
|
$scopes = [ |
|
153
|
|
|
$this->createScopeStub($scopeIdentifier0), |
|
154
|
|
|
$this->createScopeStub($scopeIdentifier1), |
|
155
|
|
|
]; |
|
156
|
|
|
|
|
157
|
|
|
/** @var ClientEntityInterface|MockObject $clientEntityMock */ |
|
158
|
|
|
$clientEntityMock = $this->createMock(ClientEntityInterface::class); |
|
159
|
|
|
$clientEntityMock->expects($this->any())->method('getIdentifier')->willReturn($clientIdentifier); |
|
160
|
|
|
|
|
161
|
|
|
$sql0 = 'SELECT acar.admin_resource_id FROM api_clients_admin_resources AS acar WHERE (acar.api_client_id = ?) AND (acar.admin_resource_id IN (?,?))'; // phpcs:ignore |
|
162
|
|
|
$valuesToBind0 = [ |
|
163
|
|
|
[$clientIdentifier, \PDO::PARAM_STR], |
|
164
|
|
|
[$scopeIdentifier0, \PDO::PARAM_STR], |
|
165
|
|
|
[$scopeIdentifier1, \PDO::PARAM_STR], |
|
166
|
|
|
]; |
|
167
|
|
|
$errorInfo0 = ['FOO', $expectedCode, $expectedMessage]; |
|
168
|
|
|
$statement0 = MockStatementFactory::createErrorStatement($this, $valuesToBind0, $errorInfo0); |
|
169
|
|
|
|
|
170
|
|
|
$this->readConnectionMock |
|
171
|
|
|
->expects($this->exactly(1)) |
|
172
|
|
|
->method('prepare') |
|
173
|
|
|
->withConsecutive([$sql0]) |
|
174
|
|
|
->willReturnOnConsecutiveCalls($statement0); |
|
175
|
|
|
|
|
176
|
|
|
$this->sut->finalizeScopes($scopes, $grantType, $clientEntityMock, null); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param string $identifier |
|
181
|
|
|
* |
|
182
|
|
|
* @return Entity|MockObject |
|
183
|
|
|
*/ |
|
184
|
|
|
protected function createScopeStub(string $identifier): Entity |
|
185
|
|
|
{ |
|
186
|
|
|
$scopeStub = $this->createMock(Entity::class); |
|
187
|
|
|
$scopeStub->expects($this->any())->method('getIdentifier')->willReturn($identifier); |
|
188
|
|
|
|
|
189
|
|
|
return $scopeStub; |
|
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|