Completed
Push — develop ( 0c4bef...dc46fb )
by Mathieu
01:41
created

DBObjectTest::testDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 13
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 21
rs 9.8333
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;
0 ignored issues
show
Bug Best Practice introduced by
The property undefinedVar does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
64
    }
65
66
    public function testDBProperty()
67
    {
68
        $testDBO = new \Suricate\DBObject();
69
        $testDBO->regularProperty = 42;
0 ignored issues
show
Bug Best Practice introduced by
The property regularProperty does not exist on Suricate\DBObject. Since you implemented __set, consider adding a @property annotation.
Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
73
        $this->assertNotEquals($testDBO->name, 'test name edited');
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
74
        $this->assertNull($testDBO->not_loaded_var);
0 ignored issues
show
Bug Best Practice introduced by
The property not_loaded_var does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
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));
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
91
        $this->assertFalse(isset($testDBO->undefVar));
0 ignored issues
show
Bug Best Practice introduced by
The property undefVar does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The property unloadable does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The property prot_var does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
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 = '';
0 ignored issues
show
Bug Best Practice introduced by
The property realProperty does not exist on Suricate\DBObject. Since you implemented __set, consider adding a @property annotation.
Loading history...
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');
0 ignored issues
show
Bug Best Practice introduced by
The property realProperty does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
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();
0 ignored issues
show
Bug introduced by
The method __wakeup() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

201
        $mock->/** @scrutinizer ignore-call */ 
202
               __wakeup();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The property category does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
296
297
        $this->assertEquals('John', $testDBO->name);
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Suricate\DBObject. Since you implemented __set, consider adding a @property annotation.
Loading history...
310
        $testDBO->name = 'Steve';
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Suricate\DBObject. Since you implemented __set, consider adding a @property annotation.
Loading history...
311
        $testDBO->date_added = '2019-01-27';
0 ignored issues
show
Bug Best Practice introduced by
The property date_added does not exist on Suricate\DBObject. Since you implemented __set, consider adding a @property annotation.
Loading history...
312
        $testDBO->save();
313
314
        $loaded = $this->getDBOject();
315
        $retVal = $loaded->load(55);
316
        $this->assertTrue($testDBO->isLoaded());
317
        $this->assertEquals(55, $loaded->id);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
318
        $this->assertEquals('Steve', $loaded->name);
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $retVal is dead and can be removed.
Loading history...
340
        $loadedForce->id = 56;
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Suricate\DBObject. Since you implemented __set, consider adding a @property annotation.
Loading history...
341
        $loadedForce->save(true);
342
343
        $loaded = $this->getDBOject();
344
        $retVal = $loaded->load(56);
345
346
        $this->assertEquals(56, $loaded->id);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
347
        $this->assertEquals('John', $loaded->name);
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
348
        $this->assertInstanceOf('\Suricate\DBObject', $retVal);
349
    }
350
351
    public function testDelete()
352
    {
353
        // Prepare database
354
        $this->setupData();
355
356
        // Simple save
357
        $testDBO = $this->getDBOject();
358
        $testDBO->id = 55;
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Suricate\DBObject. Since you implemented __set, consider adding a @property annotation.
Loading history...
359
        $testDBO->name = 'Steve';
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Suricate\DBObject. Since you implemented __set, consider adding a @property annotation.
Loading history...
360
        $testDBO->date_added = '2019-01-27';
0 ignored issues
show
Bug Best Practice introduced by
The property date_added does not exist on Suricate\DBObject. Since you implemented __set, consider adding a @property annotation.
Loading history...
361
        $testDBO->save();
362
363
        $testDBO = $this->getDBOject();
364
        $retVal = $testDBO->loadOrFail(55);
365
        $this->assertInstanceOf('\Suricate\DBObject', $retVal);
366
367
        $testDBO->delete();
368
369
        $testDBO = $this->getDBOject();
370
        $this->expectException(\Suricate\Exception\ModelNotFoundException::class);
371
        $testDBO->loadOrFail(55);
372
    }
373
374
    public function testLoadFromSQL()
