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

testDeleteNotificationStopPropagationInBeforeEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 31
rs 9.424
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\Notification\BeforeCreateNotificationEvent as BeforeCreateNotificationEventInterface;
10
use eZ\Publish\API\Repository\Events\Notification\BeforeDeleteNotificationEvent as BeforeDeleteNotificationEventInterface;
11
use eZ\Publish\API\Repository\Events\Notification\BeforeMarkNotificationAsReadEvent as BeforeMarkNotificationAsReadEventInterface;
12
use eZ\Publish\API\Repository\Events\Notification\CreateNotificationEvent as CreateNotificationEventInterface;
13
use eZ\Publish\API\Repository\Events\Notification\DeleteNotificationEvent as DeleteNotificationEventInterface;
14
use eZ\Publish\API\Repository\Events\Notification\MarkNotificationAsReadEvent as MarkNotificationAsReadEventInterface;
15
use eZ\Publish\API\Repository\NotificationService as NotificationServiceInterface;
16
use eZ\Publish\API\Repository\Values\Notification\CreateStruct;
17
use eZ\Publish\API\Repository\Values\Notification\Notification;
18
use eZ\Publish\Core\Event\NotificationService;
19
20
class NotificationServiceTest extends AbstractServiceTest
21
{
22 View Code Duplication
    public function testCreateNotificationEvents()
23
    {
24
        $traceableEventDispatcher = $this->getEventDispatcher(
25
            BeforeCreateNotificationEventInterface::class,
26
            CreateNotificationEventInterface::class
27
        );
28
29
        $parameters = [
30
            $this->createMock(CreateStruct::class),
31
        ];
32
33
        $notification = $this->createMock(Notification::class);
34
        $innerServiceMock = $this->createMock(NotificationServiceInterface::class);
35
        $innerServiceMock->method('createNotification')->willReturn($notification);
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...
36
37
        $service = new NotificationService($innerServiceMock, $traceableEventDispatcher);
38
        $result = $service->createNotification(...$parameters);
39
40
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
41
42
        $this->assertSame($notification, $result);
43
        $this->assertSame($calledListeners, [
44
            [BeforeCreateNotificationEventInterface::class, 0],
45
            [CreateNotificationEventInterface::class, 0],
46
        ]);
47
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
48
    }
49
50 View Code Duplication
    public function testReturnCreateNotificationResultInBeforeEvents()
51
    {
52
        $traceableEventDispatcher = $this->getEventDispatcher(
53
            BeforeCreateNotificationEventInterface::class,
54
            CreateNotificationEventInterface::class
55
        );
56
57
        $parameters = [
58
            $this->createMock(CreateStruct::class),
59
        ];
60
61
        $notification = $this->createMock(Notification::class);
62
        $eventNotification = $this->createMock(Notification::class);
63
        $innerServiceMock = $this->createMock(NotificationServiceInterface::class);
64
        $innerServiceMock->method('createNotification')->willReturn($notification);
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...
65
66
        $traceableEventDispatcher->addListener(BeforeCreateNotificationEventInterface::class, function (BeforeCreateNotificationEventInterface $event) use ($eventNotification) {
67
            $event->setNotification($eventNotification);
68
        }, 10);
69
70
        $service = new NotificationService($innerServiceMock, $traceableEventDispatcher);
71
        $result = $service->createNotification(...$parameters);
72
73
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
74
75
        $this->assertSame($eventNotification, $result);
76
        $this->assertSame($calledListeners, [
77
            [BeforeCreateNotificationEventInterface::class, 10],
78
            [BeforeCreateNotificationEventInterface::class, 0],
79
            [CreateNotificationEventInterface::class, 0],
80
        ]);
81
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
82
    }
83
84 View Code Duplication
    public function testCreateNotificationStopPropagationInBeforeEvents()
85
    {
86
        $traceableEventDispatcher = $this->getEventDispatcher(
87
            BeforeCreateNotificationEventInterface::class,
88
            CreateNotificationEventInterface::class
89
        );
90
91
        $parameters = [
92
            $this->createMock(CreateStruct::class),
93
        ];
94
95
        $notification = $this->createMock(Notification::class);
96
        $eventNotification = $this->createMock(Notification::class);
97
        $innerServiceMock = $this->createMock(NotificationServiceInterface::class);
98
        $innerServiceMock->method('createNotification')->willReturn($notification);
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...
99
100
        $traceableEventDispatcher->addListener(BeforeCreateNotificationEventInterface::class, function (BeforeCreateNotificationEventInterface $event) use ($eventNotification) {
101
            $event->setNotification($eventNotification);
102
            $event->stopPropagation();
103
        }, 10);
104
105
        $service = new NotificationService($innerServiceMock, $traceableEventDispatcher);
106
        $result = $service->createNotification(...$parameters);
107
108
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
109
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
110
111
        $this->assertSame($eventNotification, $result);
112
        $this->assertSame($calledListeners, [
113
            [BeforeCreateNotificationEventInterface::class, 10],
114
        ]);
115
        $this->assertSame($notCalledListeners, [
116
            [BeforeCreateNotificationEventInterface::class, 0],
117
            [CreateNotificationEventInterface::class, 0],
118
        ]);
119
    }
120
121
    public function testDeleteNotificationEvents()
122
    {
123
        $traceableEventDispatcher = $this->getEventDispatcher(
124
            BeforeDeleteNotificationEventInterface::class,
125
            DeleteNotificationEventInterface::class
126
        );
127
128
        $parameters = [
129
            $this->createMock(Notification::class),
130
        ];
131
132
        $innerServiceMock = $this->createMock(NotificationServiceInterface::class);
133
134
        $service = new NotificationService($innerServiceMock, $traceableEventDispatcher);
135
        $service->deleteNotification(...$parameters);
136
137
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
138
139
        $this->assertSame($calledListeners, [
140
            [BeforeDeleteNotificationEventInterface::class, 0],
141
            [DeleteNotificationEventInterface::class, 0],
142
        ]);
143
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
144
    }
145
146
    public function testDeleteNotificationStopPropagationInBeforeEvents()
147
    {
148
        $traceableEventDispatcher = $this->getEventDispatcher(
149
            BeforeDeleteNotificationEventInterface::class,
150
            DeleteNotificationEventInterface::class
151
        );
152
153
        $parameters = [
154
            $this->createMock(Notification::class),
155
        ];
156
157
        $innerServiceMock = $this->createMock(NotificationServiceInterface::class);
158
159
        $traceableEventDispatcher->addListener(BeforeDeleteNotificationEventInterface::class, function (BeforeDeleteNotificationEventInterface $event) {
160
            $event->stopPropagation();
161
        }, 10);
162
163
        $service = new NotificationService($innerServiceMock, $traceableEventDispatcher);
164
        $service->deleteNotification(...$parameters);
165
166
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
167
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
168
169
        $this->assertSame($calledListeners, [
170
            [BeforeDeleteNotificationEventInterface::class, 10],
171
        ]);
172
        $this->assertSame($notCalledListeners, [
173
            [BeforeDeleteNotificationEventInterface::class, 0],
174
            [DeleteNotificationEventInterface::class, 0],
175
        ]);
176
    }
177
178
    public function testMarkNotificationAsReadEvents()
179
    {
180
        $traceableEventDispatcher = $this->getEventDispatcher(
181
            BeforeMarkNotificationAsReadEventInterface::class,
182
            MarkNotificationAsReadEventInterface::class
183
        );
184
185
        $parameters = [
186
            $this->createMock(Notification::class),
187
        ];
188
189
        $innerServiceMock = $this->createMock(NotificationServiceInterface::class);
190
191
        $service = new NotificationService($innerServiceMock, $traceableEventDispatcher);
192
        $service->markNotificationAsRead(...$parameters);
193
194
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
195
196
        $this->assertSame($calledListeners, [
197
            [BeforeMarkNotificationAsReadEventInterface::class, 0],
198
            [MarkNotificationAsReadEventInterface::class, 0],
199
        ]);
200
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
201
    }
202
203
    public function testMarkNotificationAsReadStopPropagationInBeforeEvents()
204
    {
205
        $traceableEventDispatcher = $this->getEventDispatcher(
206
            BeforeMarkNotificationAsReadEventInterface::class,
207
            MarkNotificationAsReadEventInterface::class
208
        );
209
210
        $parameters = [
211
            $this->createMock(Notification::class),
212
        ];
213
214
        $innerServiceMock = $this->createMock(NotificationServiceInterface::class);
215
216
        $traceableEventDispatcher->addListener(BeforeMarkNotificationAsReadEventInterface::class, function (BeforeMarkNotificationAsReadEventInterface $event) {
217
            $event->stopPropagation();
218
        }, 10);
219
220
        $service = new NotificationService($innerServiceMock, $traceableEventDispatcher);
221
        $service->markNotificationAsRead(...$parameters);
222
223
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
224
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
225
226
        $this->assertSame($calledListeners, [
227
            [BeforeMarkNotificationAsReadEventInterface::class, 10],
228
        ]);
229
        $this->assertSame($notCalledListeners, [
230
            [BeforeMarkNotificationAsReadEventInterface::class, 0],
231
            [MarkNotificationAsReadEventInterface::class, 0],
232
        ]);
233
    }
234
}
235