1
|
|
|
<?php |
2
|
|
|
require_once 'stubs/Category.php'; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @SuppressWarnings("StaticAccess") |
6
|
|
|
*/ |
7
|
|
|
class DBObjectTest extends \PHPUnit\Framework\TestCase |
8
|
|
|
{ |
9
|
|
|
protected $tableName = 'users'; |
10
|
|
|
public function testContructor() |
11
|
|
|
{ |
12
|
|
|
$classname = '\Suricate\DBObject'; |
13
|
|
|
|
14
|
|
|
// Get mock, without the constructor being called |
15
|
|
|
$mock = $this->getMockBuilder($classname) |
16
|
|
|
->disableOriginalConstructor() |
17
|
|
|
->setMethods(array('setRelations')) |
18
|
|
|
->getMockForAbstractClass(); |
19
|
|
|
|
20
|
|
|
// set expectations for constructor calls |
21
|
|
|
$mock->expects($this->once()) |
22
|
|
|
->method('setRelations'); |
23
|
|
|
|
24
|
|
|
// now call the constructor |
25
|
|
|
$reflectedClass = new ReflectionClass($classname); |
26
|
|
|
$constructor = $reflectedClass->getConstructor(); |
27
|
|
|
$constructor->invoke($mock); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testGetTableName() |
31
|
|
|
{ |
32
|
|
|
$testName = 'my_sql_table'; |
33
|
|
|
|
34
|
|
|
$testDBO = new \Suricate\DBObject(); |
35
|
|
|
self::mockProperty($testDBO, 'tableName', $testName); |
36
|
|
|
$this->assertEquals($testName, $testDBO->getTableName()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testGetTableIndex() |
40
|
|
|
{ |
41
|
|
|
$testIndex = 'id'; |
42
|
|
|
|
43
|
|
|
$testDBO = new \Suricate\DBObject(); |
44
|
|
|
self::mockProperty($testDBO, 'tableIndex', $testIndex); |
45
|
|
|
$this->assertEquals($testIndex, $testDBO->getTableIndex()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testGetDBConfig() |
49
|
|
|
{ |
50
|
|
|
$testConfigName = 'my_config'; |
51
|
|
|
|
52
|
|
|
$testDBO = new \Suricate\DBObject(); |
53
|
|
|
self::mockProperty($testDBO, 'DBConfig', $testConfigName); |
54
|
|
|
$this->assertEquals($testConfigName, $testDBO->getDBConfig()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testUndefinedGet() |
58
|
|
|
{ |
59
|
|
|
$testDBO = new \Suricate\DBObject(); |
60
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'last_update']); |
61
|
|
|
$this->expectException(\InvalidArgumentException::class); |
62
|
|
|
|
63
|
|
|
$testDBO->undefinedVar; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testDBProperty() |
67
|
|
|
{ |
68
|
|
|
$testDBO = new \Suricate\DBObject(); |
69
|
|
|
$testDBO->regularProperty = 42; |
|
|
|
|
70
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'not_loaded_var']); |
71
|
|
|
self::mockProperty($testDBO, 'dbValues', ['id' => 1, 'name' => 'test name']); |
72
|
|
|
$this->assertEquals($testDBO->id, 1); |
|
|
|
|
73
|
|
|
$this->assertNotEquals($testDBO->name, 'test name edited'); |
|
|
|
|
74
|
|
|
$this->assertNull($testDBO->not_loaded_var); |
|
|
|
|
75
|
|
|
|
76
|
|
|
$this->assertTrue($testDBO->isDBVariable('id')); |
77
|
|
|
$this->assertFalse($testDBO->isDBVariable('regularProperty')); |
78
|
|
|
|
79
|
|
|
$this->assertTrue($testDBO->propertyExists('regularProperty')); |
80
|
|
|
$this->assertTrue($testDBO->propertyExists('id')); |
81
|
|
|
$this->assertFalse($testDBO->propertyExists('unknownProperty')); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testIsset() |
85
|
|
|
{ |
86
|
|
|
$testDBO = new \Suricate\DBObject(); |
87
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'not_loaded_var']); |
88
|
|
|
self::mockProperty($testDBO, 'dbValues', ['id' => 1, 'name' => 'test name']); |
89
|
|
|
|
90
|
|
|
$this->assertTrue(isset($testDBO->id)); |
|
|
|
|
91
|
|
|
$this->assertFalse(isset($testDBO->undefVar)); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testIsLoaded() |
95
|
|
|
{ |
96
|
|
|
$testIndex = 'id'; |
97
|
|
|
|
98
|
|
|
$testDBO = new \Suricate\DBObject(); |
99
|
|
|
self::mockProperty($testDBO, 'tableIndex', $testIndex); |
100
|
|
|
self::mockProperty($testDBO, 'dbVariables', [$testIndex, 'name', 'not_loaded_var']); |
101
|
|
|
$this->assertFalse($testDBO->isLoaded()); |
102
|
|
|
|
103
|
|
|
self::mockProperty($testDBO, 'dbValues', [$testIndex => 1, 'name' => 'test name']); |
104
|
|
|
$this->assertFalse($testDBO->isLoaded()); |
105
|
|
|
|
106
|
|
|
$this->setupData(); |
107
|
|
|
$dbo = $this->getDBOject(); |
108
|
|
|
$this->assertFalse($dbo->isLoaded()); |
109
|
|
|
$dbo->load(1); |
110
|
|
|
$this->assertTrue($dbo->isLoaded()); |
111
|
|
|
$dbo->load(999); |
112
|
|
|
$this->assertFalse($dbo->isLoaded()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testProtected() |
116
|
|
|
{ |
117
|
|
|
$testDBO = Category::instanciate([ |
118
|
|
|
'id' => 1, |
119
|
|
|
'name' => 'test record', |
120
|
|
|
]); |
121
|
|
|
$reflector = new ReflectionClass(Category::class); |
122
|
|
|
$property = $reflector->getProperty('protectedValues'); |
123
|
|
|
$property->setAccessible(true); |
124
|
|
|
$this->assertSame([], $property->getValue($testDBO)); |
125
|
|
|
$this->assertNull($testDBO->unloadable); |
|
|
|
|
126
|
|
|
|
127
|
|
|
$reflector = new ReflectionClass(Category::class); |
128
|
|
|
$property = $reflector->getProperty('protectedValues'); |
129
|
|
|
$property->setAccessible(true); |
130
|
|
|
$this->assertSame([], $property->getValue($testDBO)); |
131
|
|
|
|
132
|
|
|
$reflector = new ReflectionClass(Category::class); |
133
|
|
|
$property = $reflector->getProperty('loadedProtectedVariables'); |
134
|
|
|
$property->setAccessible(true); |
135
|
|
|
$this->assertSame([], $property->getValue($testDBO)); |
136
|
|
|
|
137
|
|
|
$this->assertSame(42, $testDBO->prot_var); |
|
|
|
|
138
|
|
|
$property = $reflector->getProperty('protectedValues'); |
139
|
|
|
$property->setAccessible(true); |
140
|
|
|
$this->assertSame(['prot_var' => 42], $property->getValue($testDBO)); |
141
|
|
|
|
142
|
|
|
$reflector = new ReflectionClass(Category::class); |
143
|
|
|
$property = $reflector->getProperty('loadedProtectedVariables'); |
144
|
|
|
$property->setAccessible(true); |
145
|
|
|
$this->assertSame(['prot_var' => true], $property->getValue($testDBO)); |
146
|
|
|
} |
147
|
|
|
public function testInstanciate() |
148
|
|
|
{ |
149
|
|
|
$testDBO = Category::instanciate([ |
150
|
|
|
'id' => 1, |
151
|
|
|
'name' => 'test record', |
152
|
|
|
]); |
153
|
|
|
|
154
|
|
|
$reflector = new ReflectionClass(Category::class); |
155
|
|
|
$property = $reflector->getProperty('dbValues'); |
156
|
|
|
$property->setAccessible(true); |
157
|
|
|
$this->assertEquals([ |
158
|
|
|
'id' => 1, |
159
|
|
|
'name' => 'test record', |
160
|
|
|
], $property->getValue($testDBO)); |
161
|
|
|
|
162
|
|
|
$this->assertFalse($testDBO->isLoaded()); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testHydrate() |
166
|
|
|
{ |
167
|
|
|
$testDBO = new \Suricate\DBObject(); |
168
|
|
|
$testDBO->realProperty = ''; |
|
|
|
|
169
|
|
|
|
170
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name']); |
171
|
|
|
$testDBO->hydrate([ |
172
|
|
|
'id' => 1, |
173
|
|
|
'name' => 'test record', |
174
|
|
|
'add_column' => 'test value', |
175
|
|
|
'realProperty' => 'my string', |
176
|
|
|
]); |
177
|
|
|
|
178
|
|
|
$this->assertEquals($testDBO->realProperty, 'my string'); |
|
|
|
|
179
|
|
|
|
180
|
|
|
$reflector = new ReflectionClass(get_class($testDBO)); |
181
|
|
|
$property = $reflector->getProperty('dbValues'); |
182
|
|
|
$property->setAccessible(true); |
183
|
|
|
$this->assertEquals([ |
184
|
|
|
'id' => 1, |
185
|
|
|
'name' => 'test record', |
186
|
|
|
], $property->getValue($testDBO)); |
187
|
|
|
|
188
|
|
|
$this->assertFalse($testDBO->isLoaded()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testWakeup() |
192
|
|
|
{ |
193
|
|
|
$mock = $this->getMockBuilder(\Suricate\DBObject::class) |
194
|
|
|
->setMethods(['setRelations']) |
195
|
|
|
->getMock(); |
196
|
|
|
|
197
|
|
|
$mock |
198
|
|
|
->expects($this->once()) |
199
|
|
|
->method('setRelations'); |
200
|
|
|
|
201
|
|
|
$mock->__wakeup(); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function testRelationOneOne() |
205
|
|
|
{ |
206
|
|
|
$relations = [ |
207
|
|
|
'category' => [ |
208
|
|
|
'type' => \Suricate\DBObject::RELATION_ONE_ONE, |
209
|
|
|
'source' => 'category_id', |
210
|
|
|
'target' => 'Category' |
211
|
|
|
] |
212
|
|
|
]; |
213
|
|
|
// Prepare database |
214
|
|
|
$this->setupData(); |
215
|
|
|
$mock = $this->getMockBuilder(\Suricate\DBObject::class) |
216
|
|
|
->setMethods(['setRelations', 'getRelation']) |
217
|
|
|
->getMock(); |
218
|
|
|
|
219
|
|
|
// Prepare setup DBObject |
220
|
|
|
$testDBO = $this->getDBOject(); |
221
|
|
|
$reflector = new ReflectionClass($mock); |
222
|
|
|
$property = $reflector->getProperty('relations'); |
223
|
|
|
$property->setAccessible(true); |
224
|
|
|
$property->setValue($testDBO, $relations); |
225
|
|
|
|
226
|
|
|
// get relation values |
227
|
|
|
$reflector = new ReflectionClass($testDBO); |
228
|
|
|
$relationValuesRef = $reflector->getProperty('relationValues'); |
229
|
|
|
$relationValuesRef->setAccessible(true); |
230
|
|
|
|
231
|
|
|
$loadedRelationsRef = $reflector->getProperty('loadedRelations'); |
232
|
|
|
$loadedRelationsRef->setAccessible(true); |
233
|
|
|
|
234
|
|
|
// Load |
235
|
|
|
$testDBO->load(1); |
236
|
|
|
$relationsValues = $relationValuesRef->getValue($testDBO); |
237
|
|
|
$loadedRelations = $loadedRelationsRef->getValue($testDBO); |
238
|
|
|
|
239
|
|
|
// No relation values at first |
240
|
|
|
$this->assertSame([], $relationsValues); |
241
|
|
|
$this->assertSame([], $loadedRelations); |
242
|
|
|
$this->assertEquals('Admin', $testDBO->category->name); |
|
|
|
|
243
|
|
|
$this->assertInstanceOf('\Suricate\DBObject', $testDBO->category); |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
$relationsValues = $relationValuesRef->getValue($testDBO); |
247
|
|
|
$loadedRelations = $loadedRelationsRef->getValue($testDBO); |
248
|
|
|
|
249
|
|
|
// Check relation cache has been set |
250
|
|
|
$this->assertArrayHasKey('category', $relationsValues); |
251
|
|
|
|
252
|
|
|
// Check relation loaded flag has been set |
253
|
|
|
$this->assertArrayHasKey('category', $loadedRelations); |
254
|
|
|
|
255
|
|
|
// Check return type of relation |
256
|
|
|
$this->assertInstanceOf('\Suricate\DBObject', $relationsValues['category']); |
257
|
|
|
|
258
|
|
|
// Load new object |
259
|
|
|
$testDBO = $this->getDBOject(); |
260
|
|
|
$reflector = new ReflectionClass($mock); |
261
|
|
|
$property = $reflector->getProperty('relations'); |
262
|
|
|
$property->setAccessible(true); |
263
|
|
|
$property->setValue($testDBO, $relations); |
264
|
|
|
$testDBO->load(2); |
265
|
|
|
// get relation values |
266
|
|
|
$reflector = new ReflectionClass($testDBO); |
267
|
|
|
$relationValuesRef = $reflector->getProperty('relationValues'); |
268
|
|
|
$relationValuesRef->setAccessible(true); |
269
|
|
|
|
270
|
|
|
$loadedRelationsRef = $reflector->getProperty('loadedRelations'); |
271
|
|
|
$loadedRelationsRef->setAccessible(true); |
272
|
|
|
|
273
|
|
|
$relationsValues = $relationValuesRef->getValue($testDBO); |
274
|
|
|
$loadedRelations = $loadedRelationsRef->getValue($testDBO); |
275
|
|
|
|
276
|
|
|
// No relation values at first |
277
|
|
|
$this->assertSame([], $relationsValues); |
278
|
|
|
$this->assertSame([], $loadedRelations); |
279
|
|
|
|
280
|
|
|
// Isset implicit load relation, check that's been loaded |
281
|
|
|
$this->assertTrue(isset($testDBO->category)); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
public function testLoad() |
285
|
|
|
{ |
286
|
|
|
// Prepare database |
287
|
|
|
$this->setupData(); |
288
|
|
|
|
289
|
|
|
// Inject database handler |
290
|
|
|
$testDBO = $this->getDBOject(); |
291
|
|
|
|
292
|
|
|
$this->assertFalse($testDBO->isLoaded()); |
293
|
|
|
$retVal = $testDBO->load(1); |
294
|
|
|
$this->assertTrue($testDBO->isLoaded()); |
295
|
|
|
$this->assertEquals(1, $testDBO->id); |
|
|
|
|
296
|
|
|
|
297
|
|
|
$this->assertEquals('John', $testDBO->name); |
|
|
|
|
298
|
|
|
|
299
|
|
|
$this->assertInstanceOf('\Suricate\DBObject', $retVal); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function testSaveUpdate() |
303
|
|
|
{ |
304
|
|
|
// Prepare database |
305
|
|
|
$this->setupData(); |
306
|
|
|
|
307
|
|
|
// Simple save |
308
|
|
|
$testDBO = $this->getDBOject(); |
309
|
|
|
$testDBO->id = 55; |
|
|
|
|
310
|
|
|
$testDBO->name = 'Steve'; |
|
|
|
|
311
|
|
|
$testDBO->date_added = '2019-01-27'; |
|
|
|
|
312
|
|
|
$testDBO->save(); |
313
|
|
|
|
314
|
|
|
$loaded = $this->getDBOject(); |
315
|
|
|
$retVal = $loaded->load(55); |
316
|
|
|
$this->assertTrue($testDBO->isLoaded()); |
317
|
|
|
$this->assertEquals(55, $loaded->id); |
|
|
|
|
318
|
|
|
$this->assertEquals('Steve', $loaded->name); |
|
|
|
|
319
|
|
|
$this->assertInstanceOf('\Suricate\DBObject', $retVal); |
320
|
|
|
|
321
|
|
|
// Update |
322
|
|
|
$loaded->name = 'Tim'; |
323
|
|
|
$loaded->save(); |
324
|
|
|
$loaded = $this->getDBOject(); |
325
|
|
|
$retVal = $loaded->load(55); |
326
|
|
|
$this->assertTrue($testDBO->isLoaded()); |
327
|
|
|
$this->assertEquals(55, $loaded->id); |
328
|
|
|
$this->assertEquals('Tim', $loaded->name); |
329
|
|
|
$this->assertInstanceOf('\Suricate\DBObject', $retVal); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
public function testForceInsert() |
333
|
|
|
{ |
334
|
|
|
// Prepare database |
335
|
|
|
$this->setupData(); |
336
|
|
|
|
337
|
|
|
// Force insert |
338
|
|
|
$loadedForce = $this->getDBOject(); |
339
|
|
|
$retVal = $loadedForce->load(1); |
|
|
|
|
340
|
|
|
$loadedForce->id = 56; |
|
|
|
|
341
|
|
|
$loadedForce->save(true); |
342
|
|
|
|
343
|
|
|
$loaded = $this->getDBOject(); |
344
|
|
|
$retVal = $loaded->load(56); |
345
|
|
|
|
346
|
|
|
$this->assertEquals(56, $loaded->id); |
|
|
|
|
347
|
|
|
$this->assertEquals('John', $loaded->name); |
|
|
|
|
348
|
|
|
$this->assertInstanceOf('\Suricate\DBObject', $retVal); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function testLoadFromSQL() |
352
|
|
|
{ |
353
|
|
|
// Prepare database |
354
|
|
|
$this->setupData(); |
355
|
|
|
|
356
|
|
|
// Inject database handler |
357
|
|
|
$testDBO = $this->getDBOject(); |
358
|
|
|
|
359
|
|
|
$sql = "SELECT * FROM `users` WHERE id=:id"; |
360
|
|
|
$params = ['id' => 1]; |
361
|
|
|
|
362
|
|
|
$retVal = $testDBO->loadFromSql($sql, $params); |
363
|
|
|
$this->assertInstanceOf('\Suricate\DBObject', $retVal); |
364
|
|
|
$this->assertTrue($testDBO->isLoaded()); |
365
|
|
|
|
366
|
|
|
$params = ['id' => 100]; |
367
|
|
|
$retVal = $testDBO->loadFromSql($sql, $params); |
368
|
|
|
$this->assertFalse($retVal); |
369
|
|
|
$this->assertFalse($testDBO->isLoaded()); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
public function testLoadOrFail() |
373
|
|
|
{ |
374
|
|
|
// Prepare database |
375
|
|
|
$this->setupData(); |
376
|
|
|
|
377
|
|
|
// Inject database handler |
378
|
|
|
$testDBO = $this->getDBOject(); |
379
|
|
|
|
380
|
|
|
|
381
|
|
|
$retVal = $testDBO->loadOrFail(1); |
382
|
|
|
$this->assertInstanceOf('\Suricate\DBObject', $retVal); |
383
|
|
|
|
384
|
|
|
$this->expectException(\Suricate\Exception\ModelNotFoundException::class); |
385
|
|
|
$testDBO->loadOrFail(100); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
public function testLoadOrInstanciate() |
389
|
|
|
{ |
390
|
|
|
// Prepare database |
391
|
|
|
$this->setupData(); |
392
|
|
|
|
393
|
|
|
$testDBO = Category::loadOrInstanciate(100); |
394
|
|
|
|
395
|
|
|
$comparison = $this->getCategoryDBOject(); |
396
|
|
|
$comparison->load(100); |
397
|
|
|
|
398
|
|
|
$this->assertInstanceOf('\Suricate\DBObject', $testDBO); |
399
|
|
|
$this->assertInstanceOf('Category', $testDBO); |
400
|
|
|
|
401
|
|
|
$this->assertSame($comparison->id, $testDBO->id); |
|
|
|
|
402
|
|
|
$this->assertSame($comparison->name, $testDBO->name); |
|
|
|
|
403
|
|
|
$this->assertTrue($testDBO->isLoaded()); |
404
|
|
|
|
405
|
|
|
// non existing |
406
|
|
|
$testDBO = Category::loadOrInstanciate(102); |
407
|
|
|
$this->assertFalse($testDBO->isLoaded()); // has been instanciated, not loaded |
408
|
|
|
$this->assertSame($testDBO->id, "102"); |
409
|
|
|
$this->assertSame($testDBO->name, null); |
410
|
|
|
|
411
|
|
|
$testDBO = Category::loadOrInstanciate(['id' => 102, 'name' => 'test name']); |
412
|
|
|
$this->assertFalse($testDBO->isLoaded()); |
413
|
|
|
$this->assertSame($testDBO->id, "102"); |
414
|
|
|
$this->assertSame($testDBO->name, 'test name'); |
415
|
|
|
|
416
|
|
|
$testDBO = Category::loadOrInstanciate(['id' => 101, 'name' => 'test name']); |
417
|
|
|
$this->assertFalse($testDBO->isLoaded()); |
418
|
|
|
$this->assertSame($testDBO->id, "101"); |
419
|
|
|
$this->assertSame($testDBO->name, 'test name'); |
420
|
|
|
|
421
|
|
|
$testDBO = Category::loadOrInstanciate(['id' => 101, 'name' => 'Employee']); |
422
|
|
|
$this->assertTrue($testDBO->isLoaded()); |
423
|
|
|
$this->assertSame($testDBO->id, "101"); |
424
|
|
|
$this->assertSame($testDBO->name, 'Employee'); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
public function testCreate() |
428
|
|
|
{ |
429
|
|
|
// Prepare database |
430
|
|
|
$this->setupData(); |
431
|
|
|
$comparison = $this->getCategoryDBOject(); |
432
|
|
|
|
433
|
|
|
$testDBO = Category::create(['id' => 1020]); |
434
|
|
|
|
435
|
|
|
$this->assertInstanceOf('\Suricate\DBObject', $testDBO); |
436
|
|
|
$this->assertTrue($testDBO->isLoaded()); |
437
|
|
|
|
438
|
|
|
$comparison->load(1020); |
439
|
|
|
|
440
|
|
|
$this->assertSame($comparison->id, $testDBO->id); |
|
|
|
|
441
|
|
|
$this->assertSame($comparison->name, null); |
|
|
|
|
442
|
|
|
|
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
|
446
|
|
|
public function testToArray() |
447
|
|
|
{ |
448
|
|
|
// Prepare database |
449
|
|
|
$this->setupData(); |
450
|
|
|
|
451
|
|
|
// Inject database handler |
452
|
|
|
$testDBO = $this->getDBOject(); |
453
|
|
|
$testDBO->load(2); |
454
|
|
|
|
455
|
|
|
$this->assertSame([ |
456
|
|
|
'id' => '2', |
457
|
|
|
'category_id' => '100', |
458
|
|
|
'name' => 'Paul', |
459
|
|
|
'date_added' => '2019-01-11 00:00:00', |
460
|
|
|
], |
461
|
|
|
$testDBO->toArray() |
462
|
|
|
); |
463
|
|
|
|
464
|
|
|
$testDBO = $this->getDBOject(); |
465
|
|
|
$testDBO->load(2); |
466
|
|
|
self::mockProperty($testDBO, 'exportedVariables', [ |
467
|
|
|
'id' => 'id', |
468
|
|
|
'category_id' => 'category_id,type:integer', |
469
|
|
|
'name' => ',omitempty', |
470
|
|
|
'date_added' => '-'] |
471
|
|
|
); |
472
|
|
|
$testDBO->name = ''; |
|
|
|
|
473
|
|
|
|
474
|
|
|
$this->assertSame([ |
475
|
|
|
'id' => '2', |
476
|
|
|
'category_id' => 100, |
477
|
|
|
], |
478
|
|
|
$testDBO->toArray() |
479
|
|
|
); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
public function testToJson() |
483
|
|
|
{ |
484
|
|
|
// Prepare database |
485
|
|
|
$this->setupData(); |
486
|
|
|
|
487
|
|
|
// Inject database handler |
488
|
|
|
$testDBO = $this->getDBOject(); |
489
|
|
|
$testDBO->load(2); |
490
|
|
|
|
491
|
|
|
$this->assertSame( |
492
|
|
|
'{"id":"2","category_id":"100","name":"Paul","date_added":"2019-01-11 00:00:00"}', |
493
|
|
|
$testDBO->toJson() |
494
|
|
|
); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
public function testValidate() |
498
|
|
|
{ |
499
|
|
|
$testDBO = $this->getDBOject(); |
500
|
|
|
$this->assertTrue($testDBO->validate()); |
501
|
|
|
|
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
public static function mockProperty($object, string $propertyName, $value) |
505
|
|
|
{ |
506
|
|
|
$reflectionClass = new \ReflectionClass($object); |
507
|
|
|
|
508
|
|
|
$property = $reflectionClass->getProperty($propertyName); |
509
|
|
|
$property->setAccessible(true); |
510
|
|
|
$property->setValue($object, $value); |
511
|
|
|
$property->setAccessible(false); |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
protected function setupData() |
515
|
|
|
{ |
516
|
|
|
$pdo = new PDO('sqlite:/tmp/test.db'); |
517
|
|
|
$pdo->exec("DROP TABLE IF EXISTS `users`"); |
518
|
|
|
$pdo->exec("DROP TABLE IF EXISTS `categories`"); |
519
|
|
|
$pdo->exec("CREATE TABLE `users` (`id` INTEGER PRIMARY KEY,`category_id` INTEGER, `name` varchar(50) DEFAULT NULL,`date_added` datetime NOT NULL)"); |
520
|
|
|
$pdo->exec("CREATE TABLE `categories` (`id` INTEGER PRIMARY KEY, `name` varchar(50) DEFAULT NULL, `parent_id` INTEGER DEFAULT NULL)"); |
521
|
|
|
|
522
|
|
|
$stmt = $pdo->prepare("INSERT INTO `users` (name, category_id, date_added) VALUES (:name, :categoryid, :date)"); |
523
|
|
|
$values = [ |
524
|
|
|
['John', 100, '2019-01-10 00:00:00'], |
525
|
|
|
['Paul', 100, '2019-01-11 00:00:00'], |
526
|
|
|
['Robert', 101, '2019-01-12 00:00:00'] |
527
|
|
|
]; |
528
|
|
|
foreach ($values as $value) { |
529
|
|
|
$stmt->execute(['name' => $value[0], 'categoryid' => $value[1], 'date' => $value[2]]); |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
$stmt = $pdo->prepare("INSERT INTO `categories` (id, name) VALUES (:id, :name)"); |
533
|
|
|
$values = [ |
534
|
|
|
[100, 'Admin'], |
535
|
|
|
[101, 'Employee'] |
536
|
|
|
]; |
537
|
|
|
foreach ($values as $value) { |
538
|
|
|
$stmt->execute(['id' => $value[0], 'name' => $value[1]]); |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
protected function getDatabase() |
543
|
|
|
{ |
544
|
|
|
$database = new \Suricate\Database(); |
545
|
|
|
$database->configure([ |
546
|
|
|
'type' => 'sqlite', |
547
|
|
|
'file' => '/tmp/test.db', |
548
|
|
|
]); |
549
|
|
|
|
550
|
|
|
return $database; |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
protected function getDBOject() |
554
|
|
|
{ |
555
|
|
|
$dbLink = $this->getDatabase(); |
556
|
|
|
// Inject database handler |
557
|
|
|
$testDBO = new \Suricate\DBObject(); |
558
|
|
|
|
559
|
|
|
|
560
|
|
|
$reflector = new ReflectionClass(get_class($testDBO)); |
561
|
|
|
$property = $reflector->getProperty('dbLink'); |
562
|
|
|
$property->setAccessible(true); |
563
|
|
|
$property->setValue($testDBO, $dbLink); |
564
|
|
|
|
565
|
|
|
self::mockProperty($testDBO, 'tableName', $this->tableName); |
566
|
|
|
self::mockProperty($testDBO, 'tableIndex', 'id'); |
567
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'category_id', 'name', 'date_added']); |
568
|
|
|
|
569
|
|
|
return $testDBO; |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
protected function getCategoryDBOject() |
573
|
|
|
{ |
574
|
|
|
$dbLink = $this->getDatabase(); |
575
|
|
|
// Inject database handler |
576
|
|
|
$testDBO = new \Suricate\DBObject(); |
577
|
|
|
|
578
|
|
|
|
579
|
|
|
$reflector = new ReflectionClass(get_class($testDBO)); |
580
|
|
|
$property = $reflector->getProperty('dbLink'); |
581
|
|
|
$property->setAccessible(true); |
582
|
|
|
$property->setValue($testDBO, $dbLink); |
583
|
|
|
|
584
|
|
|
self::mockProperty($testDBO, 'tableName', 'categories'); |
585
|
|
|
self::mockProperty($testDBO, 'tableIndex', 'id'); |
586
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id','name']); |
587
|
|
|
|
588
|
|
|
return $testDBO; |
589
|
|
|
} |
590
|
|
|
} |
591
|
|
|
|