375
    {
376
        // Prepare database
377
        $this->setupData();
378
379
        // Inject database handler
380
        $testDBO = $this->getDBOject();
381
382
        $sql = "SELECT * FROM `users` WHERE id=:id";
383
        $params = ['id' => 1];
384
        
385
        $retVal = $testDBO->loadFromSql($sql, $params);
386
        $this->assertInstanceOf('\Suricate\DBObject', $retVal);
387
        $this->assertTrue($testDBO->isLoaded());
388
389
        $params = ['id' => 100];
390
        $retVal = $testDBO->loadFromSql($sql, $params);
391
        $this->assertFalse($retVal);
392
        $this->assertFalse($testDBO->isLoaded());
393
    }
394
395
    public function testLoadOrFail()
396
    {
397
        // Prepare database
398
        $this->setupData();
399
400
        // Inject database handler
401
        $testDBO = $this->getDBOject();
402
403
        
404
        $retVal = $testDBO->loadOrFail(1);
405
        $this->assertInstanceOf('\Suricate\DBObject', $retVal);
406
407
        $this->expectException(\Suricate\Exception\ModelNotFoundException::class);
408
        $testDBO->loadOrFail(100);
409
    }
410
411
    public function testLoadOrInstanciate()
412
    {
413
        // Prepare database
414
        $this->setupData();
415
416
        $testDBO = Category::loadOrInstanciate(100);
417
418
        $comparison = $this->getCategoryDBOject();
419
        $comparison->load(100);
420
421
        $this->assertInstanceOf('\Suricate\DBObject', $testDBO);
422
        $this->assertInstanceOf('Category', $testDBO);
423
424
        $this->assertSame($comparison->id, $testDBO->id);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
425
        $this->assertSame($comparison->name, $testDBO->name);
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
426
        $this->assertTrue($testDBO->isLoaded());
427
428
        // non existing
429
        $testDBO = Category::loadOrInstanciate(102);
430
        $this->assertFalse($testDBO->isLoaded()); // has been instanciated, not loaded
431
        $this->assertSame($testDBO->id, "102");
432
        $this->assertSame($testDBO->name, null);
433
434
        $testDBO = Category::loadOrInstanciate(['id' => 102, 'name' => 'test name']);
435
        $this->assertFalse($testDBO->isLoaded());
436
        $this->assertSame($testDBO->id, "102");
437
        $this->assertSame($testDBO->name, 'test name');
438
439
        $testDBO = Category::loadOrInstanciate(['id' => 101, 'name' => 'test name']);
440
        $this->assertFalse($testDBO->isLoaded());
441
        $this->assertSame($testDBO->id, "101");
442
        $this->assertSame($testDBO->name, 'test name');
443
444
        $testDBO = Category::loadOrInstanciate(['id' => 101, 'name' => 'Employee']);
445
        $this->assertTrue($testDBO->isLoaded());
446
        $this->assertSame($testDBO->id, "101");
447
        $this->assertSame($testDBO->name, 'Employee');
448
    }
449
450
    public function testCreate()
451
    {
452
        // Prepare database
453
        $this->setupData();
454
        $comparison = $this->getCategoryDBOject();
455
456
        $testDBO = Category::create(['id' => 1020]);
457
458
        $this->assertInstanceOf('\Suricate\DBObject', $testDBO);
459
        $this->assertTrue($testDBO->isLoaded());
460
461
        $comparison->load(1020);
462
463
        $this->assertSame($comparison->id, $testDBO->id);
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
464
        $this->assertSame($comparison->name, null);
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
465
        
466
    }
467
468
469
    public function testToArray()
470
    {
471
        // Prepare database
472
        $this->setupData();
473
474
        // Inject database handler
475
        $testDBO = $this->getDBOject();
476
        $testDBO->load(2);
477
        
478
        $this->assertSame([
479
            'id' => '2',
480
            'category_id' => '100',
481
            'name' => 'Paul',
482
            'date_added' => '2019-01-11 00:00:00',
483
            ],
484
            $testDBO->toArray()
485
        );
486
487
        $testDBO = $this->getDBOject();
488
        $testDBO->load(2);
489
        self::mockProperty($testDBO, 'exportedVariables', [
490
            'id' => 'id', 
491
            'category_id' => 'category_id,type:integer',
492
            'name' => ',omitempty',
493
            'date_added' => '-']
494
        );
495
        $testDBO->name = '';
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Suricate\DBObject. Since you implemented __set, consider adding a @property annotation.
Loading history...
496
497
        $this->assertSame([
498
            'id' => '2',
499
            'category_id' => 100,
500
            ],
501
            $testDBO->toArray()
502
        );
503
    }
