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