Passed
Push — develop ( 3cbb1a...ed14bd )
by Mathieu
01:40
created

ServiceTest::testConfigure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
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 12
rs 10
1
<?php
2
class ServiceTest extends \PHPUnit\Framework\TestCase
3
{
4
    
5
    public function testGetException()
6
    {
7
        $this->expectException(InvalidArgumentException::class);
8
        $service = new \Suricate\Service();
9
        $service->undefVar;
0 ignored issues
show
Bug Best Practice introduced by
The property undefVar does not exist on Suricate\Service. Since you implemented __get, consider adding a @property annotation.
Loading history...
10
    }
11
12
    public function testSetException()
13
    {
14
        $this->expectException(InvalidArgumentException::class);
15
        $service = new \Suricate\Service();
16
        $service->undefVar = "123";
0 ignored issues
show
Bug Best Practice introduced by
The property undefVar does not exist on Suricate\Service. Since you implemented __set, consider adding a @property annotation.
Loading history...
17
    }
18
19
    public function testGet()
20
    {
21
        $testService = new \Suricate\Service();
22
23
        self::mockProperty($testService, 'parametersList', ['param_1', 'param_2']);
24
        $this->assertNull($testService->param_1);
0 ignored issues
show
Bug Best Practice introduced by
The property param_1 does not exist on Suricate\Service. Since you implemented __get, consider adding a @property annotation.
Loading history...
25
        self::mockProperty($testService, 'parametersValues', ['param_1' => 'value1', 'param_2' => 'value2']);
26
        $this->assertNotNull($testService->param_1);
27
        $this->assertEquals($testService->param_1, 'value1');
28
    }
29
30
    public function testSet()
31
    {
32
        $testService = new \Suricate\Service();
33
34
        self::mockProperty($testService, 'parametersList', ['param_1', 'param_2']);
35
        $this->assertNull($testService->param_1);
0 ignored issues
show
Bug Best Practice introduced by
The property param_1 does not exist on Suricate\Service. Since you implemented __get, consider adding a @property annotation.
Loading history...
36
        $testService->param_1 = 'new_value';
0 ignored issues
show
Bug Best Practice introduced by
The property param_1 does not exist on Suricate\Service. Since you implemented __set, consider adding a @property annotation.
Loading history...
37
        $this->assertEquals($testService->param_1, 'new_value');
38
    }
39
40
    public function testConfigure()
41
    {
42
        $testService = new \Suricate\Service();
43
44
        self::mockProperty($testService, 'parametersList', ['param_1', 'param_2']);
45
        $testService->configure(['param_1' => 'value1', 'param_2' => 'value2']);
46
47
        $this->assertEquals($testService->param_1, 'value1');
0 ignored issues
show
Bug Best Practice introduced by
The property param_1 does not exist on Suricate\Service. Since you implemented __get, consider adding a @property annotation.
Loading history...
48
        $this->assertEquals($testService->param_2, 'value2');
0 ignored issues
show
Bug Best Practice introduced by
The property param_2 does not exist on Suricate\Service. Since you implemented __get, consider adding a @property annotation.
Loading history...
49
50
        $this->expectException(InvalidArgumentException::class);
51
        $testService->configure(['param_3' => 'value3']);
52
    }
53
54
    public static function mockProperty($object, string $propertyName, $value)
55
    {
56
        $reflectionClass = new \ReflectionClass($object);
57
58
        $property = $reflectionClass->getProperty($propertyName);
59
        $property->setAccessible(true);
60
        $property->setValue($object, $value);
61
        $property->setAccessible(false);
62
    }
63
}
64