504
505
    public function testToJson()
506
    {
507
        // Prepare database
508
        $this->setupData();
509
510
        // Inject database handler
511
        $testDBO = $this->getDBOject();
512
        $testDBO->load(2);
513
514
        $this->assertSame(
515
            '{"id":"2","category_id":"100","name":"Paul","date_added":"2019-01-11 00:00:00"}',
516
            $testDBO->toJson()
517
        );
518
    }
519
520
    public function testValidate()
521
    {
522
        $testDBO = $this->getDBOject();
523
        $this->assertTrue($testDBO->validate());
524
525
    }
526
527
    public static function mockProperty($object, string $propertyName, $value)
528
    {
529
        $reflectionClass = new \ReflectionClass($object);
530
531
        $property = $reflectionClass->getProperty($propertyName);
532
        $property->setAccessible(true);
533
        $property->setValue($object, $value);
534
        $property->setAccessible(false);
535
    }
536
537
    protected function setupData()
538
    {
539
        $pdo = new PDO('sqlite:/tmp/test.db');
540
        $pdo->exec("DROP TABLE IF EXISTS `users`");
541
        $pdo->exec("DROP TABLE IF EXISTS `categories`");
542
        $pdo->exec("CREATE TABLE `users` (`id` INTEGER PRIMARY KEY,`category_id` INTEGER, `name` varchar(50) DEFAULT NULL,`date_added` datetime NOT NULL)");
543
        $pdo->exec("CREATE TABLE `categories` (`id` INTEGER PRIMARY KEY, `name` varchar(50) DEFAULT NULL, `parent_id` INTEGER DEFAULT NULL)");
544
        
545
        $stmt = $pdo->prepare("INSERT INTO `users` (name, category_id, date_added) VALUES (:name, :categoryid, :date)");
546
        $values = [
547
            ['John', 100, '2019-01-10 00:00:00'],
548
            ['Paul', 100, '2019-01-11 00:00:00'],
549
            ['Robert', 101, '2019-01-12 00:00:00']
550
        ];
551
        foreach ($values as $value) {
552
            $stmt->execute(['name' => $value[0], 'categoryid' => $value[1], 'date' => $value[2]]);
553
        }
554
555
        $stmt = $pdo->prepare("INSERT INTO `categories` (id, name) VALUES (:id, :name)");
556
        $values = [
557
            [100, 'Admin'],
558
            [101, 'Employee']
559
        ];
560
        foreach ($values as $value) {
561
            $stmt->execute(['id' => $value[0], 'name' => $value[1]]);
562
        }
563
    }
564
565
    protected function getDatabase()
566
    {
567
        $database = new \Suricate\Database();
568
        $database->configure([
569
            'type' => 'sqlite',
570
            'file' => '/tmp/test.db',
571
        ]);
572
573
        return $database;
574
    }
575
576
    protected function getDBOject()
577
    {
578
        $dbLink = $this->getDatabase();
579
        // Inject database handler
580
        $testDBO = new \Suricate\DBObject();
581
582
583
        $reflector = new ReflectionClass(get_class($testDBO));
584
        $property = $reflector->getProperty('dbLink');
585
        $property->setAccessible(true);
586
        $property->setValue($testDBO, $dbLink);
587
588
        self::mockProperty($testDBO, 'tableName', $this->tableName);
589
        self::mockProperty($testDBO, 'tableIndex', 'id');
590
        self::mockProperty($testDBO, 'dbVariables', ['id', 'category_id', 'name', 'date_added']);
591
592
        return $testDBO;
593
    }
594
595
    protected function getCategoryDBOject()
596
    {
597
        $dbLink = $this->getDatabase();
598
        // Inject database handler
599
        $testDBO = new \Suricate\DBObject();
600
601
602
        $reflector = new ReflectionClass(get_class($testDBO));
603
        $property = $reflector->getProperty('dbLink');
604
        $property->setAccessible(true);
605
        $property->setValue($testDBO, $dbLink);
606
607
        self::mockProperty($testDBO, 'tableName', 'categories');
608
        self::mockProperty($testDBO, 'tableIndex', 'id');
609
        self::mockProperty($testDBO, 'dbVariables', ['id','name']);
610
611
        return $testDBO;
612
    }
613
}
614