Passed
Push — master ( 3daa2d...2c2b7e )
by Petr
07:39
created

xUserDir::shortFullPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace BasicTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_files\FilesException;
8
use kalanis\kw_files\Processing\Storage as files;
9
use kalanis\kw_paths\ArrayPath;
10
use kalanis\kw_paths\Interfaces\IPaths;
11
use kalanis\kw_paths\PathsException;
12
use kalanis\kw_storage\Storage as store;
13
use kalanis\kw_user_paths\Actions;
14
use kalanis\kw_user_paths\UserDir;
15
16
17
class ActionsTest extends CommonTestClass
18
{
19
    /**
20
     * @throws FilesException
21
     * @throws PathsException
22
     */
23
    public function testCreateInvalid1(): void
24
    {
25
        $lib = $this->getActionsWithoutPaths('/for_test');
26
        $this->expectException(PathsException::class);
27
        $lib->createTree();
28
    }
29
30
    /**
31
     * @throws FilesException
32
     * @throws PathsException
33
     */
34
    public function testCreateInvalid2(): void
35
    {
36
        $lib = $this->getActions('for_test');
37
        $this->expectException(PathsException::class);
38
        $lib->createTree();
39
    }
40
41
    /**
42
     * @throws FilesException
43
     * @throws PathsException
44
     */
45
    public function testCreate(): void
46
    {
47
        $lib = $this->getActions('/for_test');
48
        $lib->createTree();
49
        $this->assertTrue($lib->wipeConfDirs());
50
        $this->assertTrue($lib->wipeHomeDir());
51
    }
52
53
    /**
54
     * @throws FilesException
55
     * @throws PathsException
56
     */
57
    public function testWipeWorkDir(): void
58
    {
59
        $lib = $this->getActions('prepared');
60
        $this->assertTrue($lib->wipeWorkDir());
61
    }
62
63
    /**
64
     * @throws FilesException
65
     * @throws PathsException
66
     */
67
    public function testWipeWorkDir2(): void
68
    {
69
        $lib = $this->getActions('prepared/');
70
        $this->assertTrue($lib->wipeWorkDir());
71
    }
72
73
    /**
74
     * @throws FilesException
75
     * @throws PathsException
76
     */
77
    public function testWipeWorkDirInvalid1(): void
78
    {
79
        $lib = $this->getActionsWithoutPaths(null);
80
        $this->expectException(PathsException::class);
81
        $lib->wipeWorkDir();
82
    }
83
84
    /**
85
     * @throws FilesException
86
     * @throws PathsException
87
     */
88
    public function testWipeWorkDirInvalid2(): void
89
    {
90
        $lib = $this->getActions('/d/');
91
        $this->assertFalse($lib->wipeWorkDir());
92
    }
93
94
    /**
95
     * @throws FilesException
96
     * @throws PathsException
97
     */
98
    public function testWipeConfDir(): void
99
    {
100
        $lib = $this->getActions('prepared');
101
        $this->assertTrue($lib->wipeConfDirs());
102
    }
103
104
    /**
105
     * @throws FilesException
106
     * @throws PathsException
107
     */
108
    public function testWipeConfDirInvalid1(): void
109
    {
110
        $lib = $this->getActionsWithoutPaths(null);
111
        $this->expectException(PathsException::class);
112
        $lib->wipeConfDirs();
113
    }
114
115
    /**
116
     * @throws FilesException
117
     * @throws PathsException
118
     */
119
    public function testWipeConfDirInvalid2(): void
120
    {
121
        $lib = $this->getActionsWithShortPaths('/d');
122
        $this->assertFalse($lib->wipeConfDirs());
123
    }
124
125
    /**
126
     * @throws FilesException
127
     * @throws PathsException
128
     */
129
    public function testWipeConfDirInvalid3(): void
130
    {
131
        $lib = $this->getActions('/dummy/');
132
        $this->assertFalse($lib->wipeConfDirs());
133
    }
134
135
    /**
136
     * @throws FilesException
137
     * @throws PathsException
138
     */
139
    public function testWipeHomeDir(): void
140
    {
141
        $lib = $this->getActions('prepared');
142
        $this->assertTrue($lib->wipeHomeDir());
143
    }
144
145
    /**
146
     * @throws FilesException
147
     * @throws PathsException
148
     */
149
    public function testWipeHomeDirInvalid1(): void
150
    {
151
        $lib = $this->getActionsWithoutPaths(null);
152
        $this->expectException(PathsException::class);
153
        $lib->wipeHomeDir();
154
    }
155
156
    /**
157
     * @throws FilesException
158
     * @throws PathsException
159
     */
160
    public function testWipeHomeDirInvalid2(): void
161
    {
162
        $lib = $this->getActions('/d/');
163
        $this->assertFalse($lib->wipeHomeDir());
164
    }
165
166
    /**
167
     * @param string|null $path
168
     * @throws FilesException
169
     * @throws PathsException
170
     * @return Actions
171
     */
172
    protected function getActions(?string $path): Actions
173
    {
174
        $storage = $this->getStorage();
175
        return new Actions(
176
            $this->getUserDir($path),
177
            new files\ProcessNode($storage),
178
            new files\ProcessDir($storage)
179
        );
180
    }
181
182
    /**
183
     * @param string|null $path
184
     * @throws FilesException
185
     * @throws PathsException
186
     * @return Actions
187
     */
188
    protected function getActionsWithoutPaths(?string $path): Actions
189
    {
190
        $userDir = $this->getUserDir($path);
191
        $userDir->emptyFullPath();
192
        $storage = $this->getStorage();
193
        return new Actions(
194
            $userDir,
195
            new files\ProcessNode($storage),
196
            new files\ProcessDir($storage)
197
        );
198
    }
199
200
    /**
201
     * @param string|null $path
202
     * @throws FilesException
203
     * @return Actions
204
     */
205
    protected function getActionsWithShortPaths(?string $path): Actions
206
    {
207
        $userDir = $this->getUserDir($path);
208
        $userDir->shortFullPath();
209
        $storage = $this->getStorage();
210
        return new Actions(
211
            $userDir,
212
            new files\ProcessNode($storage),
213
            new files\ProcessDir($storage)
214
        );
215
    }
216
217
    /**
218
     * @param string|null $path
219
     * @throws PathsException
220
     * @return xUserDir
221
     */
222
    protected function getUserDir(?string $path): xUserDir
223
    {
224
        $userDir = new xUserDir();
225
        $userDir->setUserPath($path);
226
        $userDir->process();
227
        return $userDir;
228
    }
229
230
    /**
231
     * @throws FilesException
232
     * @throws PathsException
233
     * @return store\Storage
234
     */
235
    protected function getStorage(): store\Storage
236
    {
237
        $storage = new store\Storage(new store\Key\DefaultKey(), new store\Target\Memory());
238
        $this->mockStorageWithContent($storage);
239
        return $storage;
240
    }
241
242
    /**
243
     * @param store\Storage $storage
244
     * @throws FilesException
245
     * @throws PathsException
246
     */
247
    protected function mockStorageWithContent(store\Storage $storage): void
248
    {
249
        // hack - some predefined content in storage
250
        $dirs = new files\ProcessDir($storage);
251
        // testing data tree structure for accessing user content
252
        $dirs->createDir([IPaths::DIR_USER, 'prepared', IPaths::DIR_DATA], true);
253
        $dirs->createDir([IPaths::DIR_USER, 'prepared', IPaths::DIR_CONF], true);
254
        $dirs->createDir([IPaths::DIR_USER, 'prepared', IPaths::DIR_STYLE], true);
255
        $files = new files\ProcessFile($storage);
256
        $files->saveFile([IPaths::DIR_USER, 'prepared', IPaths::DIR_STYLE, 'moar'], 'content');
257
        // this one is not a directory, it's file and cannot be re-created as dir
258
        $files->saveFile([IPaths::DIR_USER, 'for_test'], 'okmjinuhb');
259
    }
260
}
261
262
263
class xUserDir extends UserDir
264
{
265
    /**
266
     * Hack for undefined user name
267
     * @return string
268
     */
269
    protected function makeFromUserName(): string
270
    {
271
        return '';
272
    }
273
274
    public function emptyFullPath(): void
275
    {
276
        $this->fullPath = new ArrayPath();
277
    }
278
279
    public function shortFullPath(): void
280
    {
281
        $this->fullPath = new ArrayPath();
282
        $this->fullPath->setArray(['d']);
283
    }
284
}
285