1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MappersTests\File; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use CommonTestClass; |
7
|
|
|
use kalanis\kw_mapper\Interfaces\IEntryType; |
8
|
|
|
use kalanis\kw_mapper\MapperException; |
9
|
|
|
use kalanis\kw_mapper\Mappers; |
10
|
|
|
use kalanis\kw_mapper\Records\ASimpleRecord; |
11
|
|
|
use kalanis\kw_mapper\Records\PageRecord; |
12
|
|
|
use kalanis\kw_storage\Storage; |
13
|
|
|
use kalanis\kw_storage\StorageException; |
14
|
|
|
use Traversable; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class FileTest extends CommonTestClass |
18
|
|
|
{ |
19
|
|
|
public function tearDown(): void |
20
|
|
|
{ |
21
|
|
|
$path = $this->getTestFile(); |
22
|
|
|
if (is_file($path)) { |
23
|
|
|
@unlink($path); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function testContentOk(): void |
28
|
|
|
{ |
29
|
|
|
$path = $this->getTestFile(); |
30
|
|
|
$lib = new Mappers\File\PageContent(); |
31
|
|
|
$lib->setSource($path); |
32
|
|
|
$this->assertEquals($path, $lib->getAlias()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @throws MapperException |
37
|
|
|
*/ |
38
|
|
|
public function testCannotLoad(): void |
39
|
|
|
{ |
40
|
|
|
$rec = new PageRecord(); |
41
|
|
|
$rec->path = $this->getTestFile(); |
42
|
|
|
$rec->content = 'okmijnuhbzgvtfcrdxesy'; |
43
|
|
|
|
44
|
|
|
$lib = new XFailContent(); |
45
|
|
|
$this->expectException(MapperException::class); |
46
|
|
|
$this->expectExceptionMessage('Unable to read from source'); |
47
|
|
|
$lib->load($rec); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @throws MapperException |
52
|
|
|
*/ |
53
|
|
|
public function testCannotSave(): void |
54
|
|
|
{ |
55
|
|
|
$rec = new PageRecord(); |
56
|
|
|
$rec->path = $this->getTestFile(); |
57
|
|
|
$rec->content = 'okmijnuhbzgvtfcrdxesy'; |
58
|
|
|
|
59
|
|
|
$lib = new XFailContent(); |
60
|
|
|
$this->expectException(MapperException::class); |
61
|
|
|
$this->expectExceptionMessage('Unable to write into source'); |
62
|
|
|
$lib->save($rec); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @throws MapperException |
67
|
|
|
*/ |
68
|
|
|
public function testCannotDelete(): void |
69
|
|
|
{ |
70
|
|
|
$rec = new PageRecord(); |
71
|
|
|
$rec->path = $this->getTestFile(); |
72
|
|
|
$rec->content = 'okmijnuhbzgvtfcrdxesy'; |
73
|
|
|
|
74
|
|
|
$lib = new XFailContent(); |
75
|
|
|
$this->assertFalse($lib->delete($rec)); |
76
|
|
|
$rec->path = 'cannot_be_found'; |
77
|
|
|
$this->assertTrue($lib->delete($rec)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @throws MapperException |
82
|
|
|
*/ |
83
|
|
|
public function testCannotSearch(): void |
84
|
|
|
{ |
85
|
|
|
$rec = new KeyValueRecord(); |
86
|
|
|
$rec->key = $this->getTestFile(); |
87
|
|
|
$rec->content = 'okmijnuhbzgvtfcrdxesy'; |
88
|
|
|
|
89
|
|
|
$lib = new XFailKeyValue(); |
90
|
|
|
$result = $lib->loadMultiple($rec); |
91
|
|
|
$this->assertEmpty($result); |
92
|
|
|
$this->assertEquals([], $result); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @throws MapperException |
97
|
|
|
*/ |
98
|
|
|
public function testSearchDir(): void |
99
|
|
|
{ |
100
|
|
|
$rec = new KeyValueRecord(); |
101
|
|
|
$rec->key = $this->getTestDir(); |
102
|
|
|
$rec->content = ''; |
103
|
|
|
|
104
|
|
|
$lib = new Mappers\File\KeyValue(); |
105
|
|
|
$result = $lib->loadMultiple($rec); |
106
|
|
|
$this->assertNotEmpty($result); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function getTestFile(): string |
110
|
|
|
{ |
111
|
|
|
return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'fileTest.txt'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected function getTestDir(): string |
115
|
|
|
{ |
116
|
|
|
return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'target' . DIRECTORY_SEPARATOR; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
class XFailContent extends Mappers\File\PageContent |
122
|
|
|
{ |
123
|
|
|
public function getStorage(): Storage\Storage |
124
|
|
|
{ |
125
|
|
|
return new XFailStorage( |
126
|
|
|
new Storage\Target\Volume(), |
127
|
|
|
new Storage\Format\Raw(), |
128
|
|
|
new Storage\Key\DefaultKey() |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
class XFailKeyValue extends Mappers\File\KeyValue |
135
|
|
|
{ |
136
|
|
|
public function getStorage(): Storage\Storage |
137
|
|
|
{ |
138
|
|
|
return new XFailStorage( |
139
|
|
|
new Storage\Target\Volume(), |
140
|
|
|
new Storage\Format\Raw(), |
141
|
|
|
new Storage\Key\DefaultKey() |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
class XFailStorage extends Storage\Storage |
148
|
|
|
{ |
149
|
|
|
public function read(string $sharedKey) |
150
|
|
|
{ |
151
|
|
|
throw new StorageException('XFail mock fail read'); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function write(string $sharedKey, $data, ?int $timeout = null): bool |
155
|
|
|
{ |
156
|
|
|
throw new StorageException('XFail mock fail write'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function remove(string $sharedKey): bool |
160
|
|
|
{ |
161
|
|
|
throw new StorageException('XFail mock fail write'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function lookup(string $mask): Traversable |
165
|
|
|
{ |
166
|
|
|
throw new StorageException('XFail mock fail lookup'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function exists(string $sharedKey): bool |
170
|
|
|
{ |
171
|
|
|
return ('cannot_be_found' != $sharedKey); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Class KeyValueRecord |
178
|
|
|
* @package MappersTests\File |
179
|
|
|
* @property string key |
180
|
|
|
* @property string content |
181
|
|
|
*/ |
182
|
|
|
class KeyValueRecord extends ASimpleRecord |
183
|
|
|
{ |
184
|
|
|
protected function addEntries(): void |
185
|
|
|
{ |
186
|
|
|
$this->addEntry('key', IEntryType::TYPE_STRING, 512); |
187
|
|
|
$this->addEntry('content', IEntryType::TYPE_STRING, PHP_INT_MAX); |
188
|
|
|
$this->setMapper('\MappersTests\File\KeyValueMapper'); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
class KeyValueMapper extends Mappers\File\PageContent |
194
|
|
|
{ |
195
|
|
|
protected function setMap(): void |
196
|
|
|
{ |
197
|
|
|
$this->setPathKey('key'); |
198
|
|
|
$this->setContentKey('content'); |
199
|
|
|
$this->setFormat('\kalanis\kw_mapper\Storage\File\Formats\SinglePage'); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|