Completed
Push — develop ( e3f1ea...26d7f1 )
by Mathieu
02:01
created

DBObjectTest::testIsset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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;
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...
58
    }
59
60
    public function testDBProperty()
61
    {
62
        $testDBO = new \Suricate\DBObject();
63
        $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...
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);
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...
67
        $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...
68
        $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...
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));
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...
85
        $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...
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
102
    public static function mockProperty($object, string $propertyName, $value)
103
    {
104
        $reflectionClass = new \ReflectionClass($object);
105
106
        $property = $reflectionClass->getProperty($propertyName);
107
        $property->setAccessible(true);
108
        $property->setValue($object, $value);
109
        $property->setAccessible(false);
110
    }
111
}
112