1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @author Yuriy Davletshin <[email protected]> |
6
|
|
|
* @copyright 2015 Yuriy Davletshin |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
namespace PhpLab\Di; |
10
|
|
|
|
11
|
|
|
class ContainerWithParametersTest extends \PHPUnit_Framework_TestCase |
12
|
|
|
{ |
13
|
|
|
protected $app; |
14
|
|
|
|
15
|
|
|
public function setUp() |
16
|
|
|
{ |
17
|
|
|
$this->app = new Container(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testShouldThrowExceptionIfParameterNotFound() |
21
|
|
|
{ |
22
|
|
|
$this->setExpectedException('\PhpLab\Di\NotFoundException'); |
23
|
|
|
$param = $this->app['param.not_exists']; |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testShouldAssertWhatParameterNotExists() |
27
|
|
|
{ |
28
|
|
|
$result = isset($this->app['param.not_exists']); |
29
|
|
|
$this->assertFalse($result); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testShouldAssertWhatParameterExists() |
33
|
|
|
{ |
34
|
|
|
$this->app['param.test_value'] = 'value'; |
35
|
|
|
$result = isset($this->app['param.test_value']); |
36
|
|
|
$this->assertTrue($result); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testShouldGetParameterValue() |
40
|
|
|
{ |
41
|
|
|
$this->app['param.test_value'] = 'value'; |
42
|
|
|
$param = $this->app['param.test_value']; |
43
|
|
|
$this->assertEquals('value', $param); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testShouldGetParameterAfterChangeValue() |
47
|
|
|
{ |
48
|
|
|
$this->app['param.test_value'] = 'value'; |
49
|
|
|
$this->app['param.test_value'] = 'another value'; |
50
|
|
|
$param = $this->app['param.test_value']; |
51
|
|
|
$this->assertEquals('another value', $param); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testShouldThrowExceptionIfChangeValueAfterGettingParameter() |
55
|
|
|
{ |
56
|
|
|
$this->setExpectedException('\PhpLab\Di\FrozenException'); |
57
|
|
|
$this->app['param.test_value'] = 'value'; |
58
|
|
|
$param = $this->app['param.test_value']; |
|
|
|
|
59
|
|
|
$this->app['param.test_value'] = 'another value'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testShouldRemoveParameter() |
63
|
|
|
{ |
64
|
|
|
$this->app['param.test_value'] = 'value'; |
65
|
|
|
unset($this->app['param.test_value']); |
66
|
|
|
$result = isset($this->app['param.test_value']); |
67
|
|
|
$this->assertFalse($result); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testShouldGetProtectedAnonymousFunction() |
71
|
|
|
{ |
72
|
|
|
$this->app['func.protected'] = function () { |
73
|
|
|
return 'value'; |
74
|
|
|
}; |
75
|
|
|
$result = $this->app['func.protected'](); |
76
|
|
|
$this->assertEquals('value', $result); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.