1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Tests\Innmind\Reflection\InjectionStrategy; |
5
|
|
|
|
6
|
|
|
use Innmind\Reflection\{ |
7
|
|
|
InjectionStrategy\DelegationStrategy, |
8
|
|
|
InjectionStrategy, |
9
|
|
|
Exception\PropertyCannotBeInjected, |
10
|
|
|
}; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
|
13
|
|
|
class DelegationStrategyTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function testInterface() |
16
|
|
|
{ |
17
|
|
|
$this->assertInstanceOf( |
18
|
|
|
InjectionStrategy::class, |
19
|
|
|
new DelegationStrategy |
20
|
|
|
); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testSupports() |
24
|
|
|
{ |
25
|
|
|
$strategy = new DelegationStrategy( |
26
|
|
|
$mock1 = $this->createMock(InjectionStrategy::class), |
27
|
|
|
$mock2 = $this->createMock(InjectionStrategy::class) |
28
|
|
|
); |
29
|
|
|
$object = new \stdClass; |
30
|
|
|
$property = 'foo'; |
31
|
|
|
$value = 'bar'; |
32
|
|
|
$mock1 |
33
|
|
|
->expects($this->at(0)) |
34
|
|
|
->method('supports') |
35
|
|
|
->with($object, $property, $value) |
36
|
|
|
->willReturn(false); |
37
|
|
|
$mock1 |
38
|
|
|
->expects($this->at(1)) |
39
|
|
|
->method('supports') |
40
|
|
|
->with($object, $property, $value) |
41
|
|
|
->willReturn(true); |
42
|
|
|
$mock1 |
43
|
|
|
->expects($this->at(2)) |
44
|
|
|
->method('supports') |
45
|
|
|
->with($object, $property, $value) |
46
|
|
|
->willReturn(false); |
47
|
|
|
$mock2 |
48
|
|
|
->expects($this->at(0)) |
49
|
|
|
->method('supports') |
50
|
|
|
->with($object, $property, $value) |
51
|
|
|
->willReturn(true); |
52
|
|
|
$mock2 |
53
|
|
|
->expects($this->at(1)) |
54
|
|
|
->method('supports') |
55
|
|
|
->with($object, $property, $value) |
56
|
|
|
->willReturn(false); |
57
|
|
|
|
58
|
|
|
$this->assertTrue($strategy->supports($object, $property, $value)); |
59
|
|
|
$this->assertTrue($strategy->supports($object, $property, $value)); |
60
|
|
|
$this->assertFalse($strategy->supports($object, $property, $value)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testInjectWithFirstStrategy() |
64
|
|
|
{ |
65
|
|
|
$strategy = new DelegationStrategy( |
66
|
|
|
$mock1 = $this->createMock(InjectionStrategy::class), |
67
|
|
|
$mock2 = $this->createMock(InjectionStrategy::class) |
68
|
|
|
); |
69
|
|
|
$object = new \stdClass; |
70
|
|
|
$property = 'foo'; |
71
|
|
|
$value = 'bar'; |
72
|
|
|
$mock1 |
73
|
|
|
->expects($this->once()) |
74
|
|
|
->method('supports') |
75
|
|
|
->with($object, $property, $value) |
76
|
|
|
->willReturn(true); |
77
|
|
|
$mock2 |
78
|
|
|
->expects($this->never()) |
79
|
|
|
->method('supports'); |
80
|
|
|
$mock1 |
81
|
|
|
->expects($this->once()) |
82
|
|
|
->method('inject') |
83
|
|
|
->with($object, $property, $value) |
84
|
|
|
->willReturn($object); |
85
|
|
|
$mock2 |
86
|
|
|
->expects($this->never()) |
87
|
|
|
->method('inject'); |
88
|
|
|
|
89
|
|
|
$this->assertSame($object, $strategy->inject($object, $property, $value)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testCacheStrategy() |
93
|
|
|
{ |
94
|
|
|
$strategy = new DelegationStrategy( |
95
|
|
|
$mock1 = $this->createMock(InjectionStrategy::class), |
96
|
|
|
$mock2 = $this->createMock(InjectionStrategy::class) |
97
|
|
|
); |
98
|
|
|
$object = new \stdClass; |
99
|
|
|
$property = 'foo'; |
100
|
|
|
$value = 'bar'; |
101
|
|
|
$mock1 |
102
|
|
|
->expects($this->once()) |
103
|
|
|
->method('supports') |
104
|
|
|
->with($object, $property, $value) |
105
|
|
|
->willReturn(true); |
106
|
|
|
$mock2 |
107
|
|
|
->expects($this->never()) |
108
|
|
|
->method('supports'); |
109
|
|
|
$mock1 |
110
|
|
|
->expects($this->exactly(2)) |
111
|
|
|
->method('inject') |
112
|
|
|
->with($object, $property, $value) |
113
|
|
|
->willReturn($object); |
114
|
|
|
$mock2 |
115
|
|
|
->expects($this->never()) |
116
|
|
|
->method('inject'); |
117
|
|
|
|
118
|
|
|
$this->assertSame($object, $strategy->inject($object, $property, $value)); |
119
|
|
|
$this->assertSame($object, $strategy->inject($object, $property, $value)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testExtractWithSecondStrategy() |
123
|
|
|
{ |
124
|
|
|
$strategy = new DelegationStrategy( |
125
|
|
|
$mock1 = $this->createMock(InjectionStrategy::class), |
126
|
|
|
$mock2 = $this->createMock(InjectionStrategy::class) |
127
|
|
|
); |
128
|
|
|
$object = new \stdClass; |
129
|
|
|
$property = 'foo'; |
130
|
|
|
$value = 'bar'; |
131
|
|
|
$mock1 |
132
|
|
|
->expects($this->once()) |
133
|
|
|
->method('supports') |
134
|
|
|
->with($object, $property, $value) |
135
|
|
|
->willReturn(false); |
136
|
|
|
$mock2 |
137
|
|
|
->expects($this->once()) |
138
|
|
|
->method('supports') |
139
|
|
|
->with($object, $property, $value) |
140
|
|
|
->willReturn(true); |
141
|
|
|
$mock1 |
142
|
|
|
->expects($this->never()) |
143
|
|
|
->method('inject'); |
144
|
|
|
$mock2 |
145
|
|
|
->expects($this->once()) |
146
|
|
|
->method('inject') |
147
|
|
|
->with($object, $property, $value) |
148
|
|
|
->willReturn($object); |
149
|
|
|
|
150
|
|
|
$this->assertSame($object, $strategy->inject($object, $property, $value)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testThrowWhenNoStrategySupporting() |
154
|
|
|
{ |
155
|
|
|
$strategy = new DelegationStrategy( |
156
|
|
|
$mock1 = $this->createMock(InjectionStrategy::class), |
157
|
|
|
$mock2 = $this->createMock(InjectionStrategy::class) |
158
|
|
|
); |
159
|
|
|
$object = new \stdClass; |
160
|
|
|
$property = 'foo'; |
161
|
|
|
$value = 'bar'; |
162
|
|
|
$mock1 |
163
|
|
|
->expects($this->once()) |
164
|
|
|
->method('supports') |
165
|
|
|
->with($object, $property, $value) |
166
|
|
|
->willReturn(false); |
167
|
|
|
$mock2 |
168
|
|
|
->expects($this->once()) |
169
|
|
|
->method('supports') |
170
|
|
|
->with($object, $property, $value) |
171
|
|
|
->willReturn(false); |
172
|
|
|
$mock1 |
173
|
|
|
->expects($this->never()) |
174
|
|
|
->method('inject'); |
175
|
|
|
$mock2 |
176
|
|
|
->expects($this->never()) |
177
|
|
|
->method('inject'); |
178
|
|
|
|
179
|
|
|
$this->expectException(PropertyCannotBeInjected::class); |
180
|
|
|
$this->expectExceptionMessage('Property "foo" cannot be injected'); |
181
|
|
|
|
182
|
|
|
$strategy->inject($object, $property, $value); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|