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

TableRecord   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 10
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 1
eloc 7
c 1
b 0
f 0
dl 0
loc 10
rs 10
1
<?php
2
3
use kalanis\kw_mapper\Interfaces\IEntryType;
4
use kalanis\kw_mapper\MapperException;
5
use kalanis\kw_mapper\Mappers\AMapper;
6
use kalanis\kw_mapper\Mappers\Database\ADatabase;
7
use kalanis\kw_mapper\Mappers\File\ATable;
8
use kalanis\kw_mapper\Records\ARecord;
9
use kalanis\kw_mapper\Records\ASimpleRecord;
10
use kalanis\kw_mapper\Storage\Database;
11
use kalanis\kw_mapper\Storage\Shared;
12
use PHPUnit\Framework\TestCase;
13
14
15
/**
16
 * Class CommonTestClass
17
 * The structure for mocking and configuration seems so complicated, but it's necessary to let it be totally idiot-proof
18
 */
19
class CommonTestClass extends TestCase
20
{
21
}
22
23
24
class Builder extends Shared\QueryBuilder
25
{
26
    public function resetCounter(): void
27
    {
28
        static::$uniqId = 0;
29
    }
30
}
31
32
33
class Builder2 extends Database\QueryBuilder
34
{
35
    public function resetCounter(): void
36
    {
37
        static::$uniqId = 0;
38
    }
39
}
40
41
42
class StrObjMock
43
{
44
    public function __toString()
45
    {
46
        return 'strObj';
47
    }
48
}
49
50
51
/**
52
 * Class TableRecord
53
 * Source file dumped from kw_menu
54
 * @property string file
55
 * @property int order
56
 * @property string title
57
 * @property string desc
58
 * @property bool sub
59
 */
60
class TableRecord extends ASimpleRecord
61
{
62
    protected function addEntries(): void
63
    {
64
        $this->addEntry('file', IEntryType::TYPE_STRING, 512);
65
        $this->addEntry('order', IEntryType::TYPE_INTEGER, 64);
66
        $this->addEntry('title', IEntryType::TYPE_STRING, 512);
67
        $this->addEntry('desc', IEntryType::TYPE_STRING, 512);
68
        $this->addEntry('sub', IEntryType::TYPE_BOOLEAN);
69
        $this->setMapper('\TableMapper');
70
    }
71
}
72
73
74
class TableMapper extends ATable
75
{
76
    protected function setMap(): void
77
    {
78
        $this->setFormat('\kalanis\kw_mapper\Storage\File\Formats\SeparatedElements');
79
        $this->setSource(__DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'target.meta');
80
        $this->setRelation('file', 0);
81
        $this->setRelation('order', 1);
82
        $this->setRelation('title', 2);
83
        $this->setRelation('desc', 3);
84
        $this->setRelation('sub', 4);
85
        $this->addPrimaryKey('file');
86
    }
87
}
88
89
/**
90
 * Class TableRecord
91
 * Source file dumped from kw_menu
92
 * @property int id
93
 * @property string file
94
 * @property string title
95
 * @property string desc
96
 * @property bool enabled
97
 */
98
class TableIdRecord extends ASimpleRecord
99
{
100
    protected function addEntries(): void
101
    {
102
        $this->addEntry('id', IEntryType::TYPE_INTEGER, 64);
103
        $this->addEntry('file', IEntryType::TYPE_STRING, 512);
104
        $this->addEntry('title', IEntryType::TYPE_STRING, 512);
105
        $this->addEntry('desc', IEntryType::TYPE_STRING, 512);
106
        $this->addEntry('enabled', IEntryType::TYPE_BOOLEAN);
107
    }
108
109
    public function useIdAsMapper(): void
110
    {
111
        $this->setMapper('\TableIdMapper');
112
    }
113
114
    public function useNoKeyMapper(): void
115
    {
116
        $this->setMapper('\TableNoPkMapper');
117
    }
118
119
    public function setAnotherMapper(string $name): void
120
    {
121
        $this->setMapper($name);
122
    }
123
}
124
125
126
class TableNoPkMapper extends ATable
127
{
128
    protected function setMap(): void
129
    {
130
        $this->setFormat('\kalanis\kw_mapper\Storage\File\Formats\SeparatedElements');
131
        $this->setSource(__DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'target.data');
132
        $this->setRelation('id', 0);
133
        $this->setRelation('file', 1);
134
        $this->setRelation('title', 2);
135
        $this->setRelation('desc', 3);
136
        $this->setRelation('enabled', 4);
137
    }
138
}
139
140
141
class TableIdMapper extends TableNoPkMapper
142
{
143
    protected function setMap(): void
144
    {
145
        parent::setMap();
146
        $this->addPrimaryKey('id');
147
    }
148
}
149
150
/**
151
 * Class XSimpleRecord
152
 * Simple record for testing factory
153
 * @property int id
154
 * @property string title
155
 */
156
class XSimpleRecord extends ASimpleRecord
157
{
158
    protected function addEntries(): void
159
    {
160
        $this->addEntry('id', IEntryType::TYPE_INTEGER, 64);
161
        $this->addEntry('title', IEntryType::TYPE_STRING, 512);
162
    }
163
164
    public function useDatabase(): void
165
    {
166
        $this->setMapper('\XDatabaseMapper');
167
    }
168
169
    public function useFile(): void
170
    {
171
        $this->setMapper('\XFileMapper');
172
    }
173
174
    public function useMock(): void
175
    {
176
        $this->setMapper('\XMockMapper');
177
    }
178
}
179
180
181
class XDatabaseMapper extends ADatabase
182
{
183
    protected function setMap(): void
184
    {
185
        $this->setSource('dummy');
186
        $this->setRelation('id', 'id');
187
        $this->setRelation('title', 'title');
188
        $this->addPrimaryKey('id');
189
    }
190
}
191
192
193
class XFileMapper extends ATable
194
{
195
    protected function setMap(): void
196
    {
197
        $this->setFormat('\kalanis\kw_mapper\Storage\File\Formats\SeparatedElements');
198
        $this->setSource('dummy');
199
        $this->setRelation('id', 'id');
200
        $this->setRelation('title', 'title');
201
        $this->addPrimaryKey('id');
202
    }
203
}
204
205
206
class XMockMapper extends AMapper
207
{
208
    protected function setMap(): void
209
    {
210
        $this->setSource('dummy');
211
        $this->setRelation('id', 'id');
212
        $this->setRelation('title', 'title');
213
    }
214
215
    public function getAlias(): string
216
    {
217
        return $this->getSource();
218
    }
219
220
    protected function insertRecord(ARecord $record): bool
221
    {
222
        return false;
223
    }
224
225
    protected function updateRecord(ARecord $record): bool
226
    {
227
        return false;
228
    }
229
230
    public function countRecord(ARecord $record): int
231
    {
232
        return 0;
233
    }
234
235
    public function loadMultiple(ARecord $record): array
236
    {
237
        return [];
238
    }
239
240
    protected function loadRecord(ARecord $record): bool
241
    {
242
        return false;
243
    }
244
245
    protected function deleteRecord(ARecord $record): bool
246
    {
247
        return false;
248
    }
249
}
250
251
// just fallback for MS Win system - usually defined in external library
252
253
if (!defined('REG_DWORD')) {
254
    define('REG_DWORD', 1);
255
}
256
if (!defined('REG_SZ')) {
257
    define('REG_SZ', 5);
258
}
259
if (!defined('REG_EXPAND_SZ')) {
260
    define('REG_EXPAND_SZ', 2);
261
}
262
if (!defined('REG_MULTI_SZ')) {
263
    define('REG_MULTI_SZ', 3);
264
}
265
if (!defined('REG_BINARY')) {
266
    define('REG_BINARY', 0);
267
}
268
if (!defined('REG_NONE')) {
269
    define('REG_NONE', 4);
270
}
271
272
273
if (!function_exists('reg_close_key')) {
274
    /**
275
     * @param resource $resource
276
     * @throws MapperException
277
     * @return void
278
     */
279
    function reg_close_key($resource) {
280
        throw new MapperException('No win32std library!');
281
    }
282
}
283
if (!function_exists('reg_create_key')) {
284
    /**
285
     * @param int $part
286
     * @param string $subKey
287
     * @param int|null $samDesired
288
     * @throws MapperException
289
     * @return mixed
290
     */
291
    function reg_create_key($part, $subKey, $samDesired = null)
292
    {
293
        throw new MapperException('No win32std library!');
294
    }
295
}
296
if (!function_exists('reg_enum_key')) {
297
    /**
298
     * @param int $part
299
     * @param int $index
300
     * @throws MapperException
301
     * @return mixed
302
     */
303
    function reg_enum_key($part, $index = -1) {
304
        throw new MapperException('No win32std library!');
305
    }
306
}
307
if (!function_exists('reg_enum_value')) {
308
    /**
309
     * @param resource $resource
310
     * @param int $index
311
     * @throws MapperException
312
     * @return mixed
313
     */
314
    function reg_enum_value($resource, $index = -1) {
315
        throw new MapperException('No win32std library!');
316
    }
317
}
318
if (!function_exists('reg_open_key')) {
319
    /**
320
     * @param int $part
321
     * @param string $subKey
322
     * @param int|null $samDesired
323
     * @throws MapperException
324
     * @return mixed
325
     */
326
    function reg_open_key($part, $subKey, $samDesired = null) {
327
        throw new MapperException('No win32std library!');
328
    }
329
}
330
if (!function_exists('reg_set_value')) {
331
    /**
332
     * @param int $part
333
     * @param string $valueName
334
     * @param int $typeId
335
     * @param mixed $value
336
     * @throws MapperException
337
     * @return void
338
     */
339
    function reg_set_value($part, $valueName, $typeId, $value) {
340
        throw new MapperException('No win32std library!');
341
    }
342
}
343
if (!function_exists('reg_get_value')) {
344
    /**
345
     * @param int $part
346
     * @param string $valueName
347
     * @throws MapperException
348
     * @return mixed
349
     */
350
    function reg_get_value($part, $valueName) {
351
        throw new MapperException('No win32std library!');
352
    }
353
}
354