1
|
|
|
<?php |
2
|
|
|
class ServiceTest extends \PHPUnit\Framework\TestCase |
3
|
|
|
{ |
4
|
|
|
public function testGetException() |
5
|
|
|
{ |
6
|
|
|
$this->expectException(InvalidArgumentException::class); |
7
|
|
|
$service = new \Suricate\Service(); |
8
|
|
|
$service->undefVar; |
|
|
|
|
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
public function testSetException() |
12
|
|
|
{ |
13
|
|
|
$this->expectException(InvalidArgumentException::class); |
14
|
|
|
$service = new \Suricate\Service(); |
15
|
|
|
$service->undefVar = "123"; |
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function testGet() |
19
|
|
|
{ |
20
|
|
|
$testService = new \Suricate\Service(); |
21
|
|
|
|
22
|
|
|
self::mockProperty($testService, 'parametersList', [ |
23
|
|
|
'param_1', |
24
|
|
|
'param_2' |
25
|
|
|
]); |
26
|
|
|
$this->assertNull($testService->param_1); |
|
|
|
|
27
|
|
|
self::mockProperty($testService, 'parametersValues', [ |
28
|
|
|
'param_1' => 'value1', |
29
|
|
|
'param_2' => 'value2' |
30
|
|
|
]); |
31
|
|
|
$this->assertNotNull($testService->param_1); |
32
|
|
|
$this->assertEquals($testService->param_1, 'value1'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testSet() |
36
|
|
|
{ |
37
|
|
|
$testService = new \Suricate\Service(); |
38
|
|
|
|
39
|
|
|
self::mockProperty($testService, 'parametersList', [ |
40
|
|
|
'param_1', |
41
|
|
|
'param_2' |
42
|
|
|
]); |
43
|
|
|
$this->assertNull($testService->param_1); |
|
|
|
|
44
|
|
|
$testService->param_1 = 'new_value'; |
|
|
|
|
45
|
|
|
$this->assertEquals($testService->param_1, 'new_value'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testConfigure() |
49
|
|
|
{ |
50
|
|
|
$testService = new \Suricate\Service(); |
51
|
|
|
|
52
|
|
|
self::mockProperty($testService, 'parametersList', [ |
53
|
|
|
'param_1', |
54
|
|
|
'param_2' |
55
|
|
|
]); |
56
|
|
|
$testService->configure(['param_1' => 'value1', 'param_2' => 'value2']); |
57
|
|
|
|
58
|
|
|
$this->assertEquals($testService->param_1, 'value1'); |
|
|
|
|
59
|
|
|
$this->assertEquals($testService->param_2, 'value2'); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$this->expectException(InvalidArgumentException::class); |
62
|
|
|
$testService->configure(['param_3' => 'value3']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function mockProperty($object, string $propertyName, $value) |
66
|
|
|
{ |
67
|
|
|
$reflectionClass = new \ReflectionClass($object); |
68
|
|
|
|
69
|
|
|
$property = $reflectionClass->getProperty($propertyName); |
70
|
|
|
$property->setAccessible(true); |
71
|
|
|
$property->setValue($object, $value); |
72
|
|
|
$property->setAccessible(false); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|