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
|
|
|
use PhpLab\Di\Fake\Object; |
12
|
|
|
|
13
|
|
|
class ContainerWithObjectsTest extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
protected $app; |
16
|
|
|
|
17
|
|
|
public function setUp() |
18
|
|
|
{ |
19
|
|
|
$this->app = new Container(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function testShouldAssertWhatObjectDefinitionExists() |
23
|
|
|
{ |
24
|
|
|
$this->app->setBuilder('_newObjectEveryTime', function () { |
25
|
|
|
return new Object(); |
26
|
|
|
}); |
27
|
|
|
$result = $this->app->has('_newObjectEveryTime'); |
28
|
|
|
$this->assertTrue($result); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function testShouldGetObject() |
32
|
|
|
{ |
33
|
|
|
$this->app->setBuilder('_newObjectEveryTime', function () { |
34
|
|
|
return new Object(); |
35
|
|
|
}); |
36
|
|
|
$instance = $this->app->get('_newObjectEveryTime'); |
37
|
|
|
$this->assertInstanceOf('\PhpLab\Di\Fake\Object', $instance); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testShouldGetNewInstanceOfObject() |
41
|
|
|
{ |
42
|
|
|
$this->app->setBuilder('_newObjectEveryTime', function () { |
43
|
|
|
return new Object(); |
44
|
|
|
}); |
45
|
|
|
$instance1 = $this->app->get('_newObjectEveryTime'); |
46
|
|
|
$instance2 = $this->app->get('_newObjectEveryTime'); |
47
|
|
|
$this->assertNotSame($instance1, $instance2); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testShouldDefineObjectWithOptionalArgument() |
51
|
|
|
{ |
52
|
|
|
$this->app->setBuilder('_newObjectEveryTime', function () { |
53
|
|
|
return new Object('value'); |
54
|
|
|
}); |
55
|
|
|
$instance = $this->app->get('_newObjectEveryTime'); |
56
|
|
|
$this->assertEquals('value', $instance->getValue()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testShouldDefineObjectWithSetterInjection() |
60
|
|
|
{ |
61
|
|
|
$this->app->setBuilder('_newObjectEveryTime', function () { |
62
|
|
|
$object = new Object(); |
63
|
|
|
$object->setValue('value'); |
64
|
|
|
|
65
|
|
|
return $object; |
66
|
|
|
}); |
67
|
|
|
$instance = $this->app->get('_newObjectEveryTime'); |
68
|
|
|
$this->assertEquals('value', $instance->getValue()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testShouldGetObjectAfterChangeDefinition() |
72
|
|
|
{ |
73
|
|
|
$this->app->setBuilder('_newObjectEveryTime', function () { |
74
|
|
|
return new Object(); |
75
|
|
|
}); |
76
|
|
|
$this->app->setBuilder('_newObjectEveryTime', function () { |
77
|
|
|
return new Object('value'); |
78
|
|
|
}); |
79
|
|
|
$instance = $this->app->get('_newObjectEveryTime'); |
80
|
|
|
$this->assertEquals('value', $instance->getValue()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testShouldThrowExceptionIfChangeDefinitionAfterGettingObject() |
84
|
|
|
{ |
85
|
|
|
$this->setExpectedException('\PhpLab\Di\FrozenException'); |
86
|
|
|
$this->app->setBuilder('_newObjectEveryTime', function () { |
87
|
|
|
return new Object(); |
88
|
|
|
}); |
89
|
|
|
$instance = $this->app->get('_newObjectEveryTime'); |
|
|
|
|
90
|
|
|
$this->app->setBuilder('_newObjectEveryTime', function () { |
91
|
|
|
return new Object('value'); |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testShouldThrowExceptionIfExtendObjectDefinitionNotFound() |
96
|
|
|
{ |
97
|
|
|
$this->setExpectedException('\PhpLab\Di\NotFoundException'); |
98
|
|
|
$this->app->extend('_newObjectEveryTime', function ($object) { |
99
|
|
|
$object->setValue('value'); |
100
|
|
|
|
101
|
|
|
return $object; |
102
|
|
|
}); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testShouldExtendObjectDefinition() |
106
|
|
|
{ |
107
|
|
|
$this->app->setBuilder('_newObjectEveryTime', function () { |
108
|
|
|
return new Object(); |
109
|
|
|
}); |
110
|
|
|
$this->app->extend('_newObjectEveryTime', function ($object) { |
111
|
|
|
$object->setValue('value'); |
112
|
|
|
|
113
|
|
|
return $object; |
114
|
|
|
}); |
115
|
|
|
$instance = $this->app->get('_newObjectEveryTime'); |
116
|
|
|
$this->assertEquals('value', $instance->getValue()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testShouldThrowExceptionIfExtendDefinitionAfterGettingObject() |
120
|
|
|
{ |
121
|
|
|
$this->setExpectedException('\PhpLab\Di\FrozenException'); |
122
|
|
|
$this->app->setBuilder('_newObjectEveryTime', function () { |
123
|
|
|
return new Object(); |
124
|
|
|
}); |
125
|
|
|
$instance = $this->app->get('_newObjectEveryTime'); |
|
|
|
|
126
|
|
|
$this->app->extend('_newObjectEveryTime', function ($object) { |
127
|
|
|
$object->setValue('value'); |
128
|
|
|
|
129
|
|
|
return $object; |
130
|
|
|
}); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testShouldRemoveObjectDefinition() |
134
|
|
|
{ |
135
|
|
|
$this->app->setBuilder('_newObjectEveryTime', function () { |
136
|
|
|
return new Object(); |
137
|
|
|
}); |
138
|
|
|
$this->app->remove('_newObjectEveryTime'); |
139
|
|
|
$result = $this->app->has('_newObjectEveryTime'); |
140
|
|
|
$this->assertFalse($result); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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.