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