Passed
Push — master ( 141183...2b56eb )
by Petr
08:03
created

XAUser::setData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 7
dl 0
loc 2
rs 10
1
<?php
2
3
namespace BasicTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_address_handler\Handler;
8
use kalanis\kw_address_handler\Sources as HandlerSources;
9
use kalanis\kw_auth\Auth;
10
use kalanis\kw_auth\AuthException;
11
use kalanis\kw_auth\AuthTree;
12
use kalanis\kw_auth\Interfaces;
13
use kalanis\kw_auth\Methods;
14
use kalanis\kw_auth\Mode\KwOrig;
15
use kalanis\kw_auth\Sources;
16
use kalanis\kw_auth\Statuses\Always;
17
use kalanis\kw_locks\LockException;
18
19
20
class AuthTest extends CommonTestClass
21
{
22
    public function testStatical(): void
23
    {
24
        $this->assertEmpty(Auth::getTree());
25
        Auth::fill(
26
            new Methods\Everytime(null, null)
27
        );
28
        $this->assertNotEmpty(Auth::getTree());
29
30
        $this->assertEmpty(Auth::getAuthenticator());
31
        $this->assertEmpty(Auth::getAuth());
32
        $this->assertEmpty(Auth::getAccounts());
33
        $this->assertEmpty(Auth::getClasses());
34
        $this->assertEmpty(Auth::getGroups());
35
        Auth::setAuthenticator(new XAUser());
36
        Auth::setAuth(new XAAuth());
37
        Auth::setAccounts(new XAAccounts());
38
        Auth::setClasses(new XAClasses());
39
        Auth::setGroups(new XAGroups());
40
        $this->assertNotEmpty(Auth::getAuthenticator());
41
        $this->assertNotEmpty(Auth::getAuth());
42
        $this->assertNotEmpty(Auth::getAccounts());
43
        $this->assertNotEmpty(Auth::getClasses());
44
        $this->assertNotEmpty(Auth::getGroups());
45
    }
46
47
    /**
48
     * @throws AuthException
49
     * @throws LockException
50
     */
51
    public function testTree(): void
52
    {
53
        $tree = new AuthTree();
54
        $this->assertEmpty($tree->getMethod());
55
56
        // this is what should be in bootstrap
57
        $tree->setTree(
58
            new Methods\HttpDigest(
59
                $this->fileSources(),
60
                new Methods\Everytime(null, null),
61
                new \MockCredentials([
62
                    Methods\HttpDigest::INPUT_METHOD => 'PUT',
63
                    Methods\HttpDigest::INPUT_DIGEST => '0123456789qwertzuiopasdfghjklyxcvbnm--',
64
                ])
65
            )
66
        );
67
68
        // now run that
69
        $this->assertEmpty($tree->getMethod());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $tree->getMethod() targeting kalanis\kw_auth\AuthTree::getMethod() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
70
        $tree->findMethod(new \MockCredentials());
71
        $this->assertNotEmpty($tree->getMethod());
72
        $this->assertTrue($tree->getMethod()->isAuthorized());
73
        $this->assertEquals('Debug', $tree->getMethod()->getLoggedUser()->getAuthName());
74
75
        // tree with data from url
76
        $tree->setTree(new Methods\UrlCerts(
77
            $this->fileSources(),
78
            null,
79
            new Handler(new HandlerSources\Address('//abcdef/ghi/jkl'))
80
        ));
81
        $this->assertEmpty($tree->getMethod());
0 ignored issues
show
Bug introduced by
Are you sure the usage of $tree->getMethod() targeting kalanis\kw_auth\AuthTree::getMethod() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
82
        $tree->findMethod(new \MockCredentials());
83
        $this->assertEmpty($tree->getMethod());
84
    }
85
86
    /**
87
     * Contains a full comedy/tragedy of work with locks
88
     * @throws LockException
89
     * @return Sources\Files\Volume\Files
90
     */
91
    protected function fileSources(): Sources\Files\Volume\Files
92
    {
93
        return new Sources\Files\Volume\Files(
94
            new KwOrig('yxcvbnmasdfghjklqwertzuiop0123456789'),
95
            new Always(),
96
            $this->getLockPath(),
97
            __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data'
98
        );
99
    }
100
}
101
102
103
class XAUser implements Interfaces\IUser
104
{
105
106
    public function setData(int $authId, string $authName, int $authGroup, int $authClass, ?int $authStatus, string $displayName, string $dir): void
107
    {
108
    }
109
110
    public function getAuthId(): int
111
    {
112
        return 0;
113
    }
114
115
    public function getAuthName(): string
116
    {
117
        return '';
118
    }
119
120
    public function getGroup(): int
121
    {
122
        return 0;
123
    }
124
125
    public function getClass(): int
126
    {
127
        return 0;
128
    }
129
130
    public function getStatus(): ?int
131
    {
132
        return static::USER_STATUS_UNKNOWN;
133
    }
134
135
    public function getDisplayName(): string
136
    {
137
        return '';
138
    }
139
140
    public function getDir(): string
141
    {
142
        return '';
143
    }
144
}
145
146
147
class XAAuth implements Interfaces\IAuth
148
{
149
    public function getDataOnly(string $userName): ?Interfaces\IUser
150
    {
151
        return null;
152
    }
153
154
    public function authenticate(string $userName, array $params = []): ?Interfaces\IUser
155
    {
156
        return null;
157
    }
158
}
159
160
161
class XAAccounts implements Interfaces\IAccessAccounts
162
{
163
    public function createAccount(Interfaces\IUser $user, string $password): void
164
    {
165
    }
166
167
    public function readAccounts(): array
168
    {
169
        return [];
170
    }
171
172
    public function updateAccount(Interfaces\IUser $user): bool
173
    {
174
        return true;
175
    }
176
177
    public function updatePassword(string $userName, string $passWord): bool
178
    {
179
        return true;
180
    }
181
182
    public function deleteAccount(string $userName): bool
183
    {
184
        return true;
185
    }
186
}
187
188
189
class XAGroups implements Interfaces\IAccessGroups
190
{
191
192
    public function createGroup(Interfaces\IGroup $group): void
193
    {
194
    }
195
196
    public function getGroupDataOnly(int $groupId): ?Interfaces\IGroup
197
    {
198
        return null;
199
    }
200
201
    public function readGroup(): array
202
    {
203
        return [];
204
    }
205
206
    public function updateGroup(Interfaces\IGroup $group): bool
207
    {
208
        return true;
209
    }
210
211
    public function deleteGroup(int $groupId): bool
212
    {
213
        return true;
214
    }
215
}
216
217
218
class XAClasses implements Interfaces\IAccessClasses
219
{
220
    public function readClasses(): array
221
    {
222
        return [];
223
    }
224
}
225