|
1
|
|
|
<?php |
|
2
|
|
|
class DBObjectTest extends \PHPUnit\Framework\TestCase |
|
3
|
|
|
{ |
|
4
|
|
|
public function testContructor() |
|
5
|
|
|
{ |
|
6
|
|
|
$classname = '\Suricate\DBObject'; |
|
7
|
|
|
|
|
8
|
|
|
// Get mock, without the constructor being called |
|
9
|
|
|
$mock = $this->getMockBuilder($classname) |
|
10
|
|
|
->disableOriginalConstructor() |
|
11
|
|
|
->setMethods(array('setRelations')) |
|
12
|
|
|
->getMockForAbstractClass(); |
|
13
|
|
|
|
|
14
|
|
|
// set expectations for constructor calls |
|
15
|
|
|
$mock->expects($this->once()) |
|
16
|
|
|
->method('setRelations'); |
|
17
|
|
|
|
|
18
|
|
|
// now call the constructor |
|
19
|
|
|
$reflectedClass = new ReflectionClass($classname); |
|
20
|
|
|
$constructor = $reflectedClass->getConstructor(); |
|
21
|
|
|
$constructor->invoke($mock); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function testGetTableName() |
|
25
|
|
|
{ |
|
26
|
|
|
$testName = 'my_sql_table'; |
|
27
|
|
|
|
|
28
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
29
|
|
|
self::mockProperty($testDBO, 'tableName', $testName); |
|
30
|
|
|
$this->assertEquals($testName, $testDBO->getTableName()); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testGetTableIndex() |
|
34
|
|
|
{ |
|
35
|
|
|
$testIndex = 'id'; |
|
36
|
|
|
|
|
37
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
38
|
|
|
self::mockProperty($testDBO, 'tableIndex', $testIndex); |
|
39
|
|
|
$this->assertEquals($testIndex, $testDBO->getTableIndex()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testGetDBConfig() |
|
43
|
|
|
{ |
|
44
|
|
|
$testConfigName = 'my_config'; |
|
45
|
|
|
|
|
46
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
47
|
|
|
self::mockProperty($testDBO, 'DBConfig', $testConfigName); |
|
48
|
|
|
$this->assertEquals($testConfigName, $testDBO->getDBConfig()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testUndefinedGet() |
|
52
|
|
|
{ |
|
53
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
54
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'last_update']); |
|
55
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
56
|
|
|
|
|
57
|
|
|
$testDBO->undefinedVar; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function testDBProperty() |
|
61
|
|
|
{ |
|
62
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
63
|
|
|
$testDBO->regularProperty = 42; |
|
|
|
|
|
|
64
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'not_loaded_var']); |
|
65
|
|
|
self::mockProperty($testDBO, 'dbValues', ['id' => 1, 'name' => 'test name']); |
|
66
|
|
|
$this->assertEquals($testDBO->id, 1); |
|
|
|
|
|
|
67
|
|
|
$this->assertNotEquals($testDBO->name, 'test name edited'); |
|
|
|
|
|
|
68
|
|
|
$this->assertNull($testDBO->not_loaded_var); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$this->assertTrue($testDBO->isDBVariable('id')); |
|
71
|
|
|
$this->assertFalse($testDBO->isDBVariable('regularProperty')); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertTrue($testDBO->propertyExists('regularProperty')); |
|
74
|
|
|
$this->assertTrue($testDBO->propertyExists('id')); |
|
75
|
|
|
$this->assertFalse($testDBO->propertyExists('unknownProperty')); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testIsset() |
|
79
|
|
|
{ |
|
80
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
81
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'not_loaded_var']); |
|
82
|
|
|
self::mockProperty($testDBO, 'dbValues', ['id' => 1, 'name' => 'test name']); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertTrue(isset($testDBO->id)); |
|
|
|
|
|
|
85
|
|
|
$this->assertFalse(isset($testDBO->undefVar)); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testIsLoaded() |
|
89
|
|
|
{ |
|
90
|
|
|
$testIndex = 'id'; |
|
91
|
|
|
|
|
92
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
93
|
|
|
self::mockProperty($testDBO, 'tableIndex', $testIndex); |
|
94
|
|
|
self::mockProperty($testDBO, 'dbVariables', [$testIndex, 'name', 'not_loaded_var']); |
|
95
|
|
|
$this->assertFalse($testDBO->isLoaded()); |
|
96
|
|
|
|
|
97
|
|
|
self::mockProperty($testDBO, 'dbValues', [$testIndex => 1, 'name' => 'test name']); |
|
98
|
|
|
$this->assertTrue($testDBO->isLoaded()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testHydrate() |
|
102
|
|
|
{ |
|
103
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
104
|
|
|
$testDBO->realProperty = ''; |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name']); |
|
107
|
|
|
$testDBO->hydrate([ |
|
108
|
|
|
'id' => 1, |
|
109
|
|
|
'name' => 'test record', |
|
110
|
|
|
'add_column' => 'test value', |
|
111
|
|
|
'realProperty' => 'my string', |
|
112
|
|
|
]); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertEquals($testDBO->realProperty, 'my string'); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
$reflector = new ReflectionClass(get_class($testDBO)); |
|
117
|
|
|
$property = $reflector->getProperty('dbValues'); |
|
118
|
|
|
$property->setAccessible(true); |
|
119
|
|
|
$this->assertEquals([ |
|
120
|
|
|
'id' => 1, |
|
121
|
|
|
'name' => 'test record', |
|
122
|
|
|
], $property->getValue($testDBO)); |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
public static function mockProperty($object, string $propertyName, $value) |
|
128
|
|
|
{ |
|
129
|
|
|
$reflectionClass = new \ReflectionClass($object); |
|
130
|
|
|
|
|
131
|
|
|
$property = $reflectionClass->getProperty($propertyName); |
|
132
|
|
|
$property->setAccessible(true); |
|
133
|
|
|
$property->setValue($object, $value); |
|
134
|
|
|
$property->setAccessible(false); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|