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 CrucialActionStub |
79
|
|
|
*/ |
80
|
|
|
protected $action5; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
84
|
|
|
*/ |
85
|
|
|
protected $logger; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var InformationCollected |
89
|
|
|
*/ |
90
|
|
|
protected $event; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var InformationCollected |
94
|
|
|
*/ |
95
|
|
|
protected $event2; |
96
|
|
|
|
97
|
|
|
public function setUp() |
98
|
|
|
{ |
99
|
|
|
$this->config = array( |
100
|
|
|
'default' => array( |
101
|
|
|
'email', |
102
|
|
|
), |
103
|
|
|
'content_types' => array( |
104
|
|
|
'ng_feedback_form' => array( |
105
|
|
|
'database', 'crucial', |
106
|
|
|
), |
107
|
|
|
), |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
$this->config2 = array( |
111
|
|
|
'default' => array( |
112
|
|
|
'email', |
113
|
|
|
'database', |
114
|
|
|
'email2', |
115
|
|
|
'database2', |
116
|
|
|
), |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
$this->emptyConfig = array( |
120
|
|
|
'default', |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$this->onlyDefaultConfig = array( |
124
|
|
|
'default' => array( |
125
|
|
|
'database', |
126
|
|
|
'email', |
127
|
|
|
), |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$this->action1 = $this->getMockBuilder(ActionInterface::class) |
131
|
|
|
->disableOriginalConstructor() |
132
|
|
|
->setMethods(array('act')) |
133
|
|
|
->getMock(); |
134
|
|
|
|
135
|
|
|
$this->action2 = $this->getMockBuilder(ActionInterface::class) |
136
|
|
|
->disableOriginalConstructor() |
137
|
|
|
->setMethods(array('act')) |
138
|
|
|
->getMock(); |
139
|
|
|
|
140
|
|
|
$this->action3 = $this->getMockBuilder(ActionInterface::class) |
141
|
|
|
->disableOriginalConstructor() |
142
|
|
|
->setMethods(array('act')) |
143
|
|
|
->getMock(); |
144
|
|
|
|
145
|
|
|
$this->action4 = $this->getMockBuilder(ActionInterface::class) |
146
|
|
|
->disableOriginalConstructor() |
147
|
|
|
->setMethods(array('act')) |
148
|
|
|
->getMock(); |
149
|
|
|
|
150
|
|
|
$this->action5 = new CrucialActionStub(); |
151
|
|
|
|
152
|
|
|
$this->logger = $this->getMockBuilder(LoggerInterface::class) |
153
|
|
|
->disableOriginalConstructor() |
154
|
|
|
->setMethods(array('error', 'emergency', 'alert', 'debug', 'critical', 'notice', 'info', 'warning', 'log')) |
155
|
|
|
->getMock(); |
156
|
|
|
|
157
|
|
|
$this->registry = new ActionRegistry($this->config, $this->logger); |
158
|
|
|
$this->registryForPriority = new ActionRegistry($this->config2, $this->logger); |
159
|
|
|
$this->registryWithEmptyConf = new ActionRegistry($this->emptyConfig, $this->logger); |
160
|
|
|
$this->registryWithOnlyDefaultConf = new ActionRegistry($this->onlyDefaultConfig, $this->logger); |
161
|
|
|
|
162
|
|
|
$contentType = new ContentType(array( |
163
|
|
|
'identifier' => 'ng_feedback_form', |
164
|
|
|
'fieldDefinitions' => array(), |
165
|
|
|
)); |
166
|
|
|
|
167
|
|
|
$contentType2 = new ContentType(array( |
168
|
|
|
'identifier' => 'ng_feedback_form2', |
169
|
|
|
'fieldDefinitions' => array(), |
170
|
|
|
)); |
171
|
|
|
|
172
|
|
|
$this->event = new InformationCollected( |
173
|
|
|
new DataWrapper('payload', $contentType, 'target') |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
$this->event2 = new InformationCollected( |
177
|
|
|
new DataWrapper('payload', $contentType2, 'target') |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
parent::setUp(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testAddingActions() |
184
|
|
|
{ |
185
|
|
|
$this->registry->addAction('database', $this->action1, 1); |
186
|
|
|
$this->registry->addAction('email', $this->action2, 100); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function testAct() |
190
|
|
|
{ |
191
|
|
|
$this->registry->addAction('database', $this->action1, 1); |
192
|
|
|
$this->registry->addAction('email', $this->action2, 2); |
193
|
|
|
|
194
|
|
|
$this->action1->expects($this->once()) |
195
|
|
|
->method('act') |
196
|
|
|
->with($this->event); |
197
|
|
|
|
198
|
|
|
$this->action2->expects($this->never()) |
199
|
|
|
->method('act'); |
200
|
|
|
|
201
|
|
|
$this->registry->act($this->event); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function testActWithContentTypeThatDoesNotHaveConfiguration() |
205
|
|
|
{ |
206
|
|
|
$this->registry->addAction('database', $this->action1, 1); |
207
|
|
|
$this->registry->addAction('email', $this->action2, 2); |
208
|
|
|
|
209
|
|
|
$this->action1->expects($this->never()) |
210
|
|
|
->method('act'); |
211
|
|
|
|
212
|
|
|
$this->action2->expects($this->once()) |
213
|
|
|
->method('act'); |
214
|
|
|
|
215
|
|
|
$this->registry->act($this->event2); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testActWithDefaultConfigOnly() |
219
|
|
|
{ |
220
|
|
|
$this->registryWithOnlyDefaultConf->addAction('database', $this->action1, 1); |
221
|
|
|
$this->registryWithOnlyDefaultConf->addAction('email', $this->action2, 2); |
222
|
|
|
|
223
|
|
|
$this->action1->expects($this->once()) |
224
|
|
|
->method('act'); |
225
|
|
|
|
226
|
|
|
$this->action2->expects($this->once()) |
227
|
|
|
->method('act'); |
228
|
|
|
|
229
|
|
|
$this->registryWithOnlyDefaultConf->act($this->event2); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testActWithEmptyConfig() |
233
|
|
|
{ |
234
|
|
|
$this->registryWithEmptyConf->addAction('database', $this->action1, 1); |
235
|
|
|
$this->registryWithEmptyConf->addAction('email', $this->action2, 2); |
236
|
|
|
|
237
|
|
|
$this->action1->expects($this->never()) |
238
|
|
|
->method('act'); |
239
|
|
|
|
240
|
|
|
$this->action2->expects($this->never()) |
241
|
|
|
->method('act'); |
242
|
|
|
|
243
|
|
|
$this->registryWithEmptyConf->act($this->event2); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testActWithActionFailedException() |
247
|
|
|
{ |
248
|
|
|
$this->registry->addAction('database', $this->action1, 1); |
249
|
|
|
$this->registry->addAction('email', $this->action2, 2); |
250
|
|
|
|
251
|
|
|
$this->logger->expects($this->once()) |
252
|
|
|
->method('error') |
253
|
|
|
->with('InformationCollection action database failed with reason cannot write to database'); |
254
|
|
|
|
255
|
|
|
$exception = new ActionFailedException('database', 'cannot write to database'); |
256
|
|
|
|
257
|
|
|
$this->action1->expects($this->once()) |
258
|
|
|
->method('act') |
259
|
|
|
->willThrowException($exception); |
260
|
|
|
|
261
|
|
|
$this->action2->expects($this->never()) |
262
|
|
|
->method('act'); |
263
|
|
|
|
264
|
|
|
$this->registry->act($this->event); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function testActionsAreExecutedByPriority() |
268
|
|
|
{ |
269
|
|
|
$prioritizedActions = array( |
270
|
|
|
array( |
271
|
|
|
'name' => 'email2', |
272
|
|
|
'action' => $this->action4, |
273
|
|
|
'priority' => 100, |
274
|
|
|
), |
275
|
|
|
array( |
276
|
|
|
'name' => 'database', |
277
|
|
|
'action' => $this->action1, |
278
|
|
|
'priority' => 44, |
279
|
|
|
), |
280
|
|
|
array( |
281
|
|
|
'name' => 'email', |
282
|
|
|
'action' => $this->action2, |
283
|
|
|
'priority' => 22, |
284
|
|
|
), |
285
|
|
|
array( |
286
|
|
|
'name' => 'database2', |
287
|
|
|
'action' => $this->action3, |
288
|
|
|
'priority' => 11, |
289
|
|
|
), |
290
|
|
|
); |
291
|
|
|
|
292
|
|
|
$this->registryForPriority->addAction('database', $this->action1, 44); |
293
|
|
|
$this->registryForPriority->addAction('database2', $this->action3, 11); |
294
|
|
|
$this->registryForPriority->addAction('email', $this->action2, 22); |
295
|
|
|
$this->registryForPriority->addAction('email2', $this->action4, 100); |
296
|
|
|
|
297
|
|
|
$this->action4->expects($this->once()) |
298
|
|
|
->method('act'); |
299
|
|
|
|
300
|
|
|
$this->action1->expects($this->once()) |
301
|
|
|
->method('act'); |
302
|
|
|
|
303
|
|
|
$this->action2->expects($this->once()) |
304
|
|
|
->method('act'); |
305
|
|
|
|
306
|
|
|
$this->action3->expects($this->once()) |
307
|
|
|
->method('act'); |
308
|
|
|
|
309
|
|
|
$this->registryForPriority->act($this->event); |
310
|
|
|
|
311
|
|
|
$registryReflection = new ReflectionObject($this->registryForPriority); |
312
|
|
|
$actions = $registryReflection->getProperty('actions'); |
313
|
|
|
$actions->setAccessible(true); |
314
|
|
|
|
315
|
|
|
$this->assertEquals($prioritizedActions, $actions->getValue($this->registryForPriority)); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
public function testActionsAreExecutedByPriorityWithSamePriorities() |
319
|
|
|
{ |
320
|
|
|
$prioritizedActions = array( |
321
|
|
|
array( |
322
|
|
|
'name' => 'email2', |
323
|
|
|
'action' => $this->action4, |
324
|
|
|
'priority' => 100, |
325
|
|
|
), |
326
|
|
|
array( |
327
|
|
|
'name' => 'database', |
328
|
|
|
'action' => $this->action1, |
329
|
|
|
'priority' => 44, |
330
|
|
|
), |
331
|
|
|
array( |
332
|
|
|
'name' => 'database2', |
333
|
|
|
'action' => $this->action3, |
334
|
|
|
'priority' => 11, |
335
|
|
|
), |
336
|
|
|
array( |
337
|
|
|
'name' => 'email', |
338
|
|
|
'action' => $this->action2, |
339
|
|
|
'priority' => 11, |
340
|
|
|
), |
341
|
|
|
); |
342
|
|
|
|
343
|
|
|
$this->registryForPriority->addAction('database', $this->action1, 44); |
344
|
|
|
$this->registryForPriority->addAction('database2', $this->action3, 11); |
345
|
|
|
$this->registryForPriority->addAction('email', $this->action2, 11); |
346
|
|
|
$this->registryForPriority->addAction('email2', $this->action4, 100); |
347
|
|
|
|
348
|
|
|
$this->action4->expects($this->once()) |
349
|
|
|
->method('act'); |
350
|
|
|
|
351
|
|
|
$this->action1->expects($this->once()) |
352
|
|
|
->method('act'); |
353
|
|
|
|
354
|
|
|
$this->action2->expects($this->once()) |
355
|
|
|
->method('act'); |
356
|
|
|
|
357
|
|
|
$this->action3->expects($this->once()) |
358
|
|
|
->method('act'); |
359
|
|
|
|
360
|
|
|
$this->registryForPriority->act($this->event); |
361
|
|
|
|
362
|
|
|
$registryReflection = new ReflectionObject($this->registryForPriority); |
363
|
|
|
$actions = $registryReflection->getProperty('actions'); |
364
|
|
|
$actions->setAccessible(true); |
365
|
|
|
|
366
|
|
|
$this->assertEquals($prioritizedActions, $actions->getValue($this->registryForPriority)); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
public function testSetDebugMethod() |
370
|
|
|
{ |
371
|
|
|
$this->registryForPriority->addAction('database', $this->action1, 44); |
372
|
|
|
|
373
|
|
|
$this->action1->expects($this->never()) |
374
|
|
|
->method('act'); |
375
|
|
|
|
376
|
|
|
$this->registryForPriority->setDebug(true); |
377
|
|
|
|
378
|
|
|
$registryReflection = new ReflectionObject($this->registryForPriority); |
379
|
|
|
$debug = $registryReflection->getProperty('debug'); |
380
|
|
|
$debug->setAccessible(true); |
381
|
|
|
|
382
|
|
|
$this->assertTrue($debug->getValue($this->registryForPriority)); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* @expectedException \Netgen\Bundle\InformationCollectionBundle\Exception\ActionFailedException |
387
|
|
|
* @expectedExceptionMessage InformationCollection action database failed with reason cannot write to database |
388
|
|
|
*/ |
389
|
|
|
public function testThrowExceptionWhenDebugIsTrue() |
390
|
|
|
{ |
391
|
|
|
$this->registry->addAction('database', $this->action1, 1); |
392
|
|
|
$this->registry->addAction('email', $this->action2, 2); |
393
|
|
|
|
394
|
|
|
$this->logger->expects($this->once()) |
395
|
|
|
->method('error') |
396
|
|
|
->with('InformationCollection action database failed with reason cannot write to database'); |
397
|
|
|
|
398
|
|
|
$exception = new ActionFailedException('database', 'cannot write to database'); |
399
|
|
|
|
400
|
|
|
$this->action1->expects($this->once()) |
401
|
|
|
->method('act') |
402
|
|
|
->willThrowException($exception); |
403
|
|
|
|
404
|
|
|
$this->action2->expects($this->never()) |
405
|
|
|
->method('act'); |
406
|
|
|
|
407
|
|
|
$this->registry->setDebug(true); |
408
|
|
|
$this->registry->act($this->event); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
public function testThrowExceptionWhenDebugIsFalse() |
412
|
|
|
{ |
413
|
|
|
$this->registry->addAction('crucial', $this->action5, 1); |
414
|
|
|
$this->registry->addAction('email', $this->action2, 2); |
415
|
|
|
|
416
|
|
|
$this->logger->expects($this->once()) |
417
|
|
|
->method('error') |
418
|
|
|
->with('InformationCollection action crucial failed with reason test'); |
419
|
|
|
|
420
|
|
|
$this->action2->expects($this->never()) |
421
|
|
|
->method('act'); |
422
|
|
|
|
423
|
|
|
$this->registry->setDebug(false); |
424
|
|
|
$this->registry->act($this->event); |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
|