Passed
Push — master ( 2b14a7...fa4f50 )
by Petr
02:38
created

AStorageTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 10
1
<?php
2
3
namespace StorageBasicTests;
4
5
6
use CommonTestClass;
7
use kalanis\kw_files\Interfaces\IProcessDirs;
8
use kalanis\kw_files\Interfaces\IProcessFiles;
9
use kalanis\kw_files\Interfaces\IProcessNodes;
10
use kalanis\kw_files\Processing\Storage\ProcessDir;
11
use kalanis\kw_files\Processing\Storage\ProcessFile;
12
use kalanis\kw_files\Processing\Storage\ProcessNode;
13
use kalanis\kw_storage\Interfaces\ITarget;
14
use kalanis\kw_storage\Storage\Key\DirKey;
15
use kalanis\kw_storage\Storage\Storage;
16
use kalanis\kw_storage\Storage\Target\Memory;
17
use kalanis\kw_storage\StorageException;
18
use Traversable;
19
20
21
abstract class AStorageTest extends CommonTestClass
22
{
23
    protected function getNodeLib(): IProcessNodes
24
    {
25
        DirKey::setDir($this->getTestPath());
26
        return new ProcessNode(new Storage(new DirKey(), $this->filledMemory()));
27
    }
28
29
    protected function getNodeFailLib(): IProcessNodes
30
    {
31
        DirKey::setDir($this->getTestPath());
32
        return new ProcessNode(new XFailStorage(new DirKey(), new Memory()));
33
    }
34
35
    protected function getFileLib(): IProcessFiles
36
    {
37
        DirKey::setDir($this->getTestPath());
38
        return new ProcessFile(new Storage(new DirKey(), $this->filledMemory()));
39
    }
40
41
    protected function getFileFailLib(): IProcessFiles
42
    {
43
        DirKey::setDir($this->getTestPath());
44
        return new ProcessFile(new XFailStorage(new DirKey(), new Memory()));
45
    }
46
47
    protected function getDirLib(): IProcessDirs
48
    {
49
        DirKey::setDir($this->getTestPath());
50
        return new ProcessDir(new Storage(new DirKey(), $this->filledMemory()));
51
    }
52
53
    protected function getDirFailLib(): IProcessDirs
54
    {
55
        DirKey::setDir($this->getTestPath());
56
        return new ProcessDir(new XFailStorage(new DirKey(), new Memory()));
57
    }
58
59
    protected function getDirTreeLib(): IProcessDirs
60
    {
61
        DirKey::setDir('');
62
        return new ProcessDir(new Storage(new DirKey(), $this->filledMemory()));
63
    }
64
65
    protected function getStorageLib(): Storage
66
    {
67
        DirKey::setDir('');
68
        return new Storage(new DirKey(), new Memory());
69
    }
70
71
    protected function getTestPath(): string
72
    {
73
        return DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree';
74
    }
75
76
    protected function filledMemory(): ITarget
77
    {
78
        $res = fopen('php://memory', 'r+');
79
        fwrite($res, 'qwertzuiopasdfghjklyxcvbnm0123456789');
80
        $lib = new Memory();
81
        $lib->save('', IProcessNodes::STORAGE_NODE_KEY); // root has empty file name - his name is defined by its mountpoint
82
        $lib->save('' . DIRECTORY_SEPARATOR . 'data', IProcessNodes::STORAGE_NODE_KEY);
83
        $lib->save('' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree', IProcessNodes::STORAGE_NODE_KEY);
84
        $lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'last_one', IProcessNodes::STORAGE_NODE_KEY);
85
        $lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'last_one' . DIRECTORY_SEPARATOR . '.gitkeep', '');
86
        $lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'next_one', IProcessNodes::STORAGE_NODE_KEY);
87
        $lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'next_one' . DIRECTORY_SEPARATOR . 'sub_one', IProcessNodes::STORAGE_NODE_KEY);
88
        $lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'next_one' . DIRECTORY_SEPARATOR . 'sub_one' . DIRECTORY_SEPARATOR . '.gitkeep', '');
89
        $lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'sub', IProcessNodes::STORAGE_NODE_KEY);
90
        $lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'sub' . DIRECTORY_SEPARATOR . 'dummy3.txt', 'qwertzuiopasdfghjklyxcvbnm0123456789');
91
        $lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'sub' . DIRECTORY_SEPARATOR . 'dummy4.txt', false); // intentionally!!!
92
        $lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'dummy1.txt', 'qwertzuiopasdfghjklyxcvbnm0123456789');
93
        $lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'dummy2.txt', 'qwertzuiopasdfghjklyxcvbnm0123456789');
94
        $lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'other1.txt', $res);
95
        $lib->save(DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tree' . DIRECTORY_SEPARATOR . 'other2.txt', 'qwertzuiopasdfghjklyxcvbnm0123456789');
96
        return $lib;
97
    }
98
}
99
100
101
class XFailStorage extends Storage
102
{
103
    public function write(string $sharedKey, $data, ?int $timeout = null): bool
104
    {
105
        throw new StorageException('mock');
106
    }
107
108
    public function read(string $sharedKey)
109
    {
110
        throw new StorageException('mock');
111
    }
112
113
    public function remove(string $sharedKey): bool
114
    {
115
        throw new StorageException('mock');
116
    }
117
118
    public function exists(string $sharedKey): bool
119
    {
120
        throw new StorageException('mock');
121
    }
122
123
    public function lookup(string $mask): Traversable
124
    {
125
        throw new StorageException('mock');
126
    }
127
128
    public function increment(string $key): bool
129
    {
130
        throw new StorageException('mock');
131
    }
132
133
    public function decrement(string $key): bool
134
    {
135
        throw new StorageException('mock');
136
    }
137
138
    public function removeMulti(array $keys): array
139
    {
140
        throw new StorageException('mock');
141
    }
142
}
143