Completed
Push — ezp-30616 ( 8e069f...7bf8e8 )
by
unknown
23:24 queued 01:36
created

NotificationServiceTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 215
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 6

7 Methods

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