|
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 testUndefinedGet() |
|
43
|
|
|
{ |
|
44
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
45
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'last_update']); |
|
46
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
47
|
|
|
|
|
48
|
|
|
$testDBO->undefinedVar; |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testDBProperty() |
|
52
|
|
|
{ |
|
53
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
54
|
|
|
$testDBO->regularProperty = 42; |
|
|
|
|
|
|
55
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'not_loaded_var']); |
|
56
|
|
|
self::mockProperty($testDBO, 'dbValues', ['id' => 1, 'name' => 'test name']); |
|
57
|
|
|
$this->assertEquals($testDBO->id, 1); |
|
|
|
|
|
|
58
|
|
|
$this->assertNotEquals($testDBO->name, 'test name edited'); |
|
|
|
|
|
|
59
|
|
|
$this->assertNull($testDBO->not_loaded_var); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
$this->assertTrue($testDBO->isDBVariable('id')); |
|
62
|
|
|
$this->assertFalse($testDBO->isDBVariable('regularProperty')); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertTrue($testDBO->propertyExists('regularProperty')); |
|
65
|
|
|
$this->assertTrue($testDBO->propertyExists('id')); |
|
66
|
|
|
$this->assertFalse($testDBO->propertyExists('unknownProperty')); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function testIsset() |
|
70
|
|
|
{ |
|
71
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
72
|
|
|
self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'not_loaded_var']); |
|
73
|
|
|
self::mockProperty($testDBO, 'dbValues', ['id' => 1, 'name' => 'test name']); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertTrue(isset($testDBO->id)); |
|
|
|
|
|
|
76
|
|
|
$this->assertFalse(isset($testDBO->undefVar)); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function testIsLoaded() |
|
80
|
|
|
{ |
|
81
|
|
|
$testIndex = 'id'; |
|
82
|
|
|
|
|
83
|
|
|
$testDBO = new \Suricate\DBObject(); |
|
84
|
|
|
self::mockProperty($testDBO, 'tableIndex', $testIndex); |
|
85
|
|
|
self::mockProperty($testDBO, 'dbVariables', [$testIndex, 'name', 'not_loaded_var']); |
|
86
|
|
|
$this->assertFalse($testDBO->isLoaded()); |
|
87
|
|
|
|
|
88
|
|
|
self::mockProperty($testDBO, 'dbValues', [$testIndex => 1, 'name' => 'test name']); |
|
89
|
|
|
$this->assertTrue($testDBO->isLoaded()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
public static function mockProperty($object, string $propertyName, $value) |
|
94
|
|
|
{ |
|
95
|
|
|
$reflectionClass = new \ReflectionClass($object); |
|
96
|
|
|
|
|
97
|
|
|
$property = $reflectionClass->getProperty($propertyName); |
|
98
|
|
|
$property->setAccessible(true); |
|
99
|
|
|
$property->setValue($object, $value); |
|
100
|
|
|
$property->setAccessible(false); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|