1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\Bundle\InformationCollectionBundle\Tests\Action; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\Core\Repository\Values\ContentType\ContentType; |
6
|
|
|
use Netgen\Bundle\EzFormsBundle\Form\DataWrapper; |
7
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Action\ActionInterface; |
8
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Action\ActionRegistry; |
9
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Event\InformationCollected; |
10
|
|
|
use Netgen\Bundle\InformationCollectionBundle\Exception\ActionFailedException; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use ReflectionObject; |
14
|
|
|
|
15
|
|
|
class ActionRegistryTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ActionRegistry |
19
|
|
|
*/ |
20
|
|
|
protected $registry; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ActionRegistry |
24
|
|
|
*/ |
25
|
|
|
protected $registryForPriority; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ActionRegistry |
29
|
|
|
*/ |
30
|
|
|
protected $registryWithEmptyConf; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ActionRegistry |
34
|
|
|
*/ |
35
|
|
|
protected $registryWithOnlyDefaultConf; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $config; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $config2; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $onlyDefaultConfig; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $emptyConfig; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
59
|
|
|
*/ |
60
|
|
|
protected $action1; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
64
|
|
|
*/ |
65
|
|
|
protected $action2; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
69
|
|
|
*/ |
70
|
|
|
protected $action3; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
74
|
|
|
*/ |
75
|
|
|
protected $action4; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
79
|
|
|
*/ |
80
|
|
|
protected $logger; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var InformationCollected |
84
|
|
|
*/ |
85
|
|
|
protected $event; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var InformationCollected |
89
|
|
|
*/ |
90
|
|
|
protected $event2; |
91
|
|
|
|
92
|
|
|
public function setUp() |
93
|
|
|
{ |
94
|
|
|
$this->config = array( |
95
|
|
|
'default' => array( |
96
|
|
|
'email', |
97
|
|
|
), |
98
|
|
|
'content_types' => array( |
99
|
|
|
'ng_feedback_form' => array( |
100
|
|
|
'database', |
101
|
|
|
), |
102
|
|
|
), |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$this->config2 = array( |
106
|
|
|
'default' => array( |
107
|
|
|
'email', |
108
|
|
|
'database', |
109
|
|
|
'email2', |
110
|
|
|
'database2', |
111
|
|
|
), |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$this->emptyConfig = array( |
115
|
|
|
'default', |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$this->onlyDefaultConfig = array( |
119
|
|
|
'default' => array( |
120
|
|
|
'database', |
121
|
|
|
'email', |
122
|
|
|
), |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$this->action1 = $this->getMockBuilder(ActionInterface::class) |
126
|
|
|
->disableOriginalConstructor() |
127
|
|
|
->setMethods(array('act')) |
128
|
|
|
->getMock(); |
129
|
|
|
|
130
|
|
|
$this->action2 = $this->getMockBuilder(ActionInterface::class) |
131
|
|
|
->disableOriginalConstructor() |
132
|
|
|
->setMethods(array('act')) |
133
|
|
|
->getMock(); |
134
|
|
|
|
135
|
|
|
$this->action3 = $this->getMockBuilder(ActionInterface::class) |
136
|
|
|
->disableOriginalConstructor() |
137
|
|
|
->setMethods(array('act')) |
138
|
|
|
->getMock(); |
139
|
|
|
|
140
|
|
|
$this->action4 = $this->getMockBuilder(ActionInterface::class) |
141
|
|
|
->disableOriginalConstructor() |
142
|
|
|
->setMethods(array('act')) |
143
|
|
|
->getMock(); |
144
|
|
|
|
145
|
|
|
$this->logger = $this->getMockBuilder(LoggerInterface::class) |
146
|
|
|
->disableOriginalConstructor() |
147
|
|
|
->setMethods(array('error', 'emergency', 'alert', 'debug', 'critical', 'notice', 'info', 'warning', 'log')) |
148
|
|
|
->getMock(); |
149
|
|
|
|
150
|
|
|
$this->registry = new ActionRegistry($this->config, $this->logger); |
151
|
|
|
$this->registryForPriority = new ActionRegistry($this->config2, $this->logger); |
152
|
|
|
$this->registryWithEmptyConf = new ActionRegistry($this->emptyConfig, $this->logger); |
153
|
|
|
$this->registryWithOnlyDefaultConf = new ActionRegistry($this->onlyDefaultConfig, $this->logger); |
154
|
|
|
|
155
|
|
|
$contentType = new ContentType(array( |
156
|
|
|
'identifier' => 'ng_feedback_form', |
157
|
|
|
'fieldDefinitions' => array(), |
158
|
|
|
)); |
159
|
|
|
|
160
|
|
|
$contentType2 = new ContentType(array( |
161
|
|
|
'identifier' => 'ng_feedback_form2', |
162
|
|
|
'fieldDefinitions' => array(), |
163
|
|
|
)); |
164
|
|
|
|
165
|
|
|
$this->event = new InformationCollected( |
166
|
|
|
new DataWrapper('payload', $contentType, 'target') |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
$this->event2 = new InformationCollected( |
170
|
|
|
new DataWrapper('payload', $contentType2, 'target') |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
parent::setUp(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testAddingActions() |
177
|
|
|
{ |
178
|
|
|
$this->registry->addAction('database', $this->action1, 1); |
179
|
|
|
$this->registry->addAction('email', $this->action2, 100); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
View Code Duplication |
public function testAct() |
|
|
|
|
183
|
|
|
{ |
184
|
|
|
$this->registry->addAction('database', $this->action1, 1); |
185
|
|
|
$this->registry->addAction('email', $this->action2, 2); |
186
|
|
|
|
187
|
|
|
$this->action1->expects($this->once()) |
188
|
|
|
->method('act') |
189
|
|
|
->with($this->event); |
190
|
|
|
|
191
|
|
|
$this->action2->expects($this->never()) |
192
|
|
|
->method('act'); |
193
|
|
|
|
194
|
|
|
$this->registry->act($this->event); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
View Code Duplication |
public function testActWithContentTypeThatDoesNotHaveConfiguration() |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
$this->registry->addAction('database', $this->action1, 1); |
200
|
|
|
$this->registry->addAction('email', $this->action2, 2); |
201
|
|
|
|
202
|
|
|
$this->action1->expects($this->never()) |
203
|
|
|
->method('act'); |
204
|
|
|
|
205
|
|
|
$this->action2->expects($this->once()) |
206
|
|
|
->method('act'); |
207
|
|
|
|
208
|
|
|
$this->registry->act($this->event2); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
View Code Duplication |
public function testActWithDefaultConfigOnly() |
|
|
|
|
212
|
|
|
{ |
213
|
|
|
$this->registryWithOnlyDefaultConf->addAction('database', $this->action1, 1); |
214
|
|
|
$this->registryWithOnlyDefaultConf->addAction('email', $this->action2, 2); |
215
|
|
|
|
216
|
|
|
$this->action1->expects($this->once()) |
217
|
|
|
->method('act'); |
218
|
|
|
|
219
|
|
|
$this->action2->expects($this->once()) |
220
|
|
|
->method('act'); |
221
|
|
|
|
222
|
|
|
$this->registryWithOnlyDefaultConf->act($this->event2); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
View Code Duplication |
public function testActWithEmptyConfig() |
|
|
|
|
226
|
|
|
{ |
227
|
|
|
$this->registryWithEmptyConf->addAction('database', $this->action1, 1); |
228
|
|
|
$this->registryWithEmptyConf->addAction('email', $this->action2, 2); |
229
|
|
|
|
230
|
|
|
$this->action1->expects($this->never()) |
231
|
|
|
->method('act'); |
232
|
|
|
|
233
|
|
|
$this->action2->expects($this->never()) |
234
|
|
|
->method('act'); |
235
|
|
|
|
236
|
|
|
$this->registryWithEmptyConf->act($this->event2); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
View Code Duplication |
public function testActWithActionFailedException() |
|
|
|
|
240
|
|
|
{ |
241
|
|
|
$this->registry->addAction('database', $this->action1, 1); |
242
|
|
|
$this->registry->addAction('email', $this->action2, 2); |
243
|
|
|
|
244
|
|
|
$this->logger->expects($this->once()) |
245
|
|
|
->method('error') |
246
|
|
|
->with('InformationCollection action database failed with reason cannot write to database'); |
247
|
|
|
|
248
|
|
|
$exception = new ActionFailedException('database', 'cannot write to database'); |
249
|
|
|
|
250
|
|
|
$this->action1->expects($this->once()) |
251
|
|
|
->method('act') |
252
|
|
|
->willThrowException($exception); |
253
|
|
|
|
254
|
|
|
$this->action2->expects($this->never()) |
255
|
|
|
->method('act'); |
256
|
|
|
|
257
|
|
|
$this->registry->act($this->event); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
View Code Duplication |
public function testActionsAreExecutedByPriority() |
|
|
|
|
261
|
|
|
{ |
262
|
|
|
$prioritizedActions = array( |
263
|
|
|
array( |
264
|
|
|
'name' => 'email2', |
265
|
|
|
'action' => $this->action4, |
266
|
|
|
'priority' => 100, |
267
|
|
|
), |
268
|
|
|
array( |
269
|
|
|
'name' => 'database', |
270
|
|
|
'action' => $this->action1, |
271
|
|
|
'priority' => 44, |
272
|
|
|
), |
273
|
|
|
array( |
274
|
|
|
'name' => 'email', |
275
|
|
|
'action' => $this->action2, |
276
|
|
|
'priority' => 22, |
277
|
|
|
), |
278
|
|
|
array( |
279
|
|
|
'name' => 'database2', |
280
|
|
|
'action' => $this->action3, |
281
|
|
|
'priority' => 11, |
282
|
|
|
), |
283
|
|
|
); |
284
|
|
|
|
285
|
|
|
$this->registryForPriority->addAction('database', $this->action1, 44); |
286
|
|
|
$this->registryForPriority->addAction('database2', $this->action3, 11); |
287
|
|
|
$this->registryForPriority->addAction('email', $this->action2, 22); |
288
|
|
|
$this->registryForPriority->addAction('email2', $this->action4, 100); |
289
|
|
|
|
290
|
|
|
$this->action4->expects($this->once()) |
291
|
|
|
->method('act'); |
292
|
|
|
|
293
|
|
|
$this->action1->expects($this->once()) |
294
|
|
|
->method('act'); |
295
|
|
|
|
296
|
|
|
$this->action2->expects($this->once()) |
297
|
|
|
->method('act'); |
298
|
|
|
|
299
|
|
|
$this->action3->expects($this->once()) |
300
|
|
|
->method('act'); |
301
|
|
|
|
302
|
|
|
$this->registryForPriority->act($this->event); |
303
|
|
|
|
304
|
|
|
$registryReflection = new ReflectionObject($this->registryForPriority); |
305
|
|
|
$actions = $registryReflection->getProperty('actions'); |
306
|
|
|
$actions->setAccessible(true); |
307
|
|
|
|
308
|
|
|
$this->assertEquals($prioritizedActions, $actions->getValue($this->registryForPriority)); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
View Code Duplication |
public function testActionsAreExecutedByPriorityWithSamePriorities() |
|
|
|
|
312
|
|
|
{ |
313
|
|
|
$prioritizedActions = array( |
314
|
|
|
array( |
315
|
|
|
'name' => 'email2', |
316
|
|
|
'action' => $this->action4, |
317
|
|
|
'priority' => 100, |
318
|
|
|
), |
319
|
|
|
array( |
320
|
|
|
'name' => 'database', |
321
|
|
|
'action' => $this->action1, |
322
|
|
|
'priority' => 44, |
323
|
|
|
), |
324
|
|
|
array( |
325
|
|
|
'name' => 'database2', |
326
|
|
|
'action' => $this->action3, |
327
|
|
|
'priority' => 11, |
328
|
|
|
), |
329
|
|
|
array( |
330
|
|
|
'name' => 'email', |
331
|
|
|
'action' => $this->action2, |
332
|
|
|
'priority' => 11, |
333
|
|
|
), |
334
|
|
|
); |
335
|
|
|
|
336
|
|
|
$this->registryForPriority->addAction('database', $this->action1, 44); |
337
|
|
|
$this->registryForPriority->addAction('database2', $this->action3, 11); |
338
|
|
|
$this->registryForPriority->addAction('email', $this->action2, 11); |
339
|
|
|
$this->registryForPriority->addAction('email2', $this->action4, 100); |
340
|
|
|
|
341
|
|
|
$this->action4->expects($this->once()) |
342
|
|
|
->method('act'); |
343
|
|
|
|
344
|
|
|
$this->action1->expects($this->once()) |
345
|
|
|
->method('act'); |
346
|
|
|
|
347
|
|
|
$this->action2->expects($this->once()) |
348
|
|
|
->method('act'); |
349
|
|
|
|
350
|
|
|
$this->action3->expects($this->once()) |
351
|
|
|
->method('act'); |
352
|
|
|
|
353
|
|
|
$this->registryForPriority->act($this->event); |
354
|
|
|
|
355
|
|
|
$registryReflection = new ReflectionObject($this->registryForPriority); |
356
|
|
|
$actions = $registryReflection->getProperty('actions'); |
357
|
|
|
$actions->setAccessible(true); |
358
|
|
|
|
359
|
|
|
$this->assertEquals($prioritizedActions, $actions->getValue($this->registryForPriority)); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
public function testSetDebugMethod() |
363
|
|
|
{ |
364
|
|
|
$this->registryForPriority->addAction('database', $this->action1, 44); |
365
|
|
|
|
366
|
|
|
$this->action1->expects($this->never()) |
367
|
|
|
->method('act'); |
368
|
|
|
|
369
|
|
|
$this->registryForPriority->setDebug(true); |
370
|
|
|
|
371
|
|
|
$registryReflection = new ReflectionObject($this->registryForPriority); |
372
|
|
|
$debug = $registryReflection->getProperty('debug'); |
373
|
|
|
$debug->setAccessible(true); |
374
|
|
|
|
375
|
|
|
$this->assertTrue($debug->getValue($this->registryForPriority)); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* @expectedException \Netgen\Bundle\InformationCollectionBundle\Exception\ActionFailedException |
380
|
|
|
* @expectedExceptionMessage InformationCollection action database failed with reason cannot write to database |
381
|
|
|
|
382
|
|
|
*/ |
383
|
|
View Code Duplication |
public function testThrowExceptionWhenDebugIsTrue() |
|
|
|
|
384
|
|
|
{ |
385
|
|
|
$this->registry->addAction('database', $this->action1, 1); |
386
|
|
|
$this->registry->addAction('email', $this->action2, 2); |
387
|
|
|
|
388
|
|
|
$this->logger->expects($this->once()) |
389
|
|
|
->method('error') |
390
|
|
|
->with('InformationCollection action database failed with reason cannot write to database'); |
391
|
|
|
|
392
|
|
|
$exception = new ActionFailedException('database', 'cannot write to database'); |
393
|
|
|
|
394
|
|
|
$this->action1->expects($this->once()) |
395
|
|
|
->method('act') |
396
|
|
|
->willThrowException($exception); |
397
|
|
|
|
398
|
|
|
$this->action2->expects($this->never()) |
399
|
|
|
->method('act'); |
400
|
|
|
|
401
|
|
|
$this->registry->setDebug(true); |
402
|
|
|
$this->registry->act($this->event); |
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|
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.