1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Bundle\ResourceBundle\Tests\EventSubscriber\Doctrine\ORM; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\EventSubscriber; |
15
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
16
|
|
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs; |
17
|
|
|
use Doctrine\ORM\Events; |
18
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
19
|
|
|
use Doctrine\ORM\Mapping\MappingException; |
20
|
|
|
use Lug\Bundle\ResourceBundle\EventSubscriber\Doctrine\ORM\ResourceSubscriber; |
21
|
|
|
use Lug\Component\Registry\Model\RegistryInterface; |
22
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author GeLo <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ResourceSubscriberTest extends \PHPUnit_Framework_TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var ResourceSubscriber |
31
|
|
|
*/ |
32
|
|
|
private $resourceSubscriber; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|RegistryInterface |
36
|
|
|
*/ |
37
|
|
|
private $serviceRegistry; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
protected function setUp() |
43
|
|
|
{ |
44
|
|
|
$this->serviceRegistry = $this->createServiceRegistryMock(); |
45
|
|
|
$this->resourceSubscriber = new ResourceSubscriber($this->serviceRegistry); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testInheritance() |
49
|
|
|
{ |
50
|
|
|
$this->assertInstanceOf(EventSubscriber::class, $this->resourceSubscriber); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testSubscribedEvents() |
54
|
|
|
{ |
55
|
|
|
$this->assertSame([Events::loadClassMetadata], $this->resourceSubscriber->getSubscribedEvents()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
View Code Duplication |
public function testLoadClassMetadataWithResourceAndNoParentClasses() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$event = $this->createLoadClassMetadataEventArgsMock(); |
61
|
|
|
$event |
|
|
|
|
62
|
|
|
->expects($this->once()) |
63
|
|
|
->method('getClassMetadata') |
64
|
|
|
->will($this->returnValue($classMetadata = $this->createClassMetadataMock())); |
65
|
|
|
|
66
|
|
|
$classMetadata |
|
|
|
|
67
|
|
|
->expects($this->once()) |
68
|
|
|
->method('getName') |
69
|
|
|
->will($this->returnValue($model = 'model')); |
70
|
|
|
|
71
|
|
|
$this->serviceRegistry |
|
|
|
|
72
|
|
|
->expects($this->once()) |
73
|
|
|
->method('getIterator') |
74
|
|
|
->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()]))); |
75
|
|
|
|
76
|
|
|
$resource |
|
|
|
|
77
|
|
|
->expects($this->once()) |
78
|
|
|
->method('getModel') |
79
|
|
|
->will($this->returnValue($model)); |
80
|
|
|
|
81
|
|
|
$resource |
82
|
|
|
->expects($this->once()) |
83
|
|
|
->method('getRepository') |
84
|
|
|
->will($this->returnValue($repository = 'repository')); |
85
|
|
|
|
86
|
|
|
$classMetadata |
87
|
|
|
->expects($this->once()) |
88
|
|
|
->method('setCustomRepositoryClass') |
89
|
|
|
->with($this->identicalTo($repository)); |
90
|
|
|
|
91
|
|
|
$classMetadata->parentClasses = []; |
92
|
|
|
$classMetadata->isMappedSuperclass = false; |
93
|
|
|
|
94
|
|
|
$this->resourceSubscriber->loadClassMetadata($event); |
|
|
|
|
95
|
|
|
|
96
|
|
|
$this->assertFalse($classMetadata->isMappedSuperclass); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function testLoadClassMetadataWithResourceAndParentClassesWithoutInheritance() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$event = $this->createLoadClassMetadataEventArgsMock(); |
102
|
|
|
$event |
|
|
|
|
103
|
|
|
->expects($this->once()) |
104
|
|
|
->method('getClassMetadata') |
105
|
|
|
->will($this->returnValue($classMetadata = $this->createClassMetadataMock())); |
106
|
|
|
|
107
|
|
|
$classMetadata |
|
|
|
|
108
|
|
|
->expects($this->once()) |
109
|
|
|
->method('getName') |
110
|
|
|
->will($this->returnValue($model = 'model')); |
111
|
|
|
|
112
|
|
|
$this->serviceRegistry |
|
|
|
|
113
|
|
|
->expects($this->once()) |
114
|
|
|
->method('getIterator') |
115
|
|
|
->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()]))); |
116
|
|
|
|
117
|
|
|
$resource |
|
|
|
|
118
|
|
|
->expects($this->once()) |
119
|
|
|
->method('getModel') |
120
|
|
|
->will($this->returnValue($model)); |
121
|
|
|
|
122
|
|
|
$classMetadata->parentClasses = [$parentClass = 'ParentClass']; |
123
|
|
|
|
124
|
|
|
$event |
125
|
|
|
->expects($this->once()) |
126
|
|
|
->method('getObjectManager') |
127
|
|
|
->will($this->returnValue($objectManager = $this->createObjectManagerMock())); |
128
|
|
|
|
129
|
|
|
$objectManager |
|
|
|
|
130
|
|
|
->expects($this->once()) |
131
|
|
|
->method('getClassMetadata') |
132
|
|
|
->with($this->identicalTo($parentClass)) |
133
|
|
|
->will($this->returnValue($parentMetadata = $this->createClassMetadataMock())); |
134
|
|
|
|
135
|
|
|
$parentMetadata->isMappedSuperclass = false; |
136
|
|
|
$parentMetadata->inheritanceType = ClassMetadata::INHERITANCE_TYPE_NONE; |
137
|
|
|
|
138
|
|
|
$resource |
139
|
|
|
->expects($this->once()) |
140
|
|
|
->method('getRepository') |
141
|
|
|
->will($this->returnValue($repository = 'repository')); |
142
|
|
|
|
143
|
|
|
$classMetadata |
144
|
|
|
->expects($this->once()) |
145
|
|
|
->method('setCustomRepositoryClass') |
146
|
|
|
->with($this->identicalTo($repository)); |
147
|
|
|
|
148
|
|
|
$this->resourceSubscriber->loadClassMetadata($event); |
|
|
|
|
149
|
|
|
|
150
|
|
|
$this->assertFalse($classMetadata->isMappedSuperclass); |
151
|
|
|
$this->assertTrue($parentMetadata->isMappedSuperclass); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
View Code Duplication |
public function testLoadClassMetadataWithResourceAndParentClassesWithInheritance() |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$event = $this->createLoadClassMetadataEventArgsMock(); |
157
|
|
|
$event |
|
|
|
|
158
|
|
|
->expects($this->once()) |
159
|
|
|
->method('getClassMetadata') |
160
|
|
|
->will($this->returnValue($classMetadata = $this->createClassMetadataMock())); |
161
|
|
|
|
162
|
|
|
$classMetadata |
|
|
|
|
163
|
|
|
->expects($this->once()) |
164
|
|
|
->method('getName') |
165
|
|
|
->will($this->returnValue($model = 'model')); |
166
|
|
|
|
167
|
|
|
$this->serviceRegistry |
|
|
|
|
168
|
|
|
->expects($this->once()) |
169
|
|
|
->method('getIterator') |
170
|
|
|
->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()]))); |
171
|
|
|
|
172
|
|
|
$resource |
|
|
|
|
173
|
|
|
->expects($this->once()) |
174
|
|
|
->method('getModel') |
175
|
|
|
->will($this->returnValue($model)); |
176
|
|
|
|
177
|
|
|
$classMetadata->parentClasses = [$parentClass = 'ParentClass']; |
178
|
|
|
|
179
|
|
|
$event |
180
|
|
|
->expects($this->once()) |
181
|
|
|
->method('getObjectManager') |
182
|
|
|
->will($this->returnValue($objectManager = $this->createObjectManagerMock())); |
183
|
|
|
|
184
|
|
|
$objectManager |
|
|
|
|
185
|
|
|
->expects($this->once()) |
186
|
|
|
->method('getClassMetadata') |
187
|
|
|
->with($this->identicalTo($parentClass)) |
188
|
|
|
->will($this->returnValue($parentMetadata = $this->createClassMetadataMock())); |
189
|
|
|
|
190
|
|
|
$parentMetadata->isMappedSuperclass = false; |
191
|
|
|
$parentMetadata->inheritanceType = ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE; |
192
|
|
|
|
193
|
|
|
$resource |
194
|
|
|
->expects($this->once()) |
195
|
|
|
->method('getRepository') |
196
|
|
|
->will($this->returnValue($repository = 'repository')); |
197
|
|
|
|
198
|
|
|
$classMetadata |
199
|
|
|
->expects($this->once()) |
200
|
|
|
->method('setCustomRepositoryClass') |
201
|
|
|
->with($this->identicalTo($repository)); |
202
|
|
|
|
203
|
|
|
$this->resourceSubscriber->loadClassMetadata($event); |
|
|
|
|
204
|
|
|
|
205
|
|
|
$this->assertFalse($classMetadata->isMappedSuperclass); |
206
|
|
|
$this->assertFalse($parentMetadata->isMappedSuperclass); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
View Code Duplication |
public function testLoadClassMetadataWithResourceAndParentClassesWithoutClassMetadata() |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
$event = $this->createLoadClassMetadataEventArgsMock(); |
212
|
|
|
$event |
|
|
|
|
213
|
|
|
->expects($this->once()) |
214
|
|
|
->method('getClassMetadata') |
215
|
|
|
->will($this->returnValue($classMetadata = $this->createClassMetadataMock())); |
216
|
|
|
|
217
|
|
|
$classMetadata |
|
|
|
|
218
|
|
|
->expects($this->once()) |
219
|
|
|
->method('getName') |
220
|
|
|
->will($this->returnValue($model = 'model')); |
221
|
|
|
|
222
|
|
|
$this->serviceRegistry |
|
|
|
|
223
|
|
|
->expects($this->once()) |
224
|
|
|
->method('getIterator') |
225
|
|
|
->will($this->returnValue(new \ArrayIterator([$resource = $this->createResourceMock()]))); |
226
|
|
|
|
227
|
|
|
$resource |
|
|
|
|
228
|
|
|
->expects($this->once()) |
229
|
|
|
->method('getModel') |
230
|
|
|
->will($this->returnValue($model)); |
231
|
|
|
|
232
|
|
|
$classMetadata->parentClasses = [$parentClass = 'ParentClass']; |
233
|
|
|
|
234
|
|
|
$event |
235
|
|
|
->expects($this->once()) |
236
|
|
|
->method('getObjectManager') |
237
|
|
|
->will($this->returnValue($objectManager = $this->createObjectManagerMock())); |
238
|
|
|
|
239
|
|
|
$objectManager |
|
|
|
|
240
|
|
|
->expects($this->once()) |
241
|
|
|
->method('getClassMetadata') |
242
|
|
|
->with($this->identicalTo($parentClass)) |
243
|
|
|
->will($this->throwException($this->createMappingExceptionMock())); |
|
|
|
|
244
|
|
|
|
245
|
|
|
$resource |
246
|
|
|
->expects($this->once()) |
247
|
|
|
->method('getRepository') |
248
|
|
|
->will($this->returnValue($repository = 'repository')); |
249
|
|
|
|
250
|
|
|
$classMetadata |
251
|
|
|
->expects($this->once()) |
252
|
|
|
->method('setCustomRepositoryClass') |
253
|
|
|
->with($this->identicalTo($repository)); |
254
|
|
|
|
255
|
|
|
$this->resourceSubscriber->loadClassMetadata($event); |
|
|
|
|
256
|
|
|
|
257
|
|
|
$this->assertFalse($classMetadata->isMappedSuperclass); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
View Code Duplication |
public function testLoadClassMetadataWithoutResource() |
|
|
|
|
261
|
|
|
{ |
262
|
|
|
$event = $this->createLoadClassMetadataEventArgsMock(); |
263
|
|
|
$event |
|
|
|
|
264
|
|
|
->expects($this->once()) |
265
|
|
|
->method('getClassMetadata') |
266
|
|
|
->will($this->returnValue($classMetadata = $this->createClassMetadataMock())); |
267
|
|
|
|
268
|
|
|
$classMetadata |
|
|
|
|
269
|
|
|
->expects($this->once()) |
270
|
|
|
->method('getName') |
271
|
|
|
->will($this->returnValue($model = 'model')); |
272
|
|
|
|
273
|
|
|
$this->serviceRegistry |
|
|
|
|
274
|
|
|
->expects($this->once()) |
275
|
|
|
->method('getIterator') |
276
|
|
|
->will($this->returnValue(new \ArrayIterator([]))); |
277
|
|
|
|
278
|
|
|
$classMetadata |
279
|
|
|
->expects($this->never()) |
280
|
|
|
->method('setCustomRepositoryClass'); |
281
|
|
|
|
282
|
|
|
$classMetadata->isMappedSuperclass = true; |
283
|
|
|
|
284
|
|
|
$this->resourceSubscriber->loadClassMetadata($event); |
|
|
|
|
285
|
|
|
|
286
|
|
|
$this->assertTrue($classMetadata->isMappedSuperclass); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|RegistryInterface |
291
|
|
|
*/ |
292
|
|
|
private function createServiceRegistryMock() |
293
|
|
|
{ |
294
|
|
|
return $this->getMock(RegistryInterface::class); |
|
|
|
|
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|LoadClassMetadataEventArgs |
299
|
|
|
*/ |
300
|
|
|
private function createLoadClassMetadataEventArgsMock() |
301
|
|
|
{ |
302
|
|
|
return $this->getMockBuilder(LoadClassMetadataEventArgs::class) |
303
|
|
|
->disableOriginalConstructor() |
304
|
|
|
->getMock(); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ObjectManager |
309
|
|
|
*/ |
310
|
|
|
private function createObjectManagerMock() |
311
|
|
|
{ |
312
|
|
|
return $this->getMock(ObjectManager::class); |
|
|
|
|
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ClassMetadata |
317
|
|
|
*/ |
318
|
|
|
private function createClassMetadataMock() |
319
|
|
|
{ |
320
|
|
|
return $this->getMockBuilder(ClassMetadata::class) |
321
|
|
|
->disableOriginalConstructor() |
322
|
|
|
->getMock(); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
327
|
|
|
*/ |
328
|
|
|
private function createResourceMock() |
329
|
|
|
{ |
330
|
|
|
return $this->getMock(ResourceInterface::class); |
|
|
|
|
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|MappingException |
335
|
|
|
*/ |
336
|
|
|
private function createMappingExceptionMock() |
337
|
|
|
{ |
338
|
|
|
return $this->getMock(MappingException::class); |
|
|
|
|
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.