|
1
|
|
|
<?php |
|
2
|
|
|
class DBObjectTest extends \PHPUnit\Framework\TestCase |
|
3
|
|
|
{ |
|
4
|
|
|
protected $tableName = 'users'; |
|
5
|
|
|
public function testContructor() |
|
6
|
|
|
{ |
|
7
|
|
|
$classname = '\Suricate\DBObject'; |
|
8
|
|
|
|
|
9
|
|
|
// Get mock, without the constructor being called |
|
10
|
|
|
$mock = $this->getMockBuilder($classname) |
|
11
|
|
|
->disableOriginalConstructor() |
|
12
|
|
|
->setMethods(array('setRelations')) |
|
13
|
|
|
->getMockForAbstractClass(); |
|
14
|
|
|
|
|
15
|
|
|
// set expectations for constructor calls |
|
16
|
|
|
$mock->expects($this->once()) |
|
17
|
|
|
->method('setRelations'); |
|
18
|
|
|
|
|
19
|
|
|
// now call the constructor |
|
20
|
|
|
$reflectedClass = new ReflectionClass($classname); |
|
21
|
|
|
$constructor = $reflectedClass->getConstructor(); |
|
22
|
|
|
$constructor->invoke($mock); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function testGetTableName() |
|
26
|
|
|
{ |
|
27
|
|
|
$testName = 'my_sql_table'; |
|
28
|
|
|
|
|
29
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
30
|
|
|
self::mockProperty($testDBO, 'tableName', $testName); |
|
31
|
|
|
$this->assertEquals($testName, $testDBO->getTableName()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function testGetTableIndex() |
|
35
|
|
|
{ |
|
36
|
|
|
$testIndex = 'id'; |
|
37
|
|
|
|
|
38
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
39
|
|
|
self::mockProperty($testDBO, 'tableIndex', $testIndex); |
|
40
|
|
|
$this->assertEquals($testIndex, $testDBO->getTableIndex()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testGetDBConfig() |
|
44
|
|
|
{ |
|
45
|
|
|
$testConfigName = 'my_config'; |
|
46
|
|
|
|
|
47
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
48
|
|
|
self::mockProperty($testDBO, 'DBConfig', $testConfigName); |
|
49
|
|
|
$this->assertEquals($testConfigName, $testDBO->getDBConfig()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testUndefinedGet() |
|
53
|
|
|
{ |
|
54
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
55
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'last_update']); |
|
56
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
57
|
|
|
|
|
58
|
|
|
$testDBO->undefinedVar; |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testDBProperty() |
|
62
|
|
|
{ |
|
63
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
64
|
|
|
$testDBO->regularProperty = 42; |
|
|
|
|
|
|
65
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'not_loaded_var']); |
|
66
|
|
|
self::mockProperty($testDBO, 'dbValues', ['id' => 1, 'name' => 'test name']); |
|
67
|
|
|
$this->assertEquals($testDBO->id, 1); |
|
|
|
|
|
|
68
|
|
|
$this->assertNotEquals($testDBO->name, 'test name edited'); |
|
|
|
|
|
|
69
|
|
|
$this->assertNull($testDBO->not_loaded_var); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
$this->assertTrue($testDBO->isDBVariable('id')); |
|
72
|
|
|
$this->assertFalse($testDBO->isDBVariable('regularProperty')); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertTrue($testDBO->propertyExists('regularProperty')); |
|
75
|
|
|
$this->assertTrue($testDBO->propertyExists('id')); |
|
76
|
|
|
$this->assertFalse($testDBO->propertyExists('unknownProperty')); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testIsset() |
|
80
|
|
|
{ |
|
81
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
82
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'not_loaded_var']); |
|
83
|
|
|
self::mockProperty($testDBO, 'dbValues', ['id' => 1, 'name' => 'test name']); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertTrue(isset($testDBO->id)); |
|
|
|
|
|
|
86
|
|
|
$this->assertFalse(isset($testDBO->undefVar)); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function testIsLoaded() |
|
90
|
|
|
{ |
|
91
|
|
|
$testIndex = 'id'; |
|
92
|
|
|
|
|
93
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
94
|
|
|
self::mockProperty($testDBO, 'tableIndex', $testIndex); |
|
95
|
|
|
self::mockProperty($testDBO, 'dbVariables', [$testIndex, 'name', 'not_loaded_var']); |
|
96
|
|
|
$this->assertFalse($testDBO->isLoaded()); |
|
97
|
|
|
|
|
98
|
|
|
self::mockProperty($testDBO, 'dbValues', [$testIndex => 1, 'name' => 'test name']); |
|
99
|
|
|
$this->assertTrue($testDBO->isLoaded()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testHydrate() |
|
103
|
|
|
{ |
|
104
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
105
|
|
|
$testDBO->realProperty = ''; |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name']); |
|
108
|
|
|
$testDBO->hydrate([ |
|
109
|
|
|
'id' => 1, |
|
110
|
|
|
'name' => 'test record', |
|
111
|
|
|
'add_column' => 'test value', |
|
112
|
|
|
'realProperty' => 'my string', |
|
113
|
|
|
]); |
|
114
|
|
|
|
|
115
|
|
|
$this->assertEquals($testDBO->realProperty, 'my string'); |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
$reflector = new ReflectionClass(get_class($testDBO)); |
|
118
|
|
|
$property = $reflector->getProperty('dbValues'); |
|
119
|
|
|
$property->setAccessible(true); |
|
120
|
|
|
$this->assertEquals([ |
|
121
|
|
|
'id' => 1, |
|
122
|
|
|
'name' => 'test record', |
|
123
|
|
|
], $property->getValue($testDBO)); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testLoad() |
|
127
|
|
|
{ |
|
128
|
|
|
// Prepare database |
|
129
|
|
|
$this->setupData(); |
|
130
|
|
|
$dbLink = $this->getDatabase(); |
|
131
|
|
|
|
|
132
|
|
|
// Inject database handler |
|
133
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
$reflector = new ReflectionClass(get_class($testDBO)); |
|
137
|
|
|
$property = $reflector->getProperty('dbLink'); |
|
138
|
|
|
$property->setAccessible(true); |
|
139
|
|
|
$property->setValue($testDBO, $dbLink); |
|
140
|
|
|
|
|
141
|
|
|
self::mockProperty($testDBO, 'tableName', $this->tableName); |
|
142
|
|
|
self::mockProperty($testDBO, 'tableIndex', 'id'); |
|
143
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'date_added']); |
|
144
|
|
|
|
|
145
|
|
|
$this->assertFalse($testDBO->isLoaded()); |
|
146
|
|
|
$retVal = $testDBO->load(1); |
|
147
|
|
|
$this->assertTrue($testDBO->isLoaded()); |
|
148
|
|
|
$this->assertEquals(1, $testDBO->id); |
|
|
|
|
|
|
149
|
|
|
$this->assertEquals('John', $testDBO->name); |
|
|
|
|
|
|
150
|
|
|
$this->assertInstanceOf('\Suricate\DBObject', $retVal); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
public static function mockProperty($object, string $propertyName, $value) |
|
155
|
|
|
{ |
|
156
|
|
|
$reflectionClass = new \ReflectionClass($object); |
|
157
|
|
|
|
|
158
|
|
|
$property = $reflectionClass->getProperty($propertyName); |
|
159
|
|
|
$property->setAccessible(true); |
|
160
|
|
|
$property->setValue($object, $value); |
|
161
|
|
|
$property->setAccessible(false); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
protected function setupData() |
|
165
|
|
|
{ |
|
166
|
|
|
$pdo = new PDO('sqlite:/tmp/test.db'); |
|
167
|
|
|
$pdo->exec("DROP TABLE IF EXISTS `" . $this->tableName ."`"); |
|
168
|
|
|
$pdo->exec("CREATE TABLE `" .$this->tableName. "` (`id` INTEGER PRIMARY KEY,`name` varchar(50) DEFAULT NULL,`date_added` datetime NOT NULL)"); |
|
169
|
|
|
$stmt = $pdo->prepare("INSERT INTO `" . $this->tableName . "` (name, date_added) VALUES (:name, :date)"); |
|
170
|
|
|
$values = [ |
|
171
|
|
|
['John', '2019-01-10 00:00:00'], |
|
172
|
|
|
['Paul', '2019-01-11 00:00:00'], |
|
173
|
|
|
['Robert', '2019-01-12 00:00:00'] |
|
174
|
|
|
]; |
|
175
|
|
|
foreach ($values as $value) { |
|
176
|
|
|
$stmt->execute(['name' => $value[0], 'date' => $value[1]]); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
protected function getDatabase() |
|
181
|
|
|
{ |
|
182
|
|
|
$database = new \Suricate\Database(); |
|
183
|
|
|
$database->configure([ |
|
184
|
|
|
'type' => 'sqlite', |
|
185
|
|
|
'file' => '/tmp/test.db', |
|
186
|
|
|
]); |
|
187
|
|
|
|
|
188
|
|
|
return $database; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|