|
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\EventListener; |
|
13
|
|
|
|
|
14
|
|
|
use Lug\Bundle\ResourceBundle\EventListener\MessageListener; |
|
15
|
|
|
use Lug\Bundle\ResourceBundle\Routing\ParameterResolverInterface; |
|
16
|
|
|
use Lug\Component\Resource\Domain\DomainEvent; |
|
17
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
|
18
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
|
19
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author GeLo <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class MessageListenerTest extends \PHPUnit_Framework_TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var MessageListener |
|
28
|
|
|
*/ |
|
29
|
|
|
private $messageListener; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|TranslatorInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $translator; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|PropertyAccessorInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $propertyAccessor; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $parameterResolver; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function setUp() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->translator = $this->createTranslatorMock(); |
|
52
|
|
|
$this->propertyAccessor = $this->createPropertyAccessorMock(); |
|
53
|
|
|
$this->parameterResolver = $this->createParameterResolverMock(); |
|
54
|
|
|
|
|
55
|
|
|
$this->messageListener = new MessageListener( |
|
56
|
|
|
$this->translator, |
|
57
|
|
|
$this->propertyAccessor, |
|
58
|
|
|
$this->parameterResolver |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testMessageWithExplicit() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->parameterResolver |
|
65
|
|
|
->expects($this->once()) |
|
66
|
|
|
->method('resolveApi') |
|
67
|
|
|
->will($this->returnValue(false)); |
|
68
|
|
|
|
|
69
|
|
|
$event = $this->createDomainEventMock(); |
|
70
|
|
|
$event |
|
71
|
|
|
->expects($this->once()) |
|
72
|
|
|
->method('getMessageType') |
|
73
|
|
|
->will($this->returnValue($messageType = 'message_type')); |
|
74
|
|
|
|
|
75
|
|
|
$event |
|
76
|
|
|
->expects($this->once()) |
|
77
|
|
|
->method('setMessageType') |
|
78
|
|
|
->with($this->identicalTo($messageType)); |
|
79
|
|
|
|
|
80
|
|
|
$event |
|
81
|
|
|
->expects($this->once()) |
|
82
|
|
|
->method('getMessage') |
|
83
|
|
|
->will($this->returnValue($message = 'message')); |
|
84
|
|
|
|
|
85
|
|
|
$event |
|
86
|
|
|
->expects($this->once()) |
|
87
|
|
|
->method('setMessage') |
|
88
|
|
|
->with($this->identicalTo($message)); |
|
89
|
|
|
|
|
90
|
|
|
$this->messageListener->addMessage($event); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
View Code Duplication |
public function testMessageTypeSuccess() |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$this->parameterResolver |
|
96
|
|
|
->expects($this->once()) |
|
97
|
|
|
->method('resolveApi') |
|
98
|
|
|
->will($this->returnValue(false)); |
|
99
|
|
|
|
|
100
|
|
|
$event = $this->createDomainEventMock(); |
|
101
|
|
|
$event |
|
102
|
|
|
->expects($this->once()) |
|
103
|
|
|
->method('getMessageType') |
|
104
|
|
|
->will($this->returnValue(null)); |
|
105
|
|
|
|
|
106
|
|
|
$event |
|
107
|
|
|
->expects($this->once()) |
|
108
|
|
|
->method('isStopped') |
|
109
|
|
|
->will($this->returnValue(false)); |
|
110
|
|
|
|
|
111
|
|
|
$event |
|
112
|
|
|
->expects($this->once()) |
|
113
|
|
|
->method('setMessageType') |
|
114
|
|
|
->with($this->identicalTo('success')); |
|
115
|
|
|
|
|
116
|
|
|
$event |
|
117
|
|
|
->expects($this->once()) |
|
118
|
|
|
->method('getMessage') |
|
119
|
|
|
->will($this->returnValue($message = 'message')); |
|
120
|
|
|
|
|
121
|
|
|
$event |
|
122
|
|
|
->expects($this->once()) |
|
123
|
|
|
->method('setMessage') |
|
124
|
|
|
->with($this->identicalTo($message)); |
|
125
|
|
|
|
|
126
|
|
|
$this->messageListener->addMessage($event); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
View Code Duplication |
public function testMessageTypeError() |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
|
|
$this->parameterResolver |
|
132
|
|
|
->expects($this->once()) |
|
133
|
|
|
->method('resolveApi') |
|
134
|
|
|
->will($this->returnValue(false)); |
|
135
|
|
|
|
|
136
|
|
|
$event = $this->createDomainEventMock(); |
|
137
|
|
|
$event |
|
138
|
|
|
->expects($this->once()) |
|
139
|
|
|
->method('getMessageType') |
|
140
|
|
|
->will($this->returnValue(null)); |
|
141
|
|
|
|
|
142
|
|
|
$event |
|
143
|
|
|
->expects($this->once()) |
|
144
|
|
|
->method('isStopped') |
|
145
|
|
|
->will($this->returnValue(true)); |
|
146
|
|
|
|
|
147
|
|
|
$event |
|
148
|
|
|
->expects($this->once()) |
|
149
|
|
|
->method('setMessageType') |
|
150
|
|
|
->with($this->identicalTo('error')); |
|
151
|
|
|
|
|
152
|
|
|
$event |
|
153
|
|
|
->expects($this->once()) |
|
154
|
|
|
->method('getMessage') |
|
155
|
|
|
->will($this->returnValue($message = 'message')); |
|
156
|
|
|
|
|
157
|
|
|
$event |
|
158
|
|
|
->expects($this->once()) |
|
159
|
|
|
->method('setMessage') |
|
160
|
|
|
->with($this->identicalTo($message)); |
|
161
|
|
|
|
|
162
|
|
|
$this->messageListener->addMessage($event); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function testMessageWithLabelPropertyPath() |
|
166
|
|
|
{ |
|
167
|
|
|
$this->parameterResolver |
|
168
|
|
|
->expects($this->once()) |
|
169
|
|
|
->method('resolveApi') |
|
170
|
|
|
->will($this->returnValue(false)); |
|
171
|
|
|
|
|
172
|
|
|
$event = $this->createDomainEventMock(); |
|
173
|
|
|
$event |
|
174
|
|
|
->expects($this->once()) |
|
175
|
|
|
->method('getMessageType') |
|
176
|
|
|
->will($this->returnValue($messageType = 'message_type')); |
|
177
|
|
|
|
|
178
|
|
|
$event |
|
179
|
|
|
->expects($this->once()) |
|
180
|
|
|
->method('setMessageType') |
|
181
|
|
|
->with($this->identicalTo($messageType)); |
|
182
|
|
|
|
|
183
|
|
|
$event |
|
184
|
|
|
->expects($this->once()) |
|
185
|
|
|
->method('getMessage') |
|
186
|
|
|
->will($this->returnValue(null)); |
|
187
|
|
|
|
|
188
|
|
|
$event |
|
189
|
|
|
->expects($this->once()) |
|
190
|
|
|
->method('getAction') |
|
191
|
|
|
->will($this->returnValue($action = 'action')); |
|
192
|
|
|
|
|
193
|
|
|
$event |
|
194
|
|
|
->expects($this->once()) |
|
195
|
|
|
->method('getObject') |
|
196
|
|
|
->will($this->returnValue($object = new \stdClass())); |
|
197
|
|
|
|
|
198
|
|
|
$event |
|
199
|
|
|
->expects($this->once()) |
|
200
|
|
|
->method('getResource') |
|
201
|
|
|
->will($this->returnValue($resource = $this->createResourceMock())); |
|
202
|
|
|
|
|
203
|
|
|
$resource |
|
204
|
|
|
->expects($this->once()) |
|
205
|
|
|
->method('getName') |
|
206
|
|
|
->will($this->returnValue($name = 'name')); |
|
207
|
|
|
|
|
208
|
|
|
$resource |
|
209
|
|
|
->expects($this->once()) |
|
210
|
|
|
->method('getLabelPropertyPath') |
|
211
|
|
|
->will($this->returnValue($labelPropertyPath = 'property_path')); |
|
212
|
|
|
|
|
213
|
|
|
$this->propertyAccessor |
|
|
|
|
|
|
214
|
|
|
->expects($this->once()) |
|
215
|
|
|
->method('getValue') |
|
216
|
|
|
->with( |
|
217
|
|
|
$this->identicalTo($object), |
|
218
|
|
|
$this->identicalTo($labelPropertyPath) |
|
219
|
|
|
) |
|
220
|
|
|
->will($this->returnValue($property = 'property')); |
|
221
|
|
|
|
|
222
|
|
|
$this->translator |
|
|
|
|
|
|
223
|
|
|
->expects($this->once()) |
|
224
|
|
|
->method('trans') |
|
225
|
|
|
->with( |
|
226
|
|
|
$this->identicalTo('lug.'.$name.'.'.$action.'.'.$messageType), |
|
227
|
|
|
$this->identicalTo(['%'.$name.'%' => $property]) |
|
228
|
|
|
) |
|
229
|
|
|
->will($this->returnValue($translation = 'translation')); |
|
230
|
|
|
|
|
231
|
|
|
$event |
|
232
|
|
|
->expects($this->once()) |
|
233
|
|
|
->method('setMessage') |
|
234
|
|
|
->with($this->identicalTo($translation)); |
|
235
|
|
|
|
|
236
|
|
|
$this->messageListener->addMessage($event); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
public function testMessageWithoutLabelPropertyPath() |
|
240
|
|
|
{ |
|
241
|
|
|
$this->parameterResolver |
|
242
|
|
|
->expects($this->once()) |
|
243
|
|
|
->method('resolveApi') |
|
244
|
|
|
->will($this->returnValue(false)); |
|
245
|
|
|
|
|
246
|
|
|
$event = $this->createDomainEventMock(); |
|
247
|
|
|
$event |
|
248
|
|
|
->expects($this->once()) |
|
249
|
|
|
->method('getMessageType') |
|
250
|
|
|
->will($this->returnValue($messageType = 'message_type')); |
|
251
|
|
|
|
|
252
|
|
|
$event |
|
253
|
|
|
->expects($this->once()) |
|
254
|
|
|
->method('setMessageType') |
|
255
|
|
|
->with($this->identicalTo($messageType)); |
|
256
|
|
|
|
|
257
|
|
|
$event |
|
258
|
|
|
->expects($this->once()) |
|
259
|
|
|
->method('getMessage') |
|
260
|
|
|
->will($this->returnValue(null)); |
|
261
|
|
|
|
|
262
|
|
|
$event |
|
263
|
|
|
->expects($this->once()) |
|
264
|
|
|
->method('getAction') |
|
265
|
|
|
->will($this->returnValue($action = 'action')); |
|
266
|
|
|
|
|
267
|
|
|
$event |
|
268
|
|
|
->expects($this->once()) |
|
269
|
|
|
->method('getObject') |
|
270
|
|
|
->will($this->returnValue($object = $this->createObjectMock())); |
|
271
|
|
|
|
|
272
|
|
|
$object |
|
|
|
|
|
|
273
|
|
|
->expects($this->once()) |
|
274
|
|
|
->method('__toString') |
|
275
|
|
|
->will($this->returnValue($property = 'property')); |
|
276
|
|
|
|
|
277
|
|
|
$event |
|
278
|
|
|
->expects($this->once()) |
|
279
|
|
|
->method('getResource') |
|
280
|
|
|
->will($this->returnValue($resource = $this->createResourceMock())); |
|
281
|
|
|
|
|
282
|
|
|
$resource |
|
283
|
|
|
->expects($this->once()) |
|
284
|
|
|
->method('getName') |
|
285
|
|
|
->will($this->returnValue($name = 'name')); |
|
286
|
|
|
|
|
287
|
|
|
$this->translator |
|
|
|
|
|
|
288
|
|
|
->expects($this->once()) |
|
289
|
|
|
->method('trans') |
|
290
|
|
|
->with( |
|
291
|
|
|
$this->identicalTo('lug.'.$name.'.'.$action.'.'.$messageType), |
|
292
|
|
|
$this->identicalTo(['%'.$name.'%' => $property]) |
|
293
|
|
|
) |
|
294
|
|
|
->will($this->returnValue($translation = 'translation')); |
|
295
|
|
|
|
|
296
|
|
|
$event |
|
297
|
|
|
->expects($this->once()) |
|
298
|
|
|
->method('setMessage') |
|
299
|
|
|
->with($this->identicalTo($translation)); |
|
300
|
|
|
|
|
301
|
|
|
$this->messageListener->addMessage($event); |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
public function testMessageWithApi() |
|
305
|
|
|
{ |
|
306
|
|
|
$this->parameterResolver |
|
307
|
|
|
->expects($this->once()) |
|
308
|
|
|
->method('resolveApi') |
|
309
|
|
|
->will($this->returnValue(true)); |
|
310
|
|
|
|
|
311
|
|
|
$event = $this->createDomainEventMock(); |
|
312
|
|
|
$event |
|
313
|
|
|
->expects($this->never()) |
|
314
|
|
|
->method('setMessage'); |
|
315
|
|
|
|
|
316
|
|
|
$this->messageListener->addMessage($event); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|TranslatorInterface |
|
321
|
|
|
*/ |
|
322
|
|
|
private function createTranslatorMock() |
|
323
|
|
|
{ |
|
324
|
|
|
return $this->createMock(TranslatorInterface::class); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|PropertyAccessorInterface |
|
329
|
|
|
*/ |
|
330
|
|
|
private function createPropertyAccessorMock() |
|
331
|
|
|
{ |
|
332
|
|
|
return $this->createMock(PropertyAccessorInterface::class); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ParameterResolverInterface |
|
337
|
|
|
*/ |
|
338
|
|
|
private function createParameterResolverMock() |
|
339
|
|
|
{ |
|
340
|
|
|
return $this->createMock(ParameterResolverInterface::class); |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|DomainEvent |
|
345
|
|
|
*/ |
|
346
|
|
|
private function createDomainEventMock() |
|
347
|
|
|
{ |
|
348
|
|
|
return $this->createMock(DomainEvent::class); |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ResourceInterface |
|
353
|
|
|
*/ |
|
354
|
|
|
private function createResourceMock() |
|
355
|
|
|
{ |
|
356
|
|
|
return $this->createMock(ResourceInterface::class); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
/** |
|
360
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|\stdClass |
|
361
|
|
|
*/ |
|
362
|
|
|
private function createObjectMock() |
|
363
|
|
|
{ |
|
364
|
|
|
return $this->getMockBuilder(\stdClass::class) |
|
365
|
|
|
->setMethods(['__toString']) |
|
366
|
|
|
->getMock(); |
|
367
|
|
|
} |
|
368
|
|
|
} |
|
369
|
|
|
|
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.