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

URLAliasServiceTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 338
Duplicated Lines 63.91 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A testCreateGlobalUrlAliasEvents() 31 31 1
A testReturnCreateGlobalUrlAliasResultInBeforeEvents() 37 37 1
A testCreateGlobalUrlAliasStopPropagationInBeforeEvents() 40 40 1
A testRefreshSystemUrlAliasesForLocationEvents() 0 24 1
A testRefreshSystemUrlAliasesForLocationStopPropagationInBeforeEvents() 0 31 1
A testCreateUrlAliasEvents() 31 31 1
A testReturnCreateUrlAliasResultInBeforeEvents() 37 37 1
A testCreateUrlAliasStopPropagationInBeforeEvents() 40 40 1
A testRemoveAliasesEvents() 0 24 1
A testRemoveAliasesStopPropagationInBeforeEvents() 0 31 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\URLAliasService as URLAliasServiceInterface;
10
use eZ\Publish\API\Repository\Values\Content\Location;
11
use eZ\Publish\API\Repository\Values\Content\URLAlias;
12
use eZ\Publish\Core\Event\URLAliasService;
13
use eZ\Publish\Core\Event\URLAlias\BeforeCreateGlobalUrlAliasEvent;
14
use eZ\Publish\Core\Event\URLAlias\BeforeCreateUrlAliasEvent;
15
use eZ\Publish\Core\Event\URLAlias\BeforeRefreshSystemUrlAliasesForLocationEvent;
16
use eZ\Publish\Core\Event\URLAlias\BeforeRemoveAliasesEvent;
17
use eZ\Publish\Core\Event\URLAlias\URLAliasEvents;
18
19
class URLAliasServiceTest extends AbstractServiceTest
20
{
21 View Code Duplication
    public function testCreateGlobalUrlAliasEvents()
22
    {
23
        $traceableEventDispatcher = $this->getEventDispatcher(
24
            URLAliasEvents::BEFORE_CREATE_GLOBAL_URL_ALIAS,
25
            URLAliasEvents::CREATE_GLOBAL_URL_ALIAS
26
        );
27
28
        $parameters = [
29
            'random_value_5cff79c3183471.48198669',
30
            'random_value_5cff79c3183491.90712521',
31
            'random_value_5cff79c31834a2.27245619',
32
            'random_value_5cff79c31834b7.17763784',
33
            'random_value_5cff79c31834c3.69513526',
34
        ];
35
36
        $urlAlias = $this->createMock(URLAlias::class);
37
        $innerServiceMock = $this->createMock(URLAliasServiceInterface::class);
38
        $innerServiceMock->method('createGlobalUrlAlias')->willReturn($urlAlias);
39
40
        $service = new URLAliasService($innerServiceMock, $traceableEventDispatcher);
41
        $result = $service->createGlobalUrlAlias(...$parameters);
0 ignored issues
show
Bug introduced by
The call to createGlobalUrlAlias() misses some required arguments starting with $path.
Loading history...
42
43
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
44
45
        $this->assertSame($urlAlias, $result);
46
        $this->assertSame($calledListeners, [
47
            [URLAliasEvents::BEFORE_CREATE_GLOBAL_URL_ALIAS, 0],
48
            [URLAliasEvents::CREATE_GLOBAL_URL_ALIAS, 0],
49
        ]);
50
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
51
    }
52
53 View Code Duplication
    public function testReturnCreateGlobalUrlAliasResultInBeforeEvents()
54
    {
55
        $traceableEventDispatcher = $this->getEventDispatcher(
56
            URLAliasEvents::BEFORE_CREATE_GLOBAL_URL_ALIAS,
57
            URLAliasEvents::CREATE_GLOBAL_URL_ALIAS
58
        );
59
60
        $parameters = [
61
            'random_value_5cff79c3183999.45723962',
62
            'random_value_5cff79c31839a0.16919746',
63
            'random_value_5cff79c31839b6.04657069',
64
            'random_value_5cff79c31839c8.99027893',
65
            'random_value_5cff79c31839d9.22502123',
66
        ];
67
68
        $urlAlias = $this->createMock(URLAlias::class);
69
        $eventUrlAlias = $this->createMock(URLAlias::class);
70
        $innerServiceMock = $this->createMock(URLAliasServiceInterface::class);
71
        $innerServiceMock->method('createGlobalUrlAlias')->willReturn($urlAlias);
72
73
        $traceableEventDispatcher->addListener(URLAliasEvents::BEFORE_CREATE_GLOBAL_URL_ALIAS, function (BeforeCreateGlobalUrlAliasEvent $event) use ($eventUrlAlias) {
74
            $event->setUrlAlias($eventUrlAlias);
75
        }, 10);
76
77
        $service = new URLAliasService($innerServiceMock, $traceableEventDispatcher);
78
        $result = $service->createGlobalUrlAlias(...$parameters);
0 ignored issues
show
Bug introduced by
The call to createGlobalUrlAlias() misses some required arguments starting with $path.
Loading history...
79
80
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
81
82
        $this->assertSame($eventUrlAlias, $result);
83
        $this->assertSame($calledListeners, [
84
            [URLAliasEvents::BEFORE_CREATE_GLOBAL_URL_ALIAS, 10],
85
            [URLAliasEvents::BEFORE_CREATE_GLOBAL_URL_ALIAS, 0],
86
            [URLAliasEvents::CREATE_GLOBAL_URL_ALIAS, 0],
87
        ]);
88
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
89
    }
90
91 View Code Duplication
    public function testCreateGlobalUrlAliasStopPropagationInBeforeEvents()
92
    {
93
        $traceableEventDispatcher = $this->getEventDispatcher(
94
            URLAliasEvents::BEFORE_CREATE_GLOBAL_URL_ALIAS,
95
            URLAliasEvents::CREATE_GLOBAL_URL_ALIAS
96
        );
97
98
        $parameters = [
99
            'random_value_5cff79c3183a40.78467503',
100
            'random_value_5cff79c3183a52.60688594',
101
            'random_value_5cff79c3183a62.37338343',
102
            'random_value_5cff79c3183a74.31062414',
103
            'random_value_5cff79c3183a85.16422549',
104
        ];
105
106
        $urlAlias = $this->createMock(URLAlias::class);
107
        $eventUrlAlias = $this->createMock(URLAlias::class);
108
        $innerServiceMock = $this->createMock(URLAliasServiceInterface::class);
109
        $innerServiceMock->method('createGlobalUrlAlias')->willReturn($urlAlias);
110
111
        $traceableEventDispatcher->addListener(URLAliasEvents::BEFORE_CREATE_GLOBAL_URL_ALIAS, function (BeforeCreateGlobalUrlAliasEvent $event) use ($eventUrlAlias) {
112
            $event->setUrlAlias($eventUrlAlias);
113
            $event->stopPropagation();
114
        }, 10);
115
116
        $service = new URLAliasService($innerServiceMock, $traceableEventDispatcher);
117
        $result = $service->createGlobalUrlAlias(...$parameters);
0 ignored issues
show
Bug introduced by
The call to createGlobalUrlAlias() misses some required arguments starting with $path.
Loading history...
118
119
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
120
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
121
122
        $this->assertSame($eventUrlAlias, $result);
123
        $this->assertSame($calledListeners, [
124
            [URLAliasEvents::BEFORE_CREATE_GLOBAL_URL_ALIAS, 10],
125
        ]);
126
        $this->assertSame($notCalledListeners, [
127
            [URLAliasEvents::CREATE_GLOBAL_URL_ALIAS, 0],
128
            [URLAliasEvents::BEFORE_CREATE_GLOBAL_URL_ALIAS, 0],
129
        ]);
130
    }
131
132
    public function testRefreshSystemUrlAliasesForLocationEvents()
133
    {
134
        $traceableEventDispatcher = $this->getEventDispatcher(
135
            URLAliasEvents::BEFORE_REFRESH_SYSTEM_URL_ALIASES_FOR_LOCATION,
136
            URLAliasEvents::REFRESH_SYSTEM_URL_ALIASES_FOR_LOCATION
137
        );
138
139
        $parameters = [
140
            $this->createMock(Location::class),
141
        ];
142
143
        $innerServiceMock = $this->createMock(URLAliasServiceInterface::class);
144
145
        $service = new URLAliasService($innerServiceMock, $traceableEventDispatcher);
146
        $service->refreshSystemUrlAliasesForLocation(...$parameters);
147
148
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
149
150
        $this->assertSame($calledListeners, [
151
            [URLAliasEvents::BEFORE_REFRESH_SYSTEM_URL_ALIASES_FOR_LOCATION, 0],
152
            [URLAliasEvents::REFRESH_SYSTEM_URL_ALIASES_FOR_LOCATION, 0],
153
        ]);
154
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
155
    }
156
157
    public function testRefreshSystemUrlAliasesForLocationStopPropagationInBeforeEvents()
158
    {
159
        $traceableEventDispatcher = $this->getEventDispatcher(
160
            URLAliasEvents::BEFORE_REFRESH_SYSTEM_URL_ALIASES_FOR_LOCATION,
161
            URLAliasEvents::REFRESH_SYSTEM_URL_ALIASES_FOR_LOCATION
162
        );
163
164
        $parameters = [
165
            $this->createMock(Location::class),
166
        ];
167
168
        $innerServiceMock = $this->createMock(URLAliasServiceInterface::class);
169
170
        $traceableEventDispatcher->addListener(URLAliasEvents::BEFORE_REFRESH_SYSTEM_URL_ALIASES_FOR_LOCATION, function (BeforeRefreshSystemUrlAliasesForLocationEvent $event) {
171
            $event->stopPropagation();
172
        }, 10);
173
174
        $service = new URLAliasService($innerServiceMock, $traceableEventDispatcher);
175
        $service->refreshSystemUrlAliasesForLocation(...$parameters);
176
177
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
178
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
179
180
        $this->assertSame($calledListeners, [
181
            [URLAliasEvents::BEFORE_REFRESH_SYSTEM_URL_ALIASES_FOR_LOCATION, 10],
182
        ]);
183
        $this->assertSame($notCalledListeners, [
184
            [URLAliasEvents::REFRESH_SYSTEM_URL_ALIASES_FOR_LOCATION, 0],
185
            [URLAliasEvents::BEFORE_REFRESH_SYSTEM_URL_ALIASES_FOR_LOCATION, 0],
186
        ]);
187
    }
188
189 View Code Duplication
    public function testCreateUrlAliasEvents()
190
    {
191
        $traceableEventDispatcher = $this->getEventDispatcher(
192
            URLAliasEvents::BEFORE_CREATE_URL_ALIAS,
193
            URLAliasEvents::CREATE_URL_ALIAS
194
        );
195
196
        $parameters = [
197
            $this->createMock(Location::class),
198
            'random_value_5cff79c3184f05.03459159',
199
            'random_value_5cff79c3184f14.18292216',
200
            'random_value_5cff79c3184f24.01158164',
201
            'random_value_5cff79c3184f32.03833593',
202
        ];
203
204
        $urlAlias = $this->createMock(URLAlias::class);
205
        $innerServiceMock = $this->createMock(URLAliasServiceInterface::class);
206
        $innerServiceMock->method('createUrlAlias')->willReturn($urlAlias);
207
208
        $service = new URLAliasService($innerServiceMock, $traceableEventDispatcher);
209
        $result = $service->createUrlAlias(...$parameters);
0 ignored issues
show
Bug introduced by
The call to createUrlAlias() misses some required arguments starting with $path.
Loading history...
210
211
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
212
213
        $this->assertSame($urlAlias, $result);
214
        $this->assertSame($calledListeners, [
215
            [URLAliasEvents::BEFORE_CREATE_URL_ALIAS, 0],
216
            [URLAliasEvents::CREATE_URL_ALIAS, 0],
217
        ]);
218
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
219
    }
220
221 View Code Duplication
    public function testReturnCreateUrlAliasResultInBeforeEvents()
222
    {
223
        $traceableEventDispatcher = $this->getEventDispatcher(
224
            URLAliasEvents::BEFORE_CREATE_URL_ALIAS,
225
            URLAliasEvents::CREATE_URL_ALIAS
226
        );
227
228
        $parameters = [
229
            $this->createMock(Location::class),
230
            'random_value_5cff79c3184fd7.07408772',
231
            'random_value_5cff79c3184fe2.98616568',
232
            'random_value_5cff79c3184ff0.62652505',
233
            'random_value_5cff79c3185003.87499400',
234
        ];
235
236
        $urlAlias = $this->createMock(URLAlias::class);
237
        $eventUrlAlias = $this->createMock(URLAlias::class);
238
        $innerServiceMock = $this->createMock(URLAliasServiceInterface::class);
239
        $innerServiceMock->method('createUrlAlias')->willReturn($urlAlias);
240
241
        $traceableEventDispatcher->addListener(URLAliasEvents::BEFORE_CREATE_URL_ALIAS, function (BeforeCreateUrlAliasEvent $event) use ($eventUrlAlias) {
242
            $event->setUrlAlias($eventUrlAlias);
243
        }, 10);
244
245
        $service = new URLAliasService($innerServiceMock, $traceableEventDispatcher);
246
        $result = $service->createUrlAlias(...$parameters);
0 ignored issues
show
Bug introduced by
The call to createUrlAlias() misses some required arguments starting with $path.
Loading history...
247
248
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
249
250
        $this->assertSame($eventUrlAlias, $result);
251
        $this->assertSame($calledListeners, [
252
            [URLAliasEvents::BEFORE_CREATE_URL_ALIAS, 10],
253
            [URLAliasEvents::BEFORE_CREATE_URL_ALIAS, 0],
254
            [URLAliasEvents::CREATE_URL_ALIAS, 0],
255
        ]);
256
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
257
    }
258
259 View Code Duplication
    public function testCreateUrlAliasStopPropagationInBeforeEvents()
260
    {
261
        $traceableEventDispatcher = $this->getEventDispatcher(
262
            URLAliasEvents::BEFORE_CREATE_URL_ALIAS,
263
            URLAliasEvents::CREATE_URL_ALIAS
264
        );
265
266
        $parameters = [
267
            $this->createMock(Location::class),
268
            'random_value_5cff79c3185072.24449261',
269
            'random_value_5cff79c3185080.62311461',
270
            'random_value_5cff79c3185095.31877612',
271
            'random_value_5cff79c31850a4.20254218',
272
        ];
273
274
        $urlAlias = $this->createMock(URLAlias::class);
275
        $eventUrlAlias = $this->createMock(URLAlias::class);
276
        $innerServiceMock = $this->createMock(URLAliasServiceInterface::class);
277
        $innerServiceMock->method('createUrlAlias')->willReturn($urlAlias);
278
279
        $traceableEventDispatcher->addListener(URLAliasEvents::BEFORE_CREATE_URL_ALIAS, function (BeforeCreateUrlAliasEvent $event) use ($eventUrlAlias) {
280
            $event->setUrlAlias($eventUrlAlias);
281
            $event->stopPropagation();
282
        }, 10);
283
284
        $service = new URLAliasService($innerServiceMock, $traceableEventDispatcher);
285
        $result = $service->createUrlAlias(...$parameters);
0 ignored issues
show
Bug introduced by
The call to createUrlAlias() misses some required arguments starting with $path.
Loading history...
286
287
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
288
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
289
290
        $this->assertSame($eventUrlAlias, $result);
291
        $this->assertSame($calledListeners, [
292
            [URLAliasEvents::BEFORE_CREATE_URL_ALIAS, 10],
293
        ]);
294
        $this->assertSame($notCalledListeners, [
295
            [URLAliasEvents::CREATE_URL_ALIAS, 0],
296
            [URLAliasEvents::BEFORE_CREATE_URL_ALIAS, 0],
297
        ]);
298
    }
299
300
    public function testRemoveAliasesEvents()
301
    {
302
        $traceableEventDispatcher = $this->getEventDispatcher(
303
            URLAliasEvents::BEFORE_REMOVE_ALIASES,
304
            URLAliasEvents::REMOVE_ALIASES
305
        );
306
307
        $parameters = [
308
            [],
309
        ];
310
311
        $innerServiceMock = $this->createMock(URLAliasServiceInterface::class);
312
313
        $service = new URLAliasService($innerServiceMock, $traceableEventDispatcher);
314
        $service->removeAliases(...$parameters);
315
316
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
317
318
        $this->assertSame($calledListeners, [
319
            [URLAliasEvents::BEFORE_REMOVE_ALIASES, 0],
320
            [URLAliasEvents::REMOVE_ALIASES, 0],
321
        ]);
322
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
323
    }
324
325
    public function testRemoveAliasesStopPropagationInBeforeEvents()
326
    {
327
        $traceableEventDispatcher = $this->getEventDispatcher(
328
            URLAliasEvents::BEFORE_REMOVE_ALIASES,
329
            URLAliasEvents::REMOVE_ALIASES
330
        );
331
332
        $parameters = [
333
            [],
334
        ];
335
336
        $innerServiceMock = $this->createMock(URLAliasServiceInterface::class);
337
338
        $traceableEventDispatcher->addListener(URLAliasEvents::BEFORE_REMOVE_ALIASES, function (BeforeRemoveAliasesEvent $event) {
339
            $event->stopPropagation();
340
        }, 10);
341
342
        $service = new URLAliasService($innerServiceMock, $traceableEventDispatcher);
343
        $service->removeAliases(...$parameters);
344
345
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
346
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
347
348
        $this->assertSame($calledListeners, [
349
            [URLAliasEvents::BEFORE_REMOVE_ALIASES, 10],
350
        ]);
351
        $this->assertSame($notCalledListeners, [
352
            [URLAliasEvents::REMOVE_ALIASES, 0],
353
            [URLAliasEvents::BEFORE_REMOVE_ALIASES, 0],
354
        ]);
355
    }
356
}
357