Passed
Push — master ( b28c40...f61e74 )
by Petr
08:06
created

MockAuth::authenticate()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 6
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 8
rs 9.6111
1
<?php
2
3
use kalanis\kw_auth\Data\FileUser;
4
use kalanis\kw_auth\Interfaces\IAuth;
5
use kalanis\kw_auth\Interfaces\IAuthCert;
6
use kalanis\kw_auth\Interfaces\IMode;
7
use kalanis\kw_auth\Interfaces\IUser;
8
use kalanis\kw_auth\Interfaces\IUserCert;
9
use kalanis\kw_locks\LockException;
10
use kalanis\kw_locks\Methods as LockMethod;
11
use kalanis\kw_locks\Interfaces as LockInt;
12
use kalanis\kw_storage\Interfaces\IStorage;
13
use kalanis\kw_storage\StorageException;
14
use PHPUnit\Framework\TestCase;
15
16
17
/**
18
 * Class CommonTestClass
19
 * The structure for mocking and configuration seems so complicated, but it's necessary to let it be totally idiot-proof
20
 */
21
class CommonTestClass extends TestCase
22
{
23
    /**
24
     * @throws LockException
25
     * @return LockMethod\FileLock
26
     */
27
    protected function getLockPath(): LockMethod\FileLock
28
    {
29
        return new LockMethod\FileLock(
30
            __DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . LockInt\ILock::LOCK_FILE
31
        );
32
    }
33
}
34
35
36
class MockAuth implements IAuth
37
{
38
    protected $expectedUser = null;
39
    protected $expectedPass = '';
40
41
    public function __construct(IUser $expectedUser = null, string $expectedPass = '')
42
    {
43
        $this->expectedUser = $expectedUser;
44
        $this->expectedPass = $expectedPass;
45
    }
46
47
    public function getDataOnly(string $userName): ?IUser
48
    {
49
        return $this->expectedUser;
50
    }
51
52
    public function authenticate(string $userName, array $params = []): ?IUser
53
    {
54
        return (
55
            $this->expectedUser
56
            && ($this->expectedUser->getAuthName() == $userName)
57
            && isset($params['password'])
58
            && ($params['password'] == $this->expectedPass)
59
        ) ? $this->expectedUser : null;
60
    }
61
}
62
63
64
class MockAuthCert extends MockAuth implements IAuthCert
65
{
66
    /** @var IUserCert|null */
67
    protected $expectedUser = null;
68
69
    public function __construct(IUserCert $expectedUser = null, string $expectedPass = '')
70
    {
71
        parent::__construct($expectedUser, $expectedPass);
72
    }
73
74
    public function createAccount(IUser $user, string $password): void
75
    {
76
    }
77
78
    public function readAccounts(): array
79
    {
80
        return [];
81
    }
82
83
    public function updateAccount(IUser $user): bool
84
    {
85
        return true;
86
    }
87
88
    public function updatePassword(string $userName, string $passWord): bool
89
    {
90
        return true;
91
    }
92
93
    public function deleteAccount(string $userName): bool
94
    {
95
        return true;
96
    }
97
98
    public function updateCertKeys(string $userName, ?string $certKey, ?string $certSalt): void
99
    {
100
        $this->expectedUser->/** @scrutinizer ignore-call */addCertInfo(strval($certKey), strval($certSalt));
101
    }
102
103
    public function getCertData(string $userName): ?IUserCert
104
    {
105
        return empty($userName) ? null : $this->expectedUser;
106
    }
107
}
108
109
110
class MockCredentials extends ArrayObject
111
{
112
}
113
114
115
class MockUser implements IUser
116
{
117
    public function setData(int $authId, string $authName, int $authGroup, int $authClass, ?int $authStatus, string $displayName, string $dir): void
118
    {
119
    }
120
121
    public function getAuthId(): int
122
    {
123
        return 654;
124
    }
125
126
    public function getAuthName(): string
127
    {
128
        return 'fool';
129
    }
130
131
    public function getGroup(): int
132
    {
133
        return 456789;
134
    }
135
136
    public function getClass(): int
137
    {
138
        return 999;
139
    }
140
141
    public function getStatus(): ?int
142
    {
143
        return static::USER_STATUS_ENABLED;
144
    }
145
146
    public function getDisplayName(): string
147
    {
148
        return 'FooL';
149
    }
150
151
    public function getDir(): string
152
    {
153
        return 'not_available\\:///';
154
    }
155
}
156
157
158
class MockUserToFill extends FileUser
159
{
160
    public function __construct(int $authId, string $authName, int $authGroup, int $authClass, ?int $authStatus, string $displayName, string $dir)
161
    {
162
        $this->setData($authId, $authName, $authGroup, $authClass, $authStatus, $displayName, $dir);
163
    }
164
}
165
166
167
class MockModes implements IMode
168
{
169
    protected $knownPass = '';
170
171
    public function check(string $pass, string $hash): bool
172
    {
173
        return ($this->knownPass == $pass) || ('valid' == $pass);
174
    }
175
176
    public function hash(string $pass, ?string $method = null): string
177
    {
178
        $this->knownPass = $pass;
179
        return 'validPass-' . $pass;
180
    }
181
}
182
183
184
class XFailedStorage implements IStorage
185
{
186
    protected $canOpen = false;
187
    protected $content = '';
188
189
    public function __construct(bool $canOpen = false, string $content = '')
190
    {
191
        $this->canOpen = $canOpen;
192
        $this->content = $content;
193
    }
194
195
    public function canUse(): bool
196
    {
197
        return false;
198
    }
199
200
    public function isFlat(): bool
201
    {
202
        return false;
203
    }
204
205
    public function write(string $sharedKey, $data, ?int $timeout = null): bool
206
    {
207
        throw new StorageException('Mock');
208
    }
209
210
    public function read(string $sharedKey)
211
    {
212
        if ($this->canOpen) {
213
            return $this->content;
214
        }
215
        throw new \kalanis\kw_storage\StorageException('Mock');
216
    }
217
218
    public function remove(string $sharedKey): bool
219
    {
220
        throw new \kalanis\kw_storage\StorageException('Mock');
221
    }
222
223
    public function exists(string $sharedKey): bool
224
    {
225
        throw new \kalanis\kw_storage\StorageException('Mock');
226
    }
227
228
    public function lookup(string $mask): Traversable
229
    {
230
        throw new \kalanis\kw_storage\StorageException('Mock');
231
    }
232
233
    public function increment(string $key): bool
234
    {
235
        throw new \kalanis\kw_storage\StorageException('Mock');
236
    }
237
238
    public function decrement(string $key): bool
239
    {
240
        throw new \kalanis\kw_storage\StorageException('Mock');
241
    }
242
243
    public function removeMulti(array $keys): array
244
    {
245
        throw new \kalanis\kw_storage\StorageException('Mock');
246
    }
247
}
248