Passed
Push — master ( 2f5fd4...141183 )
by Petr
02:34
created

XFailedStorage::read()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
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): void
84
    {
85
    }
86
87
    public function updatePassword(string $userName, string $passWord): void
88
    {
89
    }
90
91
    public function deleteAccount(string $userName): void
92
    {
93
    }
94
95
    public function updateCertKeys(string $userName, ?string $certKey, ?string $certSalt): void
96
    {
97
        $this->expectedUser->/** @scrutinizer ignore-call */addCertInfo(strval($certKey), strval($certSalt));
98
    }
99
100
    public function getCertData(string $userName): ?IUserCert
101
    {
102
        return empty($userName) ? null : $this->expectedUser;
103
    }
104
}
105
106
107
class MockCredentials extends ArrayObject
108
{
109
}
110
111
112
class MockUser implements IUser
113
{
114
    public function setData(int $authId, string $authName, int $authGroup, int $authClass, string $displayName, string $dir): void
115
    {
116
    }
117
118
    public function getAuthId(): int
119
    {
120
        return 654;
121
    }
122
123
    public function getAuthName(): string
124
    {
125
        return 'fool';
126
    }
127
128
    public function getGroup(): int
129
    {
130
        return 456789;
131
    }
132
133
    public function getClass(): int
134
    {
135
        return 999;
136
    }
137
138
    public function getDisplayName(): string
139
    {
140
        return 'FooL';
141
    }
142
143
    public function getDir(): string
144
    {
145
        return 'not_available\\:///';
146
    }
147
}
148
149
150
class MockUserToFill extends FileUser
151
{
152
    public function __construct(int $authId, string $authName, int $authGroup, int $authClass, string $displayName, string $dir)
153
    {
154
        $this->setData($authId, $authName, $authGroup, $authClass, $displayName, $dir);
155
    }
156
}
157
158
159
class MockModes implements IMode
160
{
161
    protected $knownPass = '';
162
163
    public function check(string $pass, string $hash): bool
164
    {
165
        return ($this->knownPass == $pass) || ('valid' == $pass);
166
    }
167
168
    public function hash(string $pass, ?string $method = null): string
169
    {
170
        $this->knownPass = $pass;
171
        return 'validPass-' . $pass;
172
    }
173
}
174
175
176
class XFailedStorage implements IStorage
177
{
178
    protected $canOpen = false;
179
    protected $content = '';
180
181
    public function __construct(bool $canOpen = false, string $content = '')
182
    {
183
        $this->canOpen = $canOpen;
184
        $this->content = $content;
185
    }
186
187
    public function canUse(): bool
188
    {
189
        return false;
190
    }
191
192
    public function write(string $sharedKey, $data, ?int $timeout = null): bool
193
    {
194
        throw new StorageException('Mock');
195
    }
196
197
    public function read(string $sharedKey)
198
    {
199
        if ($this->canOpen) {
200
            return $this->content;
201
        }
202
        throw new \kalanis\kw_storage\StorageException('Mock');
203
    }
204
205
    public function remove(string $sharedKey): bool
206
    {
207
        throw new \kalanis\kw_storage\StorageException('Mock');
208
    }
209
210
    public function exists(string $sharedKey): bool
211
    {
212
        throw new \kalanis\kw_storage\StorageException('Mock');
213
    }
214
215
    public function lookup(string $mask): Traversable
216
    {
217
        throw new \kalanis\kw_storage\StorageException('Mock');
218
    }
219
220
    public function increment(string $key): bool
221
    {
222
        throw new \kalanis\kw_storage\StorageException('Mock');
223
    }
224
225
    public function decrement(string $key): bool
226
    {
227
        throw new \kalanis\kw_storage\StorageException('Mock');
228
    }
229
230
    public function removeMulti(array $keys): array
231
    {
232
        throw new \kalanis\kw_storage\StorageException('Mock');
233
    }
234
}
235