Passed
Push — develop ( ed14bd...e3f1ea )
by Mathieu
01:50
created

DBObjectTest::testContructor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.9332
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;
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...
49
    }
50
51
    public function testDBProperty()
52
    {
53
        $testDBO = new \Suricate\DBObject();
54
        $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...
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);
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...
58
        $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...
59
        $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...
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));
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...
76
        $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...
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