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

testCreateStopPropagationInBeforeEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38

Duplication

Lines 38
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 38
loc 38
rs 9.312
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\URLWildcardService as URLWildcardServiceInterface;
10
use eZ\Publish\API\Repository\Values\Content\URLWildcard;
11
use eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult;
12
use eZ\Publish\Core\Event\URLWildcardService;
13
use eZ\Publish\Core\Event\URLWildcard\BeforeCreateEvent;
14
use eZ\Publish\Core\Event\URLWildcard\BeforeRemoveEvent;
15
use eZ\Publish\Core\Event\URLWildcard\BeforeTranslateEvent;
16
use eZ\Publish\Core\Event\URLWildcard\URLWildcardEvents;
17
18
class URLWildcardServiceTest extends AbstractServiceTest
19
{
20
    public function testRemoveEvents()
21
    {
22
        $traceableEventDispatcher = $this->getEventDispatcher(
23
            URLWildcardEvents::BEFORE_REMOVE,
24
            URLWildcardEvents::REMOVE
25
        );
26
27
        $parameters = [
28
            $this->createMock(URLWildcard::class),
29
        ];
30
31
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
32
33
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
34
        $service->remove(...$parameters);
35
36
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
37
38
        $this->assertSame($calledListeners, [
39
            [URLWildcardEvents::BEFORE_REMOVE, 0],
40
            [URLWildcardEvents::REMOVE, 0],
41
        ]);
42
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
43
    }
44
45
    public function testRemoveStopPropagationInBeforeEvents()
46
    {
47
        $traceableEventDispatcher = $this->getEventDispatcher(
48
            URLWildcardEvents::BEFORE_REMOVE,
49
            URLWildcardEvents::REMOVE
50
        );
51
52
        $parameters = [
53
            $this->createMock(URLWildcard::class),
54
        ];
55
56
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
57
58
        $traceableEventDispatcher->addListener(URLWildcardEvents::BEFORE_REMOVE, function (BeforeRemoveEvent $event) {
59
            $event->stopPropagation();
60
        }, 10);
61
62
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
63
        $service->remove(...$parameters);
64
65
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
66
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
67
68
        $this->assertSame($calledListeners, [
69
            [URLWildcardEvents::BEFORE_REMOVE, 10],
70
        ]);
71
        $this->assertSame($notCalledListeners, [
72
            [URLWildcardEvents::REMOVE, 0],
73
            [URLWildcardEvents::BEFORE_REMOVE, 0],
74
        ]);
75
    }
76
77 View Code Duplication
    public function testCreateEvents()
78
    {
79
        $traceableEventDispatcher = $this->getEventDispatcher(
80
            URLWildcardEvents::BEFORE_CREATE,
81
            URLWildcardEvents::CREATE
82
        );
83
84
        $parameters = [
85
            'random_value_5cff79c316c1f5.58580131',
86
            'random_value_5cff79c316c223.93334332',
87
            'random_value_5cff79c316c237.08397355',
88
        ];
89
90
        $urlWildcard = $this->createMock(URLWildcard::class);
91
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
92
        $innerServiceMock->method('create')->willReturn($urlWildcard);
93
94
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
95
        $result = $service->create(...$parameters);
0 ignored issues
show
Bug introduced by
The call to create() misses a required argument $destinationUrl.

This check looks for function calls that miss required arguments.

Loading history...
96
97
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
98
99
        $this->assertSame($urlWildcard, $result);
100
        $this->assertSame($calledListeners, [
101
            [URLWildcardEvents::BEFORE_CREATE, 0],
102
            [URLWildcardEvents::CREATE, 0],
103
        ]);
104
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
105
    }
106
107 View Code Duplication
    public function testReturnCreateResultInBeforeEvents()
108
    {
109
        $traceableEventDispatcher = $this->getEventDispatcher(
110
            URLWildcardEvents::BEFORE_CREATE,
111
            URLWildcardEvents::CREATE
112
        );
113
114
        $parameters = [
115
            'random_value_5cff79c316c2d5.26653678',
116
            'random_value_5cff79c316c2e7.55400833',
117
            'random_value_5cff79c316c2f8.59874187',
118
        ];
119
120
        $urlWildcard = $this->createMock(URLWildcard::class);
121
        $eventUrlWildcard = $this->createMock(URLWildcard::class);
122
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
123
        $innerServiceMock->method('create')->willReturn($urlWildcard);
124
125
        $traceableEventDispatcher->addListener(URLWildcardEvents::BEFORE_CREATE, function (BeforeCreateEvent $event) use ($eventUrlWildcard) {
126
            $event->setUrlWildcard($eventUrlWildcard);
127
        }, 10);
128
129
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
130
        $result = $service->create(...$parameters);
0 ignored issues
show
Bug introduced by
The call to create() misses a required argument $destinationUrl.

This check looks for function calls that miss required arguments.

Loading history...
131
132
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
133
134
        $this->assertSame($eventUrlWildcard, $result);
135
        $this->assertSame($calledListeners, [
136
            [URLWildcardEvents::BEFORE_CREATE, 10],
137
            [URLWildcardEvents::BEFORE_CREATE, 0],
138
            [URLWildcardEvents::CREATE, 0],
139
        ]);
140
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
141
    }
142
143 View Code Duplication
    public function testCreateStopPropagationInBeforeEvents()
144
    {
145
        $traceableEventDispatcher = $this->getEventDispatcher(
146
            URLWildcardEvents::BEFORE_CREATE,
147
            URLWildcardEvents::CREATE
148
        );
149
150
        $parameters = [
151
            'random_value_5cff79c316c359.46056769',
152
            'random_value_5cff79c316c361.53134429',
153
            'random_value_5cff79c316c374.82657815',
154
        ];
155
156
        $urlWildcard = $this->createMock(URLWildcard::class);
157
        $eventUrlWildcard = $this->createMock(URLWildcard::class);
158
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
159
        $innerServiceMock->method('create')->willReturn($urlWildcard);
160
161
        $traceableEventDispatcher->addListener(URLWildcardEvents::BEFORE_CREATE, function (BeforeCreateEvent $event) use ($eventUrlWildcard) {
162
            $event->setUrlWildcard($eventUrlWildcard);
163
            $event->stopPropagation();
164
        }, 10);
165
166
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
167
        $result = $service->create(...$parameters);
0 ignored issues
show
Bug introduced by
The call to create() misses a required argument $destinationUrl.

This check looks for function calls that miss required arguments.

Loading history...
168
169
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
170
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
171
172
        $this->assertSame($eventUrlWildcard, $result);
173
        $this->assertSame($calledListeners, [
174
            [URLWildcardEvents::BEFORE_CREATE, 10],
175
        ]);
176
        $this->assertSame($notCalledListeners, [
177
            [URLWildcardEvents::CREATE, 0],
178
            [URLWildcardEvents::BEFORE_CREATE, 0],
179
        ]);
180
    }
181
182
    public function testTranslateEvents()
183
    {
184
        $traceableEventDispatcher = $this->getEventDispatcher(
185
            URLWildcardEvents::BEFORE_TRANSLATE,
186
            URLWildcardEvents::TRANSLATE
187
        );
188
189
        $parameters = [
190
            'random_value_5cff79c316cfa7.72466150',
191
        ];
192
193
        $result = $this->createMock(URLWildcardTranslationResult::class);
194
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
195
        $innerServiceMock->method('translate')->willReturn($result);
196
197
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
198
        $result = $service->translate(...$parameters);
199
200
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
201
202
        $this->assertSame($result, $result);
203
        $this->assertSame($calledListeners, [
204
            [URLWildcardEvents::BEFORE_TRANSLATE, 0],
205
            [URLWildcardEvents::TRANSLATE, 0],
206
        ]);
207
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
208
    }
209
210 View Code Duplication
    public function testReturnTranslateResultInBeforeEvents()
211
    {
212
        $traceableEventDispatcher = $this->getEventDispatcher(
213
            URLWildcardEvents::BEFORE_TRANSLATE,
214
            URLWildcardEvents::TRANSLATE
215
        );
216
217
        $parameters = [
218
            'random_value_5cff79c316d370.25863709',
219
        ];
220
221
        $result = $this->createMock(URLWildcardTranslationResult::class);
222
        $eventResult = $this->createMock(URLWildcardTranslationResult::class);
223
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
224
        $innerServiceMock->method('translate')->willReturn($result);
225
226
        $traceableEventDispatcher->addListener(URLWildcardEvents::BEFORE_TRANSLATE, function (BeforeTranslateEvent $event) use ($eventResult) {
227
            $event->setResult($eventResult);
228
        }, 10);
229
230
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
231
        $result = $service->translate(...$parameters);
232
233
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
234
235
        $this->assertSame($eventResult, $result);
236
        $this->assertSame($calledListeners, [
237
            [URLWildcardEvents::BEFORE_TRANSLATE, 10],
238
            [URLWildcardEvents::BEFORE_TRANSLATE, 0],
239
            [URLWildcardEvents::TRANSLATE, 0],
240
        ]);
241
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
242
    }
243
244 View Code Duplication
    public function testTranslateStopPropagationInBeforeEvents()
245
    {
246
        $traceableEventDispatcher = $this->getEventDispatcher(
247
            URLWildcardEvents::BEFORE_TRANSLATE,
248
            URLWildcardEvents::TRANSLATE
249
        );
250
251
        $parameters = [
252
            'random_value_5cff79c316d3f9.73226122',
253
        ];
254
255
        $result = $this->createMock(URLWildcardTranslationResult::class);
256
        $eventResult = $this->createMock(URLWildcardTranslationResult::class);
257
        $innerServiceMock = $this->createMock(URLWildcardServiceInterface::class);
258
        $innerServiceMock->method('translate')->willReturn($result);
259
260
        $traceableEventDispatcher->addListener(URLWildcardEvents::BEFORE_TRANSLATE, function (BeforeTranslateEvent $event) use ($eventResult) {
261
            $event->setResult($eventResult);
262
            $event->stopPropagation();
263
        }, 10);
264
265
        $service = new URLWildcardService($innerServiceMock, $traceableEventDispatcher);
266
        $result = $service->translate(...$parameters);
267
268
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
269
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
270
271
        $this->assertSame($eventResult, $result);
272
        $this->assertSame($calledListeners, [
273
            [URLWildcardEvents::BEFORE_TRANSLATE, 10],
274
        ]);
275
        $this->assertSame($notCalledListeners, [
276
            [URLWildcardEvents::TRANSLATE, 0],
277
            [URLWildcardEvents::BEFORE_TRANSLATE, 0],
278
        ]);
279
    }
280
}
281