Test Failed
Push — master ( 50dc03...2f2ddd )
by Petr
02:45
created

NoBeforeMapper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 16
c 1
b 0
f 0
dl 0
loc 71
rs 10
1
<?php
2
3
namespace RecordsTests;
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\ARecord;
11
use kalanis\kw_mapper\Records\ASimpleRecord;
12
13
14
class MapperTest extends CommonTestClass
15
{
16
    public function setUp(): void
17
    {
18
        if (is_file($this->mockFile())) {
19
            chmod($this->mockFile(), 0555);
20
            unlink($this->mockFile());
21
        }
22
23
        parent::setUp();
24
    }
25
26
    public function tearDown(): void
27
    {
28
        if (is_file($this->mockFile())) {
29
            chmod($this->mockFile(), 0555);
30
            unlink($this->mockFile());
31
        }
32
        parent::tearDown();
33
    }
34
35
    /**
36
     * @throws MapperException
37
     */
38
    public function testFactory(): void
39
    {
40
        $data = new Mappers\Factory();
41
        $instance = $data->getInstance('\kalanis\kw_mapper\Mappers\File\PageContent');
42
        $this->assertInstanceOf('\kalanis\kw_mapper\Mappers\AMapper', $instance);
43
    }
44
45
    /**
46
     * @throws MapperException
47
     */
48
    public function testFactoryFail1(): void
49
    {
50
        $data = new Mappers\Factory();
51
        $this->expectException(MapperException::class);
52
        $data->getInstance('no class');
53
    }
54
55
    /**
56
     * @throws MapperException
57
     */
58
    public function testFactoryFail2(): void
59
    {
60
        $data = new Mappers\Factory();
61
        $this->expectException(MapperException::class);
62
        $data->getInstance('\stdClass');
63
    }
64
65
    /**
66
     * @throws MapperException
67
     */
68
    public function testSimple(): void
69
    {
70
        $data = new RecordForMapper();
71
        $data->setExternalEntries();
72
        $data->path = $this->mockFile();
73
        $data->content = 'yxcvbnmasdfghjklqwertzuiop';
74
        $this->assertTrue($data->save());
75
        $this->assertTrue($data->load());
76
        $this->assertTrue($data->getEntry('content')->isFromStorage());
77
        $multi = $data->loadMultiple();
78
        $this->assertEquals(1, count($multi));
79
    }
80
81
    /**
82
     * @throws MapperException
83
     */
84
    public function testUnavailable(): void
85
    {
86
        $data = new RecordForMapper();
87
        $this->expectException(MapperException::class);
88
        $data->load();
89
    }
90
91
    protected function mockFile(): string
92
    {
93
        return implode(DIRECTORY_SEPARATOR, [__DIR__, '..', 'data', 'testing_one.txt']);
94
    }
95
96
    /**
97
     * @throws MapperException
98
     */
99
    public function testNoBefore(): void
100
    {
101
        $data = new RecordForMapper();
102
        $data->setExternalEntries();
103
        $data->path = 'qwertzuiop';
104
        $data->content = 'asdfghjkl';
105
106
        $mapper = new NoBeforeMapper();
107
        $this->assertFalse($mapper->insert($data));
108
        $this->assertFalse($mapper->updateSome($data));
109
        $this->assertFalse($mapper->save($data));
110
        $this->assertFalse($mapper->load($data));
111
        $this->assertFalse($mapper->delete($data));
112
    }
113
114
    /**
115
     * @throws MapperException
116
     */
117
    public function testNoDuring(): void
118
    {
119
        $data = new RecordForMapper();
120
        $data->setExternalEntries();
121
        $data->path = 'qwertzuiop';
122
        $data->content = 'asdfghjkl';
123
124
        $mapper = new NoDuringMapper();
125
        $this->assertFalse($mapper->insert($data));
126
        $this->assertFalse($mapper->updateSome($data));
127
        $this->assertFalse($mapper->save($data));
128
        $this->assertFalse($mapper->load($data));
129
        $this->assertFalse($mapper->delete($data));
130
    }
131
132
    /**
133
     * @throws MapperException
134
     */
135
    public function testNoAfter(): void
136
    {
137
        $data = new RecordForMapper();
138
        $data->setExternalEntries();
139
        $data->path = 'qwertzuiop';
140
        $data->content = 'asdfghjkl';
141
142
        $mapper = new NoAfterMapper();
143
        $this->assertFalse($mapper->insert($data));
144
        $this->assertFalse($mapper->updateSome($data));
145
        $this->assertFalse($mapper->save($data));
146
        $this->assertFalse($mapper->load($data));
147
        $this->assertFalse($mapper->delete($data));
148
    }
149
}
150
151
152
/**
153
 * Class RecordForMapper
154
 * @package RecordsTests
155
 * @property string path
156
 * @property string content
157
 */
