Completed
Push — develop ( 13bd45...a14d83 )
by Mathieu
01:53
created

DBObjectTest::mockProperty()   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 3
dl 0
loc 8
rs 10
1
<?php
2
class DBObjectTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Bug introduced by
The type PHPUnit_Framework_TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 testUndefinedGet()
25
    {
26
        $testDBO = new \Suricate\DBObject();
27
        self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'last_update']);
28
        $this->expectException(\InvalidArgumentException::class);
29
        
30
        $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...
31
    }
32
33
    public function testDBProperty()
34
    {
35
        $testDBO = new \Suricate\DBObject();
36
        $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...
37
        self::mockProperty($testDBO, 'dbVariables', ['id', 'name', 'not_loaded_var']);
38
        self::mockProperty($testDBO, 'dbValues', ['id' => 1, 'name' => 'test name']);
39
        $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...
40
        $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...
41
        $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...
42
43
        $this->assertTrue($testDBO->isDBVariable('id'));
44
        $this->assertFalse($testDBO->isDBVariable('regularProperty'));
45
    }
46
    
47
48
    public static function mockProperty($object, string $propertyName, $value)
49
    {
50
        $reflectionClass = new \ReflectionClass($object);
51
52
        $property = $reflectionClass->getProperty($propertyName);
53
        $property->setAccessible(true);
54
        $property->setValue($object, $value);
55
        $property->setAccessible(false);
56
    }
57
}
58