1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2017 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* @license GPL-3.0 |
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Addiks\RDMBundle\Tests\Mapping; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Addiks\RDMBundle\Mapping\ServiceMapping; |
15
|
|
|
use Addiks\RDMBundle\Hydration\HydrationContextInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
17
|
|
|
use Addiks\RDMBundle\Tests\Hydration\EntityExample; |
18
|
|
|
use Addiks\RDMBundle\Tests\Hydration\ServiceExample; |
19
|
|
|
use Addiks\RDMBundle\Exception\FailedRDMAssertionExceptionInterface; |
20
|
|
|
|
21
|
|
|
final class ServiceMappingTest extends TestCase |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var ServiceMapping |
26
|
|
|
*/ |
27
|
|
|
private $serviceMapping; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ContainerInterface |
31
|
|
|
*/ |
32
|
|
|
private $container; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ServiceMapping |
36
|
|
|
*/ |
37
|
|
|
private $defaultServiceMapping; |
38
|
|
|
|
39
|
|
|
public function setUp( |
40
|
|
|
bool $isLax = true, |
41
|
|
|
string $serviceId = "some_service_id" |
42
|
|
|
): void { |
43
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$this->serviceMapping = new ServiceMapping( |
46
|
|
|
$this->container, |
47
|
|
|
$serviceId, |
48
|
|
|
$isLax, |
49
|
|
|
"some origin" |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
$this->defaultServiceMapping = new ServiceMapping( |
53
|
|
|
$this->container, |
54
|
|
|
"some_default_service_id" |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @test |
60
|
|
|
*/ |
61
|
|
|
public function shouldKnowItsServiceId() |
62
|
|
|
{ |
63
|
|
|
$this->assertEquals("some_service_id", $this->serviceMapping->getServiceId()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @test |
68
|
|
|
*/ |
69
|
|
|
public function shouldKnowItsOrigin() |
70
|
|
|
{ |
71
|
|
|
$this->assertEquals("some origin", $this->serviceMapping->describeOrigin()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @test |
76
|
|
|
*/ |
77
|
|
|
public function shouldHaveNoColumns() |
78
|
|
|
{ |
79
|
|
|
$this->assertEquals([], $this->serviceMapping->collectDBALColumns()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @test |
84
|
|
|
*/ |
85
|
|
|
public function shouldKnowIfLaxOrNot() |
86
|
|
|
{ |
87
|
|
|
$this->assertEquals(true, $this->serviceMapping->isLax()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @test |
92
|
|
|
*/ |
93
|
|
|
public function shouldBeNotLaxByDefault() |
94
|
|
|
{ |
95
|
|
|
$this->assertEquals(false, $this->defaultServiceMapping->isLax()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @test |
100
|
|
|
*/ |
101
|
|
|
public function shouldNotKnowItsOriginByDefault() |
102
|
|
|
{ |
103
|
|
|
$this->assertEquals("unknown", $this->defaultServiceMapping->describeOrigin()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @test |
108
|
|
|
*/ |
109
|
|
|
public function shouldResolveToService() |
110
|
|
|
{ |
111
|
|
|
$this->setUp(false, "some_service"); |
112
|
|
|
|
113
|
|
|
$expectedService = new ServiceExample("lorem", 123); |
114
|
|
|
|
115
|
|
|
$this->container->method('has')->will($this->returnValueMap([ |
116
|
|
|
['some_service', true], |
117
|
|
|
])); |
118
|
|
|
|
119
|
|
|
$this->container->method('get')->will($this->returnValueMap([ |
120
|
|
|
['some_service', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $expectedService], |
121
|
|
|
])); |
122
|
|
|
|
123
|
|
|
/** @var mixed $actualValue */ |
124
|
|
|
$actualService = $this->serviceMapping->resolveValue( |
125
|
|
|
$this->createMock(HydrationContextInterface::class), |
126
|
|
|
[] |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
$this->assertSame($expectedService, $actualService); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @test |
134
|
|
|
*/ |
135
|
|
|
public function shouldNotRevertService() |
136
|
|
|
{ |
137
|
|
|
$this->assertEquals([], $this->serviceMapping->revertValue( |
138
|
|
|
$this->createMock(HydrationContextInterface::class), |
139
|
|
|
null |
140
|
|
|
)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @test |
145
|
|
|
*/ |
146
|
|
|
public function shouldAssertThatCorrectServiceWasSet() |
147
|
|
|
{ |
148
|
|
|
$this->setUp(false, "some_service"); |
149
|
|
|
|
150
|
|
|
$service = new ServiceExample("lorem", 123); |
151
|
|
|
$otherService = new ServiceExample("ipsum", 456); |
152
|
|
|
|
153
|
|
|
$this->container->method('has')->will($this->returnValueMap([ |
154
|
|
|
['some_service', true], |
155
|
|
|
])); |
156
|
|
|
|
157
|
|
|
$this->container->method('get')->will($this->returnValueMap([ |
158
|
|
|
['some_service', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $service], |
159
|
|
|
])); |
160
|
|
|
|
161
|
|
|
$this->expectException(FailedRDMAssertionExceptionInterface::class); |
162
|
|
|
|
163
|
|
|
/** @var HydrationContextInterface $context */ |
164
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
165
|
|
|
$context->method('getEntityClass')->willReturn(EntityExample::class); |
|
|
|
|
166
|
|
|
|
167
|
|
|
$this->serviceMapping->assertValue($context, [], $otherService); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @test |
172
|
|
|
*/ |
173
|
|
|
public function shouldNotAssertLaxFieldMappings() |
174
|
|
|
{ |
175
|
|
|
$this->setUp(true); |
176
|
|
|
|
177
|
|
|
$this->container->expects($this->never())->method('has'); |
178
|
|
|
$this->container->expects($this->never())->method('get'); |
179
|
|
|
|
180
|
|
|
/** @var HydrationContextInterface $context */ |
181
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
182
|
|
|
$context->method('getEntityClass')->willReturn(EntityExample::class); |
183
|
|
|
|
184
|
|
|
$service = new ServiceExample("lorem", 123); |
185
|
|
|
|
186
|
|
|
$this->serviceMapping->assertValue($context, [], $service); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @test |
191
|
|
|
*/ |
192
|
|
|
public function shouldNotAssertIfAlwaysLaxModeIsActive() |
193
|
|
|
{ |
194
|
|
|
$this->setUp(false); |
195
|
|
|
|
196
|
|
|
$this->container->expects($this->never())->method('has'); |
197
|
|
|
$this->container->expects($this->never())->method('get'); |
198
|
|
|
|
199
|
|
|
/** @var HydrationContextInterface $context */ |
200
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
201
|
|
|
$context->method('getEntityClass')->willReturn(EntityExample::class); |
202
|
|
|
|
203
|
|
|
/** @var mixed $service */ |
204
|
|
|
$service = new ServiceExample("lorem", 123); |
205
|
|
|
|
206
|
|
|
ServiceMapping::setAlwaysLax(true); |
207
|
|
|
|
208
|
|
|
$this->serviceMapping->assertValue($context, [], $service); |
209
|
|
|
|
210
|
|
|
ServiceMapping::setAlwaysLax(false); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @test |
215
|
|
|
*/ |
216
|
|
|
public function shouldWakeUpInnerMapping() |
217
|
|
|
{ |
218
|
|
|
/** @var ContainerInterface $container */ |
219
|
|
|
$container = $this->createMock(ContainerInterface::class); |
220
|
|
|
|
221
|
|
|
/** @var HydrationContextInterface $context */ |
222
|
|
|
$context = $this->createMock(HydrationContextInterface::class); |
223
|
|
|
|
224
|
|
|
$container->method("has")->willReturn(true); |
|
|
|
|
225
|
|
|
$container->method("get")->willReturn("Foo bar baz"); |
226
|
|
|
|
227
|
|
|
$this->serviceMapping->wakeUpMapping($container); |
228
|
|
|
|
229
|
|
|
$this->assertSame("Foo bar baz", $this->serviceMapping->resolveValue($context, [])); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..