Completed
Push — develop ( 1cf741...8fa47c )
by Mathieu
01:42
created

DBObjectTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 100
c 7
b 0
f 0
dl 0
loc 184
rs 10
wmc 14

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getDatabase() 0 9 1
A testGetTableName() 0 7 1
A testConnect() 0 21 1
A testContructor() 0 18 1
A testHydrate() 0 22 1
A testGetDBConfig() 0 7 1
A mockProperty() 0 8 1
A testGetTableIndex() 0 7 1
A setupData() 0 13 2
A testIsLoaded() 0 11 1
A testDBProperty() 0 16 1
A testUndefinedGet() 0 7 1
A testIsset() 0 8 1
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;
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...
59
    }
60
61
    public function testDBProperty()
62
    {
63
        $testDBO = new \Suricate\DBObject();
64
        $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...
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);
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...
68
        $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...
69
        $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...
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));
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...
86
        $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...
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 = '';
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...
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');
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...
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 testConnect()
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
        $testDBO->load(1);
146
        $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...
147
148
    }
149
    
150
151
    public static function mockProperty($object, string $propertyName, $value)
152
    {
153
        $reflectionClass = new \ReflectionClass($object);
154
155
        $property = $reflectionClass->getProperty($propertyName);
156
        $property->setAccessible(true);
157
        $property->setValue($object, $value);
158
        $property->setAccessible(false);
159
    }
160
161
    protected function setupData()
162
    {
163
        $pdo = new PDO('sqlite:/tmp/test.db');
164
        $pdo->exec("DROP TABLE IF EXISTS `" . $this->tableName ."`");
165
        $pdo->exec("CREATE TABLE `" .$this->tableName. "` (`id` INTEGER PRIMARY KEY,`name` varchar(50) DEFAULT NULL,`date_added` datetime NOT NULL)");
166
        $stmt = $pdo->prepare("INSERT INTO `" . $this->tableName . "` (name, date_added) VALUES (:name, :date)");
167
        $values = [
168
            ['John', '2019-01-10 00:00:00'],
169
            ['Paul', '2019-01-11 00:00:00'],
170
            ['Robert', '2019-01-12 00:00:00']
171
        ];
172
        foreach ($values as $value) {
173
            $stmt->execute(['name' => $value[0], 'date' => $value[1]]);
174
        }
175
    }
176
177
    protected function getDatabase()
178
    {
179
        $database = new \Suricate\Database();
180
        $database->configure([
181
            'type' => 'sqlite',
182
            'file' => '/tmp/test.db',
183
        ]);
184
185
        return $database;
186
    }
187
}
188