158
class RecordForMapper extends ASimpleRecord
159
{
160
    protected function addEntries(): void
161
    {}
162
163
    public function setExternalEntries(): void
164
    {
165
        $this->addEntry('path', IEntryType::TYPE_STRING, 512);
166
        $this->addEntry('content', IEntryType::TYPE_STRING, PHP_INT_MAX);
167
        $this->setMapper('\kalanis\kw_mapper\Mappers\File\PageContent');
168
    }
169
}
170
171
172
class NoBeforeMapper extends Mappers\AMapper
173
{
174
    protected function setMap(): void
175
    {
176
        $this->setRelation('path', 0);
177
        $this->addPrimaryKey('path');
178
    }
179
180
    public function getAlias(): string
181
    {
182
        return 'dummy';
183
    }
184
185
    public function updateSome(ARecord $record): bool
186
    {
187
        return $this->update($record);
188
    }
189
190
    public function countRecord(ARecord $record): int
191
    {
192
        return 0;
193
    }
194
195
    public function loadMultiple(ARecord $record): array
196
    {
197
        return [];
198
    }
199
200
    protected function insertRecord(ARecord $record): bool
201
    {
202
        return true;
203
    }
204
205
    protected function updateRecord(ARecord $record): bool
206
    {
207
        return true;
208
    }
209
210
    protected function loadRecord(ARecord $record): bool
211
    {
212
        return true;
213
    }
214
215
    protected function deleteRecord(ARecord $record): bool
216
    {
217
        return true;
218
    }
219
220
    protected function beforeSave(ARecord $record): bool
221
    {
222
        return false;
223
    }
224
225
    protected function beforeDelete(ARecord $record): bool
226
    {
227
        return false;
228
    }
229
230
    protected function beforeUpdate(ARecord $record): bool
231
    {
232
        return false;
233
    }
234
235
    protected function beforeInsert(ARecord $record): bool
236
    {
237
        return false;
238
    }
239
240
    protected function beforeLoad(ARecord $record): bool
241
    {
242
        return false;
243
    }
244
}
245
246
247
class NoDuringMapper extends Mappers\AMapper
248
{
249
    protected function setMap(): void
250
    {
251
        $this->setRelation('path', 0);
252
        $this->addPrimaryKey('path');
253
    }
254
255
    public function getAlias(): string
256
    {
257
        return 'dummy';
258
    }
259
260
    public function updateSome(ARecord $record): bool
261
    {
262
        return $this->update($record);
263
    }
264
265
    public function countRecord(ARecord $record): int
266
    {
267
        return 1;
268
    }
269
270
    public function loadMultiple(ARecord $record): array
271
    {
272
        return [];
273
    }
274
275
    protected function insertRecord(ARecord $record): bool
276
    {
277
        return false;
278
    }
279
280
    protected function updateRecord(ARecord $record): bool
281
    {
282
        return false;
283
    }
284
285
    protected function loadRecord(ARecord $record): bool
286
    {
287
        return false;
288
    }
289
290
    protected function deleteRecord(ARecord $record): bool
291
    {
292
        return false;
293
    }
294
}
295
296
297
class NoAfterMapper extends Mappers\AMapper
298
{
299
    protected function setMap(): void
300
    {
301
        $this->setRelation('path', 0);
302
        $this->addPrimaryKey('path');
303
    }
304
305
    public function getAlias(): string
306
    {
307
        return 'dummy';
308
    }
309
310
    public function updateSome(ARecord $record): bool
311
    {
312
        return $this->update($record);
313
    }
314
315
    public function countRecord(ARecord $record): int
316
    {
317
        return 0;
318
    }
319
320
    public function loadMultiple(ARecord $record): array
321
    {
322
        return [];
323
    }
324
325
    protected function insertRecord(ARecord $record): bool
326
    {
327
        return true;
328
    }
329
330
    protected function updateRecord(ARecord $record): bool
331
    {
332
        return true;
333
    }
334
335
    protected function loadRecord(ARecord $record): bool
336
    {
337
        return true;
338
    }
339
340
    protected function deleteRecord(ARecord $record): bool
341
    {
342
        return true;
343
    }
344
345
    protected function afterSave(ARecord $record): bool
346
    {
347
        return false;
348
    }
349
350
    protected function afterDelete(ARecord $record): bool
351
    {
352
        return false;
353
    }
354
355
    protected function afterUpdate(ARecord $record): bool
356
    {
357
        return false;
358
    }
359
360
    protected function afterInsert(ARecord $record): bool
361
    {
362
        return false;
363
    }
364
365
    protected function afterLoad(ARecord $record): bool
366
    {
367
        return false;
368
    }
369
}
370