Passed
Push — develop ( dccc18...13c103 )
by Mathieu
01:46
created

DBObjectTest::testIsLoaded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 10
1
<?php
2
require_once 'stubs/Category.php';
3
4
class DBObjectTest extends \PHPUnit\Framework\TestCase
5
{
6
    protected $tableName = 'users';
7
    public function testContructor()
8
    {
9
        $classname = '\Suricate\DBObject';
10
11
        // Get mock, without the constructor being called
12
        $mock = $this->getMockBuilder($classname)
13
            ->disableOriginalConstructor()
14
            ->setMethods(array('setRelations'))
15
            ->getMockForAbstractClass();
16
17
        // set expectations for constructor calls
18
        $mock->expects($this->once())
19
            ->method('setRelations');
20
21
        // now call the constructor
22
        $reflectedClass = new ReflectionClass($classname);
23
        $constructor = $reflectedClass->getConstructor();
24
        $constructor->invoke($mock);
25
    }
26
27
    public function testGetTableName()
28
    {
29
        $testName = 'my_sql_table';
30
31
        $testDBO = new \Suricate\DBObject();
32
        self::mockProperty($testDBO, 'tableName', $testName);
33
        $this->assertEquals($testName, $testDBO->getTableName());
34
    }
35
36
    public function testGetTableIndex()
37
    {
38
        $testIndex = 'id';
39
40
        $testDBO = new \Suricate\DBObject();
41
        self::mockProperty($testDBO, 'tableIndex', $testIndex);
42
        $this->assertEquals($testIndex, $testDBO->getTableIndex());
43
    }
44
45
    public function testGetDBConfig()
46
    {
47
        $testConfigName = 'my_config';
48
49
        $testDBO = new \Suricate\DBObject();
50
        self::mockProperty($testDBO, 'DBConfig', $testConfigName);
51
        $this->assertEquals($testConfigName, $testDBO->getDBConfig());
52
    }
53
54
    public function testUndefinedGet()
55
    {
56
        $testDBO = new \Suricate\DBObject();
57
        self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'last_update']);
58
        $this->expectException(\InvalidArgumentException::class);
59
        
60
        $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...
61
    }
62
63
    public function testDBProperty()
64
    {
65
        $testDBO = new \Suricate\DBObject();
66
        $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...
67
        self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'not_loaded_var']);
68
        self::mockProperty($testDBO, 'dbValues', ['id' => 1, 'name' => 'test name']);
69
        $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...
70
        $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...
71
        $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...
72
73
        $this->assertTrue($testDBO->isDBVariable('id'));
74
        $this->assertFalse($testDBO->isDBVariable('regularProperty'));
75
76
        $this->assertTrue($testDBO->propertyExists('regularProperty'));
77
        $this->assertTrue($testDBO->propertyExists('id'));
78
        $this->assertFalse($testDBO->propertyExists('unknownProperty'));
79
    }
80
81
    public function testIsset()
82
    {
83
        $testDBO = new \Suricate\DBObject();
84
        self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'not_loaded_var']);
85
        self::mockProperty($testDBO, 'dbValues', ['id' => 1, 'name' => 'test name']);
86
87
        $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...
88
        $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...
89
    }
90
91
    public function testIsLoaded()
92
    {
93
        $testIndex = 'id';
94
95
        $testDBO = new \Suricate\DBObject();
96
        self::mockProperty($testDBO, 'tableIndex', $testIndex);
97
        self::mockProperty($testDBO, 'dbVariables', [$testIndex, 'name', 'not_loaded_var']);
98
        $this->assertFalse($testDBO->isLoaded());
99
100
        self::mockProperty($testDBO, 'dbValues', [$testIndex => 1, 'name' => 'test name']);
101
        $this->assertTrue($testDBO->isLoaded());
102
    }
103
104
    public function testInstanciate()
105
    {
106
        $testDBO = Category::instanciate([
107
            'id' => 1,
108
            'name' => 'test record',
109
        ]);
110
111
        $reflector = new ReflectionClass(Category::class);
112
        $property = $reflector->getProperty('dbValues');
113
        $property->setAccessible(true);
114
        $this->assertEquals([
115
            'id' => 1,
116
            'name' => 'test record',
117
        ], $property->getValue($testDBO));
118
    }
