Completed
Push — ezp-30616-follow-up ( 2e0607...b74676 )
by
unknown
35:08 queued 20:12
created

TrashServiceTest::testRecoverEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 28
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 28
loc 28
rs 9.472
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
namespace eZ\Publish\Core\Event\Tests;
8
9
use eZ\Publish\API\Repository\Events\Trash\BeforeDeleteTrashItemEvent as BeforeDeleteTrashItemEventInterface;
10
use eZ\Publish\API\Repository\Events\Trash\BeforeEmptyTrashEvent as BeforeEmptyTrashEventInterface;
11
use eZ\Publish\API\Repository\Events\Trash\BeforeRecoverEvent as BeforeRecoverEventInterface;
12
use eZ\Publish\API\Repository\Events\Trash\BeforeTrashEvent as BeforeTrashEventInterface;
13
use eZ\Publish\API\Repository\Events\Trash\DeleteTrashItemEvent as DeleteTrashItemEventInterface;
14
use eZ\Publish\API\Repository\Events\Trash\EmptyTrashEvent as EmptyTrashEventInterface;
15
use eZ\Publish\API\Repository\Events\Trash\RecoverEvent as RecoverEventInterface;
16
use eZ\Publish\API\Repository\Events\Trash\TrashEvent as TrashEventInterface;
17
use eZ\Publish\API\Repository\TrashService as TrashServiceInterface;
18
use eZ\Publish\API\Repository\Values\Content\Location;
19
use eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResult;
20
use eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResultList;
21
use eZ\Publish\API\Repository\Values\Content\TrashItem;
22
use eZ\Publish\Core\Event\TrashService;
23
24
class TrashServiceTest extends AbstractServiceTest
25
{
26 View Code Duplication
    public function testEmptyTrashEvents()
27
    {
28
        $traceableEventDispatcher = $this->getEventDispatcher(
29
            BeforeEmptyTrashEventInterface::class,
30
            EmptyTrashEventInterface::class
31
        );
32
33
        $parameters = [
34
        ];
35
36
        $resultList = $this->createMock(TrashItemDeleteResultList::class);
37
        $innerServiceMock = $this->createMock(TrashServiceInterface::class);
38
        $innerServiceMock->method('emptyTrash')->willReturn($resultList);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
40
        $service = new TrashService($innerServiceMock, $traceableEventDispatcher);
41
        $result = $service->emptyTrash(...$parameters);
42
43
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
44
45
        $this->assertSame($resultList, $result);
46
        $this->assertSame($calledListeners, [
47
            [BeforeEmptyTrashEventInterface::class, 0],
48
            [EmptyTrashEventInterface::class, 0],
49
        ]);
50
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
51
    }
52
53 View Code Duplication
    public function testReturnEmptyTrashResultInBeforeEvents()
54
    {
55
        $traceableEventDispatcher = $this->getEventDispatcher(
56
            BeforeEmptyTrashEventInterface::class,
57
            EmptyTrashEventInterface::class
58
        );
59
60
        $parameters = [
61
        ];
62
63
        $resultList = $this->createMock(TrashItemDeleteResultList::class);
64
        $eventResultList = $this->createMock(TrashItemDeleteResultList::class);
65
        $innerServiceMock = $this->createMock(TrashServiceInterface::class);
66
        $innerServiceMock->method('emptyTrash')->willReturn($resultList);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
68
        $traceableEventDispatcher->addListener(BeforeEmptyTrashEventInterface::class, function (BeforeEmptyTrashEventInterface $event) use ($eventResultList) {
69
            $event->setResultList($eventResultList);
70
        }, 10);
71
72
        $service = new TrashService($innerServiceMock, $traceableEventDispatcher);
73
        $result = $service->emptyTrash(...$parameters);
74
75
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
76
77
        $this->assertSame($eventResultList, $result);
78
        $this->assertSame($calledListeners, [
79
            [BeforeEmptyTrashEventInterface::class, 10],
80
            [BeforeEmptyTrashEventInterface::class, 0],
81
            [EmptyTrashEventInterface::class, 0],
82
        ]);
83
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
84
    }
85
86 View Code Duplication
    public function testEmptyTrashStopPropagationInBeforeEvents()
87
    {
88
        $traceableEventDispatcher = $this->getEventDispatcher(
89
            BeforeEmptyTrashEventInterface::class,
90
            EmptyTrashEventInterface::class
91
        );
92
93
        $parameters = [
94
        ];
95
96
        $resultList = $this->createMock(TrashItemDeleteResultList::class);
97
        $eventResultList = $this->createMock(TrashItemDeleteResultList::class);
98
        $innerServiceMock = $this->createMock(TrashServiceInterface::class);
99
        $innerServiceMock->method('emptyTrash')->willReturn($resultList);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
101
        $traceableEventDispatcher->addListener(BeforeEmptyTrashEventInterface::class, function (BeforeEmptyTrashEventInterface $event) use ($eventResultList) {
102
            $event->setResultList($eventResultList);
103
            $event->stopPropagation();
104
        }, 10);
105
106
        $service = new TrashService($innerServiceMock, $traceableEventDispatcher);
107
        $result = $service->emptyTrash(...$parameters);
108
109
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
110
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
111
112
        $this->assertSame($eventResultList, $result);
113
        $this->assertSame($calledListeners, [
114
            [BeforeEmptyTrashEventInterface::class, 10],
115
        ]);
116
        $this->assertSame($notCalledListeners, [
117
            [BeforeEmptyTrashEventInterface::class, 0],
118
            [EmptyTrashEventInterface::class, 0],
119
        ]);
120
    }
121
122 View Code Duplication
    public function testTrashEvents()
123
    {
124
        $traceableEventDispatcher = $this->getEventDispatcher(
125
            BeforeTrashEventInterface::class,
126
            TrashEventInterface::class
127
        );
128
129
        $parameters = [
130
            $this->createMock(Location::class),
131
        ];
132
133
        $trashItem = $this->createMock(TrashItem::class);
134
        $innerServiceMock = $this->createMock(TrashServiceInterface::class);
135
        $innerServiceMock->method('trash')->willReturn($trashItem);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
137
        $service = new TrashService($innerServiceMock, $traceableEventDispatcher);
138
        $result = $service->trash(...$parameters);
139
140
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
141
142
        $this->assertSame($trashItem, $result);
143
        $this->assertSame($calledListeners, [
144
            [BeforeTrashEventInterface::class, 0],
145
            [TrashEventInterface::class, 0],
146
        ]);
147
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
148
    }
149
150 View Code Duplication
    public function testReturnTrashResultInBeforeEvents()
151
    {
152
        $traceableEventDispatcher = $this->getEventDispatcher(
153
            BeforeTrashEventInterface::class,
154
            TrashEventInterface::class
155
        );
156
157
        $parameters = [
158
            $this->createMock(Location::class),
159
        ];
160
161
        $trashItem = $this->createMock(TrashItem::class);
162
        $eventTrashItem = $this->createMock(TrashItem::class);
163
        $innerServiceMock = $this->createMock(TrashServiceInterface::class);
164
        $innerServiceMock->method('trash')->willReturn($trashItem);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
165
166
        $traceableEventDispatcher->addListener(BeforeTrashEventInterface::class, function (BeforeTrashEventInterface $event) use ($eventTrashItem) {
167
            $event->setResult($eventTrashItem);
168
        }, 10);
169
170
        $service = new TrashService($innerServiceMock, $traceableEventDispatcher);
171
        $result = $service->trash(...$parameters);
172
173
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
174
175
        $this->assertSame($eventTrashItem, $result);
176
        $this->assertSame($calledListeners, [
177
            [BeforeTrashEventInterface::class, 10],
178
            [BeforeTrashEventInterface::class, 0],
179
            [TrashEventInterface::class, 0],
180
        ]);
181
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
182
    }
183
184 View Code Duplication
    public function testTrashStopPropagationInBeforeEvents()
185
    {
186
        $traceableEventDispatcher = $this->getEventDispatcher(
187
            BeforeTrashEventInterface::class,
188
            TrashEventInterface::class
189
        );
190
191
        $parameters = [
192
            $this->createMock(Location::class),
193
        ];
194
195
        $trashItem = $this->createMock(TrashItem::class);
196
        $eventTrashItem = $this->createMock(TrashItem::class);
197
        $innerServiceMock = $this->createMock(TrashServiceInterface::class);
198
        $innerServiceMock->method('trash')->willReturn($trashItem);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
199
200
        $traceableEventDispatcher->addListener(BeforeTrashEventInterface::class, function (BeforeTrashEventInterface $event) use ($eventTrashItem) {
201
            $event->setResult($eventTrashItem);
202
            $event->stopPropagation();
203
        }, 10);
204
205
        $service = new TrashService($innerServiceMock, $traceableEventDispatcher);
206
        $result = $service->trash(...$parameters);
207
208
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
209
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
210
211
        $this->assertSame($eventTrashItem, $result);
212
        $this->assertSame($calledListeners, [
213
            [BeforeTrashEventInterface::class, 10],
214
        ]);
215
        $this->assertSame($notCalledListeners, [
216
            [BeforeTrashEventInterface::class, 0],
217
            [TrashEventInterface::class, 0],
218
        ]);
219
    }
220
221 View Code Duplication
    public function testRecoverEvents()
222
    {
223
        $traceableEventDispatcher = $this->getEventDispatcher(
224
            BeforeRecoverEventInterface::class,
225
            RecoverEventInterface::class
226
        );
227
228
        $parameters = [
229
            $this->createMock(TrashItem::class),
230
            $this->createMock(Location::class),
231
        ];
232
233
        $location = $this->createMock(Location::class);
234
        $innerServiceMock = $this->createMock(TrashServiceInterface::class);
235
        $innerServiceMock->method('recover')->willReturn($location);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
236
237
        $service = new TrashService($innerServiceMock, $traceableEventDispatcher);
238
        $result = $service->recover(...$parameters);
239
240
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
241
242
        $this->assertSame($location, $result);
243
        $this->assertSame($calledListeners, [
244
            [BeforeRecoverEventInterface::class, 0],
245
            [RecoverEventInterface::class, 0],
246
        ]);
247
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
248
    }
249
250 View Code Duplication
    public function testReturnRecoverResultInBeforeEvents()
251
    {
252
        $traceableEventDispatcher = $this->getEventDispatcher(
253
            BeforeRecoverEventInterface::class,
254
            RecoverEventInterface::class
255
        );
256
257
        $parameters = [
258
            $this->createMock(TrashItem::class),
259
            $this->createMock(Location::class),
260
        ];
261
262
        $location = $this->createMock(Location::class);
263
        $eventLocation = $this->createMock(Location::class);
264
        $innerServiceMock = $this->createMock(TrashServiceInterface::class);
265
        $innerServiceMock->method('recover')->willReturn($location);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
266
267
        $traceableEventDispatcher->addListener(BeforeRecoverEventInterface::class, function (BeforeRecoverEventInterface $event) use ($eventLocation) {
268
            $event->setLocation($eventLocation);
269
        }, 10);
270
271
        $service = new TrashService($innerServiceMock, $traceableEventDispatcher);
272
        $result = $service->recover(...$parameters);
273
274
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
275
276
        $this->assertSame($eventLocation, $result);
277
        $this->assertSame($calledListeners, [
278
            [BeforeRecoverEventInterface::class, 10],
279
            [BeforeRecoverEventInterface::class, 0],
280
            [RecoverEventInterface::class, 0],
281
        ]);
282
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
283
    }
284
285 View Code Duplication
    public function testRecoverStopPropagationInBeforeEvents()
286
    {
287
        $traceableEventDispatcher = $this->getEventDispatcher(
288
            BeforeRecoverEventInterface::class,
289
            RecoverEventInterface::class
290
        );
291
292
        $parameters = [
293
            $this->createMock(TrashItem::class),
294
            $this->createMock(Location::class),
295
        ];
296
297
        $location = $this->createMock(Location::class);
298
        $eventLocation = $this->createMock(Location::class);
299
        $innerServiceMock = $this->createMock(TrashServiceInterface::class);
300
        $innerServiceMock->method('recover')->willReturn($location);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
301
302
        $traceableEventDispatcher->addListener(BeforeRecoverEventInterface::class, function (BeforeRecoverEventInterface $event) use ($eventLocation) {
303
            $event->setLocation($eventLocation);
304
            $event->stopPropagation();
305
        }, 10);
306
307
        $service = new TrashService($innerServiceMock, $traceableEventDispatcher);
308
        $result = $service->recover(...$parameters);
309
310
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
311
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
312
313
        $this->assertSame($eventLocation, $result);
314
        $this->assertSame($calledListeners, [
315
            [BeforeRecoverEventInterface::class, 10],
316
        ]);
317
        $this->assertSame($notCalledListeners, [
318
            [BeforeRecoverEventInterface::class, 0],
319
            [RecoverEventInterface::class, 0],
320
        ]);
321
    }
322
323 View Code Duplication
    public function testDeleteTrashItemEvents()
324
    {
325
        $traceableEventDispatcher = $this->getEventDispatcher(
326
            BeforeDeleteTrashItemEventInterface::class,
327
            DeleteTrashItemEventInterface::class
328
        );
329
330
        $parameters = [
331
            $this->createMock(TrashItem::class),
332
        ];
333
334
        $result = $this->createMock(TrashItemDeleteResult::class);
335
        $innerServiceMock = $this->createMock(TrashServiceInterface::class);
336
        $innerServiceMock->method('deleteTrashItem')->willReturn($result);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
337
338
        $service = new TrashService($innerServiceMock, $traceableEventDispatcher);
339
        $result = $service->deleteTrashItem(...$parameters);
340
341
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
342
343
        $this->assertSame($result, $result);
344
        $this->assertSame($calledListeners, [
345
            [BeforeDeleteTrashItemEventInterface::class, 0],
346
            [DeleteTrashItemEventInterface::class, 0],
347
        ]);
348
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
349
    }
350
351 View Code Duplication
    public function testReturnDeleteTrashItemResultInBeforeEvents()
352
    {
353
        $traceableEventDispatcher = $this->getEventDispatcher(
354
            BeforeDeleteTrashItemEventInterface::class,
355
            DeleteTrashItemEventInterface::class
356
        );
357
358
        $parameters = [
359
            $this->createMock(TrashItem::class),
360
        ];
361
362
        $result = $this->createMock(TrashItemDeleteResult::class);
363
        $eventResult = $this->createMock(TrashItemDeleteResult::class);
364
        $innerServiceMock = $this->createMock(TrashServiceInterface::class);
365
        $innerServiceMock->method('deleteTrashItem')->willReturn($result);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
366
367
        $traceableEventDispatcher->addListener(BeforeDeleteTrashItemEventInterface::class, function (BeforeDeleteTrashItemEventInterface $event) use ($eventResult) {
368
            $event->setResult($eventResult);
369
        }, 10);
370
371
        $service = new TrashService($innerServiceMock, $traceableEventDispatcher);
372
        $result = $service->deleteTrashItem(...$parameters);
373
374
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
375
376
        $this->assertSame($eventResult, $result);
377
        $this->assertSame($calledListeners, [
378
            [BeforeDeleteTrashItemEventInterface::class, 10],
379
            [BeforeDeleteTrashItemEventInterface::class, 0],
380
            [DeleteTrashItemEventInterface::class, 0],
381
        ]);
382
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
383
    }
384
385 View Code Duplication
    public function testDeleteTrashItemStopPropagationInBeforeEvents()
386
    {
387
        $traceableEventDispatcher = $this->getEventDispatcher(
388
            BeforeDeleteTrashItemEventInterface::class,
389
            DeleteTrashItemEventInterface::class
390
        );
391
392
        $parameters = [
393
            $this->createMock(TrashItem::class),
394
        ];
395
396
        $result = $this->createMock(TrashItemDeleteResult::class);
397
        $eventResult = $this->createMock(TrashItemDeleteResult::class);
398
        $innerServiceMock = $this->createMock(TrashServiceInterface::class);
399
        $innerServiceMock->method('deleteTrashItem')->willReturn($result);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
400
401
        $traceableEventDispatcher->addListener(BeforeDeleteTrashItemEventInterface::class, function (BeforeDeleteTrashItemEventInterface $event) use ($eventResult) {
402
            $event->setResult($eventResult);
403
            $event->stopPropagation();
404
        }, 10);
405
406
        $service = new TrashService($innerServiceMock, $traceableEventDispatcher);
407
        $result = $service->deleteTrashItem(...$parameters);
408
409
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
410
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
411
412
        $this->assertSame($eventResult, $result);
413
        $this->assertSame($calledListeners, [
414
            [BeforeDeleteTrashItemEventInterface::class, 10],
415
        ]);
416
        $this->assertSame($notCalledListeners, [
417
            [BeforeDeleteTrashItemEventInterface::class, 0],
418
            [DeleteTrashItemEventInterface::class, 0],
419
        ]);
420
    }
421
}
422