|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RecordsTests; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use CommonTestClass; |
|
7
|
|
|
use kalanis\kw_mapper\Adapters\DataExchange; |
|
8
|
|
|
use kalanis\kw_mapper\Interfaces\IEntryType; |
|
9
|
|
|
use kalanis\kw_mapper\MapperException; |
|
10
|
|
|
use kalanis\kw_mapper\Mappers; |
|
11
|
|
|
use kalanis\kw_mapper\Records\AStrictRecord; |
|
12
|
|
|
use kalanis\kw_mapper\Records\ASimpleRecord; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class RecordTest extends CommonTestClass |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @throws MapperException |
|
19
|
|
|
*/ |
|
20
|
|
|
public function testSimple(): void |
|
21
|
|
|
{ |
|
22
|
|
|
$data = new UserSimpleRecord(); |
|
23
|
|
|
$this->assertEmpty($data['id']); |
|
24
|
|
|
$this->assertEmpty($data['name']); |
|
25
|
|
|
$this->assertEmpty($data['password']); |
|
26
|
|
|
$this->assertEmpty($data['enabled']); |
|
27
|
|
|
$this->assertEmpty($data['details']); |
|
28
|
|
|
$this->assertNotEmpty($data->getMapper()); |
|
29
|
|
|
$this->assertInstanceOf('\kalanis\kw_mapper\Mappers\AMapper', $data->getMapper()); |
|
30
|
|
|
|
|
31
|
|
|
$data['id'] = '999'; |
|
32
|
|
|
$data['name'] = 321654897; |
|
33
|
|
|
$data['password'] = 'lkjhgfdsa'; |
|
34
|
|
|
$data['enabled'] = true; |
|
35
|
|
|
$data['enabled'] = '1'; |
|
36
|
|
|
$data['details'] = ['auth' => 'ldap', 'rights' => 'limited']; |
|
37
|
|
|
|
|
38
|
|
|
$this->assertEquals('999', $data['id']); |
|
39
|
|
|
$this->assertEquals(321654897, $data['name']); |
|
40
|
|
|
$this->assertEquals('lkjhgfdsa', $data['password']); |
|
41
|
|
|
$this->assertEquals('ldap', $data['details']['auth']); |
|
42
|
|
|
|
|
43
|
|
|
$data2 = clone $data; |
|
44
|
|
|
$data2->name = 'zgvtfcrdxesy'; |
|
45
|
|
|
$data2->password = 'mnbvcxy'; |
|
46
|
|
|
$this->assertEquals('zgvtfcrdxesy', $data2['name']); |
|
47
|
|
|
$this->assertEquals('mnbvcxy', $data2['password']); |
|
48
|
|
|
$this->assertNotEquals('zgvtfcrdxesy', $data['name']); |
|
49
|
|
|
$this->assertNotEquals('mnbvcxy', $data['password']); |
|
50
|
|
|
$this->assertEquals(321654897, $data->name); |
|
51
|
|
|
$this->assertEquals('lkjhgfdsa', $data->password); |
|
52
|
|
|
|
|
53
|
|
|
$objectEntry = $data->getEntry('details'); |
|
54
|
|
|
$this->assertEquals(IEntryType::TYPE_OBJECT, $objectEntry->getType()); |
|
55
|
|
|
$this->assertInstanceOf('\kalanis\kw_mapper\Interfaces\ICanFill', $objectEntry->getData()); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($data as $key => $entry) { |
|
58
|
|
|
$this->assertNotEmpty($key); |
|
59
|
|
|
$this->assertNotEmpty($entry); |
|
60
|
|
|
$this->assertInstanceOf('\kalanis\kw_mapper\Records\Entry', $data->getEntry($key)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$data->details = 'another piece'; |
|
64
|
|
|
$this->assertEquals('another piece', $data->details); |
|
65
|
|
|
|
|
66
|
|
|
$data->loadWithData(['name' => 'okmijn', 'id' => '555', 'unset' => 'asdfghj']); |
|
67
|
|
|
$this->assertEquals('okmijn', $data->name); |
|
68
|
|
|
$this->assertEquals('555', $data->id); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @throws MapperException |
|
73
|
|
|
*/ |
|
74
|
|
|
public function testStrict(): void |
|
75
|
|
|
{ |
|
76
|
|
|
$data = new UserStrictRecord(); |
|
77
|
|
|
$this->assertEmpty($data['id']); |
|
78
|
|
|
$this->assertEmpty($data['name']); |
|
79
|
|
|
$this->assertEmpty($data['password']); |
|
80
|
|
|
$this->assertEmpty($data['enabled']); |
|
81
|
|
|
$this->assertEmpty($data['details']); |
|
82
|
|
|
|
|
83
|
|
|
$data['id'] = 999; |
|
84
|
|
|
$data['name'] = 'plokmijnuhb'; |
|
85
|
|
|
$data['password'] = 'lkjhgfdsa'; |
|
86
|
|
|
$data['enabled'] = true; |
|
87
|
|
|
$data['details'] = 'simply'; |
|
88
|
|
|
$data['details'] = ['auth' => 'ldap', 'rights' => 'limited']; |
|
89
|
|
|
|
|
90
|
|
|
$this->assertEquals(999, $data['id']); |
|
91
|
|
|
$this->assertEquals('plokmijnuhb', $data['name']); |
|
92
|
|
|
$this->assertEquals('lkjhgfdsa', $data['password']); |
|
93
|
|
|
$this->assertEquals('ldap', $data['details']['auth']); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @throws MapperException |
|
98
|
|
|
*/ |
|
99
|
|
|
public function testCannotAddLater(): void |
|
100
|
|
|
{ |
|
101
|
|
|
$data = new UserStrictRecord(); |
|
102
|
|
|
$this->expectException(MapperException::class); |
|
103
|
|
|
$data['expect'] = 'nothing'; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @throws MapperException |
|
108
|
|
|
*/ |
|
109
|
|
|
public function testCannotRemove(): void |
|
110
|
|
|
{ |
|
111
|
|
|
$data = new UserStrictRecord(); |
|
112
|
|
|
$this->expectException(MapperException::class); |
|
113
|
|
|
unset($data['password']); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @throws MapperException |
|
118
|
|
|
*/ |
|
119
|
|
|
public function testLimitBoolType(): void |
|
120
|
|
|
{ |
|
121
|
|
|
$data1 = new UserSimpleRecord(); |
|
122
|
|
|
$data1->enabled = null; |
|
123
|
|
|
$data1->enabled = 'yes'; |
|
124
|
|
|
|
|
125
|
|
|
$data2 = new UserStrictRecord(); |
|
126
|
|
|
$data2->enabled = null; |
|
127
|
|
|
$this->expectException(MapperException::class); |
|
128
|
|
|
$data2->enabled = 'yes'; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @throws MapperException |
|
133
|
|
|
*/ |
|
134
|
|
|
public function testLimitIntType(): void |
|
135
|
|
|
{ |
|
136
|
|
|
$data1 = new UserSimpleRecord(); |
|
137
|
|
|
$data1['id'] = null; |
|
138
|
|
|
$data1['id'] = 'yes'; |
|
139
|
|
|
|
|
140
|
|
|
$data2 = new UserStrictRecord(); |
|
141
|
|
|
$data2['id'] = null; |
|
142
|
|
|
$this->expectException(MapperException::class); |
|
143
|
|
|
$data2['id'] = 'yes'; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @throws MapperException |
|
148
|
|
|
*/ |
|
149
|
|
|
public function testLimitIntSize(): void |
|
150
|
|
|
{ |
|
151
|
|
|
$data1 = new UserSimpleRecord(); |
|
152
|
|
|
$data1['id'] = 8888; |
|
153
|
|
|
$data1['id'] = 8889; |
|
154
|
|
|
|
|
155
|
|
|
$data2 = new UserStrictRecord(); |
|
156
|
|
|
$data2['id'] = 8888; |
|
157
|
|
|
$this->expectException(MapperException::class); |
|
158
|
|
|
$data2['id'] = 8889; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @throws MapperException |
|
163
|
|
|
*/ |
|
164
|
|
|
public function testLimitStringType(): void |
|
165
|
|
|
{ |
|
166
|
|
|
$data1 = new UserSimpleRecord(); |
|
167
|
|
|
$data1['password'] = null; |
|
168
|
|
|
$data1['password'] = new \stdClass(); |
|
169
|
|
|
|
|
170
|
|
|
$data2 = new UserStrictRecord(); |
|
171
|
|
|
$data2['password'] = null; |
|
172
|
|
|
$this->expectException(MapperException::class); |
|
173
|
|
|
$data2['password'] = new \stdClass(); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @throws MapperException |
|
178
|
|
|
*/ |
|
179
|
|
|
public function testLimitStringSize(): void |
|
180
|
|
|
{ |
|
181
|
|
|
$data1 = new UserSimpleRecord(); |
|
182
|
|
|
$data1['password'] = 'poiuztrelkjhgfds'; |
|
183
|
|
|
$data1['password'] = 'poiuztrelkjhgfdsa'; |
|
184
|
|
|
|
|
185
|
|
|
$data2 = new UserStrictRecord(); |
|
186
|
|
|
$data2['password'] = 'poiuztrelkjhgfds'; |
|
187
|
|
|
$this->expectException(MapperException::class); |
|
188
|
|
|
$data2['password'] = 'poiuztrelkjhgfdsa'; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @throws MapperException |
|
193
|
|
|
*/ |
|
194
|
|
|
public function testInvalidLimit(): void |
|
195
|
|
|
{ |
|
196
|
|
|
$this->expectException(MapperException::class); |
|
197
|
|
|
new FailedUserRecord1(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @throws MapperException |
|
202
|
|
|
*/ |
|
203
|
|
|
public function testInvalidSize(): void |
|
204
|
|
|
{ |
|
205
|
|
|
$this->expectException(MapperException::class); |
|
206
|
|
|
new FailedUserRecord2(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @throws MapperException |
|
211
|
|
|
*/ |
|
212
|
|
|
public function testInvalidObject(): void |
|
213
|
|
|
{ |
|
214
|
|
|
$this->expectException(MapperException::class); |
|
215
|
|
|
new FailedUserRecord3(); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @throws MapperException |
|
220
|
|
|
*/ |
|
221
|
|
|
public function testInvalidObjectDef(): void |
|
222
|
|
|
{ |
|
223
|
|
|
$this->expectException(MapperException::class); |
|
224
|
|
|
new FailedUserRecord4(); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @throws MapperException |
|
229
|
|
|
*/ |
|
230
|
|
|
public function testInvalidPreset(): void |
|
231
|
|
|
{ |
|
232
|
|
|
$data = new FailedUserRecord5(); |
|
233
|
|
|
$data->status = null; |
|
234
|
|
|
$data->status = 'fail'; |
|
235
|
|
|
$this->expectException(MapperException::class); |
|
236
|
|
|
$data->status = 'not-set'; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @throws MapperException |
|
241
|
|
|
*/ |
|
242
|
|
|
public function testInsertInvalidInputArray1(): void |
|
243
|
|
|
{ |
|
244
|
|
|
$data = new FailedUserRecord6(); |
|
245
|
|
|
$this->expectException(MapperException::class); |
|
246
|
|
|
$data->others = 'okmijn'; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @throws MapperException |
|
251
|
|
|
*/ |
|
252
|
|
|
public function testInsertInvalidInputArray2(): void |
|
253
|
|
|
{ |
|
254
|
|
|
$data = new FailedUserRecord6(); |
|
255
|
|
|
$data->others = [new UserStrictRecord(), new UserSimpleRecord()]; |
|
256
|
|
|
$this->expectException(MapperException::class); |
|
257
|
|
|
$data->others = ['okmijn']; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @throws MapperException |
|
262
|
|
|
*/ |
|
263
|
|
|
public function testDataExchange(): void |
|
264
|
|
|
{ |
|
265
|
|
|
$data = new UserSimpleRecord(); |
|
266
|
|
|
$data['id'] = '999'; |
|
267
|
|
|
$data['name'] = 321654897; |
|
268
|
|
|
$data['password'] = 'lkjhgfdsa'; |
|
269
|
|
|
$data['enabled'] = true; |
|
270
|
|
|
|
|
271
|
|
|
$ex = new DataExchange($data); |
|
272
|
|
|
$ex->addExclude('password'); |
|
273
|
|
|
$this->assertEquals(1, $ex->import(['id' => 888, 'password' => 'mnbvcxy'])); |
|
274
|
|
|
$ex->clearExclude(); |
|
275
|
|
|
$pack = $ex->export(); |
|
276
|
|
|
|
|
277
|
|
|
$this->assertEquals(888, $pack['id']); |
|
278
|
|
|
$this->assertEquals(321654897, $pack['name']); |
|
279
|
|
|
$this->assertEquals('lkjhgfdsa', $pack['password']); |
|
280
|
|
|
$this->assertEquals(true, $pack['enabled']); |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Class UserStrictRecord |
|
287
|
|
|
* @package RecordsTests |
|
288
|
|
|
* @property int id |
|
289
|
|
|
* @property string name |
|
290
|
|
|
* @property string password |
|
291
|
|
|
* @property bool enabled |
|
292
|
|
|
* @property \kalanis\kw_mapper\Adapters\MappedStdClass details |
|
293
|
|
|
*/ |
|
294
|
|
|
class UserStrictRecord extends AStrictRecord |
|
295
|
|
|
{ |
|
296
|
|
|
protected function addEntries(): void |
|
297
|
|
|
{ |
|
298
|
|
|
$this->addEntry('id', IEntryType::TYPE_INTEGER, 8888); // max size of inner number is 8888 |
|
299
|
|
|
$this->addEntry('name', IEntryType::TYPE_STRING, 128); |
|
300
|
|
|
$this->addEntry('password', IEntryType::TYPE_STRING, 16); // max length of string is 16 chars |
|
301
|
|
|
$this->addEntry('status', IEntryType::TYPE_SET, ['ok', 'fail', 'error']); |
|
302
|
|
|
$this->addEntry('enabled', IEntryType::TYPE_BOOLEAN); |
|
303
|
|
|
$this->addEntry('others', IEntryType::TYPE_ARRAY); |
|
304
|
|
|
$this->addEntry('details', IEntryType::TYPE_OBJECT, '\kalanis\kw_mapper\Adapters\MappedStdClass'); |
|
305
|
|
|
$this->setMapper('\RecordsTests\UserFileMapper'); |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Class UserSimpleRecord |
|
312
|
|
|
* @package RecordsTests |
|
313
|
|
|
* @property int id |
|
314
|
|
|
* @property string name |
|
315
|
|
|
* @property string password |
|
316
|
|
|
* @property bool enabled |
|
317
|
|
|
* @property \kalanis\kw_mapper\Adapters\MappedStdClass details |
|
318
|
|
|
*/ |
|
319
|
|
|
class UserSimpleRecord extends ASimpleRecord |
|
320
|
|
|
{ |
|
321
|
|
|
protected function addEntries(): void |
|
322
|
|
|
{ |
|
323
|
|
|
$this->addEntry('id', IEntryType::TYPE_INTEGER, 8888); // max size of inner number is 8888 |
|
324
|
|
|
$this->addEntry('name', IEntryType::TYPE_STRING, 128); |
|
325
|
|
|
$this->addEntry('password', IEntryType::TYPE_STRING, 16); // max length of string is 16 chars |
|
326
|
|
|
$this->addEntry('enabled', IEntryType::TYPE_BOOLEAN); |
|
327
|
|
|
$this->addEntry('details', IEntryType::TYPE_OBJECT, '\kalanis\kw_mapper\Adapters\MappedStdClass'); |
|
328
|
|
|
$this->setMapper('\RecordsTests\UserFileMapper'); |
|
329
|
|
|
} |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
|
|
333
|
|
|
class FailedUserRecord1 extends ASimpleRecord |
|
334
|
|
|
{ |
|
335
|
|
|
protected function addEntries(): void |
|
336
|
|
|
{ |
|
337
|
|
|
$this->addEntry('id', 777, '456'); |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
|
|
342
|
|
|
class FailedUserRecord2 extends ASimpleRecord |
|
343
|
|
|
{ |
|
344
|
|
|
protected function addEntries(): void |
|
345
|
|
|
{ |
|
346
|
|
|
$this->addEntry('id', IEntryType::TYPE_INTEGER, 'asdf'); |
|
347
|
|
|
} |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
|
|
351
|
|
|
class FailedUserRecord3 extends ASimpleRecord |
|
352
|
|
|
{ |
|
353
|
|
|
protected function addEntries(): void |
|
354
|
|
|
{ |
|
355
|
|
|
$this->addEntry('details', IEntryType::TYPE_OBJECT, new \stdClass()); |
|
356
|
|
|
} |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
|
|
360
|
|
|
class FailedUserRecord4 extends ASimpleRecord |
|
361
|
|
|
{ |
|
362
|
|
|
protected function addEntries(): void |
|
363
|
|
|
{ |
|
364
|
|
|
$this->addEntry('details', IEntryType::TYPE_OBJECT, '\stdClass'); |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
|
|
369
|
|
|
class FailedUserRecord5 extends AStrictRecord |
|
370
|
|
|
{ |
|
371
|
|
|
protected function addEntries(): void |
|
372
|
|
|
{ |
|
373
|
|
|
$this->addEntry('status', IEntryType::TYPE_SET, ['ok', 'fail', 'error']); |
|
374
|
|
|
} |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
|
|
378
|
|
|
class FailedUserRecord6 extends AStrictRecord |
|
379
|
|
|
{ |
|
380
|
|
|
protected function addEntries(): void |
|
381
|
|
|
{ |
|
382
|
|
|
$this->addEntry('others', IEntryType::TYPE_ARRAY); |
|
383
|
|
|
} |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
|
|
387
|
|
|
class UserFileMapper extends Mappers\File\ATable |
|
388
|
|
|
{ |
|
389
|
|
|
protected function setMap(): void |
|
390
|
|
|
{ |
|
391
|
|
|
$this->setSource(__DIR__ . DIRECTORY_SEPARATOR . 'users.txt'); |
|
392
|
|
|
$this->setFormat('\kalanis\kw_mapper\Storage\File\Formats\SeparatedElements'); |
|
393
|
|
|
$this->setRelation('id', 0); |
|
394
|
|
|
$this->setRelation('name', 1); |
|
395
|
|
|
$this->setRelation('password', 2); |
|
396
|
|
|
$this->setRelation('enabled', 3); |
|
397
|
|
|
$this->setRelation('details', 4); |
|
398
|
|
|
$this->addPrimaryKey('id'); |
|
399
|
|
|
} |
|
400
|
|
|
} |
|
401
|
|
|
|