119
120
    public function testHydrate()
121
    {
122
        $testDBO = new \Suricate\DBObject();
123
        $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...
124
125
        self::mockProperty($testDBO, 'dbVariables', ['id', 'name']);
126
        $testDBO->hydrate([
127
            'id' => 1,
128
            'name' => 'test record',
129
            'add_column' => 'test value',
130
            'realProperty' => 'my string',
131
        ]);
132
133
        $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...
134
135
        $reflector = new ReflectionClass(get_class($testDBO));
136
        $property = $reflector->getProperty('dbValues');
137
        $property->setAccessible(true);
138
        $this->assertEquals([
139
            'id' => 1,
140
            'name' => 'test record',
141
        ], $property->getValue($testDBO));
142
    }
143
144
    public function testWakeup()
145
    {
146
        $mock = $this->getMockBuilder(\Suricate\DBObject::class)
147
            ->setMethods(['setRelations'])
148
            ->getMock();
149
150
        $mock
151
            ->expects($this->once())
152
            ->method('setRelations');
153
        
154
        $mock->__wakeup();
0 ignored issues
show
Bug introduced by
The method __wakeup() does not exist on PHPUnit\Framework\MockObject\MockObject. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

154
        $mock->/** @scrutinizer ignore-call */ 
155
               __wakeup();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
155
    }
156
157
    public function testRelations()
158
    {
159
        $relations = [
160
            'category' => [
161
                'type' => \Suricate\DBObject::RELATION_ONE_ONE,
162
                'source' => 'category_id',
163
                'target' => 'Category'
164
            ]
165
        ];
166
        // Prepare database
167
        $this->setupData();
168
        $mock = $this->getMockBuilder(\Suricate\DBObject::class)
169
            ->setMethods(['setRelations', 'getRelation'])
170
            ->getMock();
171
172
        // Prepare setup DBObject
173
        $testDBO = $this->getDBOject();
174
        $reflector = new ReflectionClass($mock);
175
        $property = $reflector->getProperty('relations');
176
        $property->setAccessible(true);
177
        $property->setValue($testDBO, $relations);
178
179
        // get relation values
180
        $reflector = new ReflectionClass($testDBO);
181
        $relationValuesRef = $reflector->getProperty('relationValues');
182
        $relationValuesRef->setAccessible(true);
183
184
        $loadedRelationsRef = $reflector->getProperty('loadedRelations');
185
        $loadedRelationsRef->setAccessible(true);
186
187
        // Load
188
        $testDBO->load(1);
189
        $relationsValues = $relationValuesRef->getValue($testDBO);
190
        $loadedRelations = $loadedRelationsRef->getValue($testDBO);
191
192
        // No relation values at first
193
        $this->assertSame([], $relationsValues);
194
        $this->assertSame([], $loadedRelations);
195
        $this->assertEquals('Admin', $testDBO->category->name);
0 ignored issues
show
Bug Best Practice introduced by
The property category does not exist on Suricate\DBObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
196
        $this->assertInstanceOf('\Suricate\DBObject', $testDBO->category);
197
198
        $relationsValues = $relationValuesRef->getValue($testDBO);
199
        $loadedRelations = $loadedRelationsRef->getValue($testDBO);
200
201
        // Check relation cache has been set
202
        $this->assertArrayHasKey('category', $relationsValues);
203
204
        // Check relation loaded flag has been set
205
        $this->assertArrayHasKey('category', $loadedRelations);
206
207
        // Check return type of relation
208
        $this->assertInstanceOf('\Suricate\DBObject', $relationsValues['category']);
209
210
    }
211
212
    public function testLoad()
213
    {
214
        // Prepare database
215
        $this->setupData();
216
217
        // Inject database handler
218
        $testDBO = $this->getDBOject();
219
220
        $this->assertFalse($testDBO->isLoaded());
221
        $retVal = $testDBO->load(1);
222
        $this->assertTrue($testDBO->isLoaded());
223
        $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...
224
225
        $this->assertEquals('John', $testDBO->name);
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...
226
        
227
        $this->assertInstanceOf('\Suricate\DBObject', $retVal);
228
    }
229
230
    public function testToArray()
231
    {
232
        // Prepare database
233
        $this->setupData();
234
235
        // Inject database handler
236
        $testDBO = $this->getDBOject();
237
        $testDBO->load(2);
238
        
239
        $this->assertSame([
240
            'id' => '2',
241
            'category_id' => '100',
242
            'name' => 'Paul',
243
            'date_added' => '2019-01-11 00:00:00',
244
            ],
245
            $testDBO->toArray()
246
        );
247
248
        $testDBO = $this->getDBOject();
249
        $testDBO->load(2);
250
        self::mockProperty($testDBO, 'exportedVariables', [
251
            'id' => 'id', 
252
            'category_id' => 'category_id,type:integer',
253
            'name' => 'name',
254
            'date_added' => '-']
255
        );
256
257
        $this->assertSame([
258
            'id' => '2',
259
            'category_id' => 100,
260
            'name' => 'Paul',
261
            ],
262
            $testDBO->toArray()
263
        );
264
    }
265
266
    public function testToJson()
267
    {
268
        // Prepare database
269
        $this->setupData();
270
271
        // Inject database handler
272
        $testDBO = $this->getDBOject();
273
        $testDBO->load(2);
274
275
        $this->assertSame(
276
            '{"id":"2","category_id":"100","name":"Paul","date_added":"2019-01-11 00:00:00"}',
277
            $testDBO->toJson()
278
        );
279
    }
280
281
    public static function mockProperty($object, string $propertyName, $value)
282
    {
283
        $reflectionClass = new \ReflectionClass($object);
284
285
        $property = $reflectionClass->getProperty($propertyName);
286
        $property->setAccessible(true);
287
        $property->setValue($object, $value);
288
        $property->setAccessible(false);
289
    }
290
291
    protected function setupData()
292
    {
293
        $pdo = new PDO('sqlite:/tmp/test.db');
294
        $pdo->exec("DROP TABLE IF EXISTS `users`");
295
        $pdo->exec("DROP TABLE IF EXISTS `categories`");
296
        $pdo->exec("CREATE TABLE `users` (`id` INTEGER PRIMARY KEY,`category_id` INTEGER, `name` varchar(50) DEFAULT NULL,`date_added` datetime NOT NULL)");
297
        $pdo->exec("CREATE TABLE `categories` (`id` INTEGER PRIMARY KEY, `name` varchar(50) DEFAULT NULL)");
298
        
299
        $stmt = $pdo->prepare("INSERT INTO `users` (name, category_id, date_added) VALUES (:name, :categoryid, :date)");
300
        $values = [
301
            ['John', 100, '2019-01-10 00:00:00'],
302
            ['Paul', 100, '2019-01-11 00:00:00'],
303
            ['Robert', 101, '2019-01-12 00:00:00']
304
        ];
305
        foreach ($values as $value) {
306
            $stmt->execute(['name' => $value[0], 'categoryid' => $value[1], 'date' => $value[2]]);
307
        }
308
309
        $stmt = $pdo->prepare("INSERT INTO `categories` (id, name) VALUES (:id, :name)");
310
        $values = [
311
            [100, 'Admin'],
312
            [101, 'Employee']
313
        ];
314
        foreach ($values as $value) {
315
            $stmt->execute(['id' => $value[0], 'name' => $value[1]]);
316
        }
317
    }
318
319
    protected function getDatabase()
320
    {
321
        $database = new \Suricate\Database();
322
        $database->configure([
323
            'type' => 'sqlite',
324
            'file' => '/tmp/test.db',
325
        ]);
326
327
        return $database;
328
    }
329
330
    protected function getDBOject()
331
    {
332
        $dbLink = $this->getDatabase();
333
        // Inject database handler
334
        $testDBO = new \Suricate\DBObject();
335
336
337
        $reflector = new ReflectionClass(get_class($testDBO));
338
        $property = $reflector->getProperty('dbLink');
339
        $property->setAccessible(true);
340
        $property->setValue($testDBO, $dbLink);
341
342
        self::mockProperty($testDBO, 'tableName', $this->tableName);
343
        self::mockProperty($testDBO, 'tableIndex', 'id');
344
        self::mockProperty($testDBO, 'dbVariables', ['id', 'category_id', 'name', 'date_added']);
345
346
        return $testDBO;
347
    }
348
}
349