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

TableTest::testOperationsNoPk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 43
rs 9.424
cc 1
nc 1
nop 0
1
<?php
2
3
namespace MappersTests\File;
4
5
6
use CommonTestClass;
7
use kalanis\kw_mapper\MapperException;
8
use kalanis\kw_mapper\Records\ARecord;
9
use kalanis\kw_mapper\Storage;
10
11
12
/**
13
 * Class TableTest
14
 * @package MappersTests\File
15
 * Need numeric PK and without PK
16
 * Need some with different entries loaded
17
 */
18
class TableTest extends CommonTestClass
19
{
20
    public function tearDown(): void
21
    {
22
        $path = $this->getTestFile();
23
        if (is_file($path)) {
24
            @unlink($path);
25
        }
26
    }
27
28
    /**
29
     * @throws MapperException
30
     */
31
    public function testMulti(): void
32
    {
33
        $data = new \TableRecord();
34
        $this->assertEquals(5, count($data->loadMultiple()));
35
36
        $data->sub = true;
37
        $this->assertEquals(3, $data->count());
38
    }
39
40
    /**
41
     * @throws MapperException
42
     */
43
    public function testSingle(): void
44
    {
45
        $data = new \TableRecord();
46
        $data->file = 'nonexistent';
47
        $this->assertFalse($data->load());
48
        $this->assertEquals(null, $data->title);
49
50
        $data->file = 'dummy2.htm';
51
        $this->assertTrue($data->load());
52
        $this->assertEquals('ghi', $data->title);
53
    }
54
55
    /**
56
     * @throws MapperException
57
     */
58
    public function testOperationsOnMeta(): void
59
    {
60
        $data = new \TableRecord();
61
        $this->initSource($data);
62
        $data->getMapper()->orderFromFirst(true);
63
64
        // clear insert
65
        $data->file = 'another';
66
        $data->title = 'file';
67
        $data->desc = 'to load';
68
        $data->order = 11;
69
        $data->sub = false;
70
        $this->assertTrue($data->save(true)); // insert with PK - must use force
71
72
        // find by PK
73
        $data = new \TableRecord();
74
        $this->assertEquals($this->getTestFile(), $data->getMapper()->getSource()); // just check between set
75
76
        $data->file = 'another';
77
        $this->assertTrue($data->load());
78
        $this->assertEquals('to load', $data->desc);
79
80
        // update by PK
81
        $data = new \TableRecord();
82
        $data->file = 'another';
83
        $this->assertTrue($data->load());
84
        $data->title = 'experiment';
85
        $this->assertTrue($data->save());
86
87
        $data = new \TableRecord();
88
        $data->file = 'another';
89
        $this->assertTrue($data->load());
90
        $this->assertEquals('experiment', $data->title);
91
92
        // delete by PK
93
        $data = new \TableRecord();
94
        $data->file = 'another';
95
        $this->assertTrue($data->delete());
96
    }
97
98
    /**
99
     * @throws MapperException
100
     */
101
    public function testOperationsOnPk(): void
102
    {
103
        $data = new \TableIdRecord();
104
        $data->useIdAsMapper();
105
        $this->initSource($data, $this->getSourceFileClassic());
106
        $data->getMapper()->orderFromFirst(true);
107
108
        // clear insert
109
        $data->file = 'another';
110
        $data->title = 'file';
111
        $data->desc = 'to load';
112
        $data->enabled = false;
113
        $this->assertTrue($data->save()); // insert without PK into table with PK
114
115
        // find by PK
116
        $data = new \TableIdRecord();
117
        $data->useIdAsMapper();
118
        $this->assertEquals($this->getTestFile(), $data->getMapper()->getSource()); // just check between set
119
120
        $data->id = 6;
121
        $this->assertTrue($data->load());
122
        $this->assertEquals('to load', $data->desc);
123
124
        // update by PK
125
        $data = new \TableIdRecord();
126
        $data->useIdAsMapper();
127
        $data->id = 6;
128
        $this->assertTrue($data->load());
129
        $data->title = 'experiment';
130
        $this->assertTrue($data->save());
131
132
        $data = new \TableIdRecord();
133
        $data->useIdAsMapper();
134
        $data->id = 6;
135
        $this->assertTrue($data->load());
136
        $this->assertEquals('experiment', $data->title);
137
138
        // delete by PK
139
        $data = new \TableIdRecord();
140
        $data->useIdAsMapper();
141
        $data->id = 6;
142
        $this->assertTrue($data->delete());
143
    }
144
145
    /**
146
     * @throws MapperException
147
     */
148
    public function testOperationsNoPk(): void
149
    {
150
        $data = new \TableIdRecord();
151
        $data->useNoKeyMapper();
152
        $this->initSource($data, $this->getSourceFileClassic());
153
        $data->getMapper()->orderFromFirst(false);
154
155
        // clear insert
156
        $data->id = 6;
157
        $data->file = 'another';
158
        $data->title = 'file';
159
        $data->desc = 'to load';
160
        $data->enabled = false;
161
        $this->assertTrue($data->save(true)); // insert into table without PK - you must set own id
162
163
        // find by PK
164
        $data = new \TableIdRecord();
165
        $data->useNoKeyMapper();
166
        $this->assertEquals($this->getTestFile(), $data->getMapper()->getSource()); // just check between set
167
168
        $data->id = 6;
169
        $this->assertTrue($data->load());
170
        $this->assertEquals('to load', $data->desc);
171
172
        // update by PK
173
        $data = new \TableIdRecord();
174
        $data->useNoKeyMapper();
175
        $data->id = 6;
176
        $this->assertTrue($data->load());
177
        $data->title = 'experiment';
178
        $this->assertTrue($data->save());
179
180
        $data = new \TableIdRecord();
181
        $data->useNoKeyMapper();
182
        $data->id = 6;
183
        $this->assertTrue($data->load());
184
        $this->assertEquals('experiment', $data->title);
185
186
        // delete by PK
187
        $data = new \TableIdRecord();
188
        $data->useNoKeyMapper();
189
        $data->id = 6;
190
        $this->assertTrue($data->delete());
191
    }
192
193
    /**
194
     * @throws MapperException
195
     */
196
    public function testNonExistent(): void
197
    {
198
        $data = new \TableRecord();
199
        $this->initSource($data);
200
201
        // clear insert
202
        $data->file = 'another';
203
        $data->title = 'file';
204
        $data->desc = 'to load';
205
        $data->order = 11;
206
        $data->sub = false;
207
        $this->assertTrue($data->save()); // insert with PK - no force, not preset, just insert
208
209
        // forced exists insert - false
210
        $data = new \TableRecord();
211
        $data->file = 'unknown.htm';
212
        $data->title = 'file';
213
        $data->desc = 'to load';
214
        $data->order = 11;
215
        $data->sub = false;
216
        $this->assertFalse($data->save(true)); // insert with PK - use force, exists but cannot update
217
218
        // update by PK
219
        $data = new \TableRecord();
220
        $data->file = 'unknown';
221
        $this->assertFalse($data->load());
222
223
        // delete by PK
224
        $data = new \TableRecord();
225
        $data->file = 'unknown';
226
        $this->assertFalse($data->delete());
227
    }
228
229
    /**
230
     * @throws MapperException
231
     */
232
    public function testBefore(): void
233
    {
234
        $data = new TableBefore();
235
        $this->initSource($data);
236
237
        $data->file = 'another';
238
        $data->title = 'file';
239
        $data->desc = 'to load';
240
        $data->order = 11;
241
        $data->sub = false;
242
        $this->assertFalse($data->save()); // save with failing before
243
244
        $data = new TableBefore();
245
        $data->file = 'dummy2.htm';
246
        $this->assertFalse($data->load()); // load with failing before
247
    }
248
249
    /**
250
     * @throws MapperException
251
     */
252
    public function testAfter(): void
253
    {
254
        $data = new TableAfter();
255
        $this->initSource($data);
256
257
        $data->file = 'another';
258
        $data->title = 'file';
259
        $data->desc = 'to load';
260
        $data->order = 11;
261
        $data->sub = false;
262
        $this->assertFalse($data->save()); // save with failing after
263
264
        $data = new TableAfter();
265
        $data->file = 'dummy2.htm';
266
        $this->assertFalse($data->load()); // load with failing after
267
    }
268
269
    protected function initSource(ARecord $record, string $source = ''): void
270
    {
271
        $source = empty($source) ? $this->getSourceFileMeta() : $source ;
272
        $target = $this->getTestFile();
273
        copy($source, $target);
274
        $record->getMapper()->setSource($target);
275
    }
276
277
    protected function getSourceFileMeta(): string
278
    {
279
        return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'target.meta';
280
    }
281
282
    protected function getSourceFileClassic(): string
283
    {
284
        return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'target.data';
285
    }
286
287
    protected function getTestFile(): string
288
    {
289
        return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'tableTest.txt';
290
    }
291
}
292
293
294
class TableBefore extends \TableRecord
295
{
296
    protected function addEntries(): void
297
    {
298
        parent::addEntries();
299
        $this->setMapper('\MappersTests\File\TableBeforeMapper');
300
    }
301
}
302
303
304
class TableBeforeMapper extends \TableMapper
305
{
306
    protected function beforeLoad(ARecord $record): bool
307
    {
308
        return false;
309
    }
310
311
    protected function beforeSave(ARecord $record): bool
312
    {
313
        return false;
314
    }
315
}
316
317
318
class TableAfter extends \TableRecord
319
{
320
    protected function addEntries(): void
321
    {
322
        parent::addEntries();
323
        $this->setMapper('\MappersTests\File\TableAfterMapper');
324
    }
325
}
326
327
328
class TableAfterMapper extends \TableMapper
329
{
330
    protected function afterLoad(ARecord $record): bool
331
    {
332
        return false;
333
    }
334
335
    protected function afterSave(ARecord $record): bool
336
    {
337
        return false;
338
    }
339
}
340