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

GroupsTest::testGroupManipulation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 19
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 32
rs 9.6333
1
<?php
2
3
namespace SourcesTests\Files\Storage;
4
5
6
use CommonTestClass;
7
use kalanis\kw_auth\AuthException;
8
use kalanis\kw_auth\Data\FileGroup;
9
use kalanis\kw_auth\Sources\Files\Storage\Groups;
10
use kalanis\kw_locks\LockException;
11
use kalanis\kw_storage\Storage\Key\DefaultKey;
12
use kalanis\kw_storage\Storage\Storage;
13
use kalanis\kw_storage\Storage\Target\Memory;
14
use kalanis\kw_storage\StorageException;
15
16
17
class GroupsTest extends CommonTestClass
18
{
19
    protected $sourcePath = '.groups';
20
21
    /**
22
     * @throws AuthException
23
     * @throws LockException
24
     */
25
    public function testNotExistsData(): void
26
    {
27
        $lib = $this->emptyGroupSources();
28
        $this->assertNull($lib->getGroupDataOnly(15));
29
    }
30
31
    /**
32
     * @throws AuthException
33
     * @throws LockException
34
     */
35
    public function testCreateGroupOnEmptyInstance(): void
36
    {
37
        $lib = $this->emptyGroupSources();
38
        $group = $this->wantedGroup();
39
40
        // create
41
        $lib->createGroup($group);
42
        // check data
43
        $saved = $lib->getGroupDataOnly(1);
44
        $this->assertEquals('another', $saved->getGroupName());
45
        $this->assertEquals('Testing group', $saved->getGroupDesc());
46
        $this->assertEquals(1001, $saved->getGroupAuthorId());
47
    }
48
49
    /**
50
     * @throws AuthException
51
     * @throws LockException
52
     */
53
    public function testUpdateGroupOnEmptyInstance(): void
54
    {
55
        $lib = $this->emptyGroupSources();
56
        $group = $this->wantedGroup();
57
58
        // update
59
        $this->expectException(AuthException::class);
60
        $lib->updateGroup($group);
61
    }
62
63
    /**
64
     * @throws AuthException
65
     * @throws LockException
66
     * @throws StorageException
67
     */
68
    public function testGroupManipulation(): void
69
    {
70
        $lib = $this->groupSources();
71
        $group = $this->wantedGroup();
72
73
        // create
74
        $lib->createGroup($group);
75
        // check data
76
        $saved = $lib->getGroupDataOnly(3);
77
        $this->assertEquals('another', $saved->getGroupName());
78
        $this->assertEquals('Testing group', $saved->getGroupDesc());
79
        $this->assertEquals(1001, $saved->getGroupAuthorId());
80
81
        // update
82
        $group->setData(
83
            $group->getGroupId(),
84
            $group->getGroupName(),
85
            1002,
86
            'WheĐn yoĐu dđo nođt knđow',
87
            999
88
        );
89
        $lib->updateGroup($group);
90
91
        // check data - again with new values
92
        $saved = $lib->getGroupDataOnly(3);
93
        $this->assertEquals('When you do not know', $saved->getGroupDesc()); // overwrite this
94
        $this->assertEquals(1001, $saved->getGroupAuthorId()); // cannot overwrite this
95
96
        // remove
97
        $lib->deleteGroup($group->getGroupId());
98
        // check for existence
99
        $this->assertEmpty($lib->getGroupDataOnly(3));
100
    }
101
102
    /**
103
     * @throws AuthException
104
     * @throws LockException
105
     * @throws StorageException
106
     */
107
    public function testCreateFail(): void
108
    {
109
        $lib = $this->groupSources();
110
        $group = $this->wantedGroup('');
111
        $this->expectException(AuthException::class);
112
        $lib->createGroup($group);
113
    }
114
115
    /**
116
     * @throws AuthException
117
     * @throws LockException
118
     * @throws StorageException
119
     */
120
    public function testAllGroups(): void
121
    {
122
        $lib = $this->groupSources();
123
        $data = $lib->readGroup();
124
        $this->assertEquals('Maintainers', $data[0]->getGroupDesc());
125
        $this->assertEquals(1000, $data[1]->getGroupAuthorId());
126
    }
127
128
    /**
129
     * @throws AuthException
130
     * @throws LockException
131
     */
132
    public function testRemoveGroupOnEmptyInstance(): void
133
    {
134
        $lib = $this->emptyGroupSources();
135
136
        // delete
137
        $lib->deleteGroup(41);
138
        $this->assertNull($lib->getGroupDataOnly(41));
139
    }
140
141
    /**
142
     * @throws AuthException
143
     * @throws LockException
144
     */
145
    public function testCreateStorageFail(): void
146
    {
147
        $lib = $this->failedGroupSources();
148
        $group = $this->wantedGroup('');
149
        $this->expectException(AuthException::class);
150
        $lib->createGroup($group);
151
    }
152
153
    /**
154
     * @throws AuthException
155
     * @throws LockException
156
     */
157
    public function testCreateStorageFailSave(): void
158
    {
159
        $lib = $this->failedGroupSources(true);
160
        $group = $this->wantedGroup('');
161
        $this->expectException(AuthException::class);
162
        $lib->createGroup($group);
163
    }
164
165
    /**
166
     * @throws AuthException
167
     * @throws LockException
168
     */
169
    public function testReadGroupsStorageFail(): void
170
    {
171
        $lib = $this->failedGroupSources();
172
        $this->expectException(AuthException::class);
173
        $lib->readGroup();
174
    }
175
176
    /**
177
     * @throws AuthException
178
     * @throws LockException
179
     */
180
    public function testUpdateGroupStorageFail(): void
181
    {
182
        $lib = $this->failedGroupSources();
183
        $this->expectException(AuthException::class);
184
        $lib->updateGroup($this->wantedGroup());
185
    }
186
187
    /**
188
     * @throws AuthException
189
     * @throws LockException
190
     */
191
    public function testUpdateGroupStorageFailSave(): void
192
    {
193
        $lib = $this->failedGroupSources(true);
194
        $this->expectException(AuthException::class);
195
        $lib->updateGroup($this->wantedGroup());
196
    }
197
198
    /**
199
     * @throws AuthException
200
     * @throws LockException
201
     */
202
    public function testRemoveGroupStorageFail(): void
203
    {
204
        $lib = $this->failedGroupSources();
205
        $this->assertFalse($lib->deleteGroup(41));
206
    }
207
208
    /**
209
     * @throws AuthException
210
     * @throws LockException
211
     */
212
    public function testRemoveGroupStorageFailSave(): void
213
    {
214
        $lib = $this->failedGroupSources(true, '0:all:1000:Main:1:' . "\r\n" . '2:folks:1000:Dirt:1:' . "\r\n");
215
        $this->expectException(AuthException::class);
216
        $lib->deleteGroup(2);
217
    }
218
219
    /**
220
     * Contains a full comedy/tragedy of work with locks
221
     * @throws LockException
222
     * @throws StorageException
223
     * @return Groups
224
     */
225
    protected function groupSources(): Groups
226
    {
227
        $storage = new Storage(new DefaultKey(), new Memory());
228
        $file = new Groups(
229
            $storage,
230
            $this->getLockPath(),
231
            $this->sourcePath
232
        );
233
        $storage->write($this->sourcePath,
234
            '0:root:1000:Maintainers:1:' . "\r\n"
235
            . '1:admin:1000:Administrators:1:' . "\r\n"
236
            . '# commented out' . "\r\n"
237
            . '2:user:1000:All users:1:' . "\r\n"
238
            // last line is intentionally empty one
239
        );
240
        return $file;
241
    }
242
243
    /**
244
     * @throws LockException
245
     * @return Groups
246
     */
247
    protected function emptyGroupSources(): Groups
248
    {
249
        return new Groups(
250
            new Storage(new DefaultKey(), new Memory()),
251
            $this->getLockPath(),
252
            $this->sourcePath
253
        );
254
    }
255
256
    /**
257
     * @param bool $canOpen
258
     * @param string $content
259
     * @throws LockException
260
     * @return Groups
261
     */
262
    protected function failedGroupSources(bool $canOpen = false, string $content = ''): Groups
263
    {
264
        return new Groups(
265
            new \XFailedStorage($canOpen, $content),
266
            $this->getLockPath(),
267
            $this->sourcePath
268
        );
269
    }
270
271
    protected function wantedGroup($name = 'another'): FileGroup
272
    {
273
        $user = new FileGroup();
274
        $user->setData(3, $name, 1001, 'Testing group', 888);
275
        return $user;
276
    }
277
}
278