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

SectionServiceTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 378
Duplicated Lines 43.12 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 163
loc 378
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 8

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testAssignSectionEvents() 0 25 1
A testAssignSectionStopPropagationInBeforeEvents() 32 32 1
A testUpdateSectionEvents() 28 28 1
A testReturnUpdateSectionResultInBeforeEvents() 34 34 1
A testUpdateSectionStopPropagationInBeforeEvents() 37 37 1
A testAssignSectionToSubtreeEvents() 0 25 1
A testAssignSectionToSubtreeStopPropagationInBeforeEvents() 32 32 1
A testDeleteSectionEvents() 0 24 1
A testDeleteSectionStopPropagationInBeforeEvents() 0 31 1
A testCreateSectionEvents() 0 27 1
A testReturnCreateSectionResultInBeforeEvents() 0 33 1
A testCreateSectionStopPropagationInBeforeEvents() 0 36 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\SectionService as SectionServiceInterface;
10
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
11
use eZ\Publish\API\Repository\Values\Content\Location;
12
use eZ\Publish\API\Repository\Values\Content\Section;
13
use eZ\Publish\API\Repository\Values\Content\SectionCreateStruct;
14
use eZ\Publish\API\Repository\Values\Content\SectionUpdateStruct;
15
use eZ\Publish\Core\Event\SectionService;
16
use eZ\Publish\Core\Event\Section\BeforeAssignSectionEvent;
17
use eZ\Publish\Core\Event\Section\BeforeAssignSectionToSubtreeEvent;
18
use eZ\Publish\Core\Event\Section\BeforeCreateSectionEvent;
19
use eZ\Publish\Core\Event\Section\BeforeDeleteSectionEvent;
20
use eZ\Publish\Core\Event\Section\BeforeUpdateSectionEvent;
21
use eZ\Publish\Core\Event\Section\SectionEvents;
22
23
class SectionServiceTest extends AbstractServiceTest
24
{
25
    public function testAssignSectionEvents()
26
    {
27
        $traceableEventDispatcher = $this->getEventDispatcher(
28
            SectionEvents::BEFORE_ASSIGN_SECTION,
29
            SectionEvents::ASSIGN_SECTION
30
        );
31
32
        $parameters = [
33
            $this->createMock(ContentInfo::class),
34
            $this->createMock(Section::class),
35
        ];
36
37
        $innerServiceMock = $this->createMock(SectionServiceInterface::class);
38
39
        $service = new SectionService($innerServiceMock, $traceableEventDispatcher);
40
        $service->assignSection(...$parameters);
0 ignored issues
show
Bug introduced by
The call to assignSection() misses a required argument $section.

This check looks for function calls that miss required arguments.

Loading history...
41
42
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
43
44
        $this->assertSame($calledListeners, [
45
            [SectionEvents::BEFORE_ASSIGN_SECTION, 0],
46
            [SectionEvents::ASSIGN_SECTION, 0],
47
        ]);
48
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
49
    }
50
51 View Code Duplication
    public function testAssignSectionStopPropagationInBeforeEvents()
52
    {
53
        $traceableEventDispatcher = $this->getEventDispatcher(
54
            SectionEvents::BEFORE_ASSIGN_SECTION,
55
            SectionEvents::ASSIGN_SECTION
56
        );
57
58
        $parameters = [
59
            $this->createMock(ContentInfo::class),
60
            $this->createMock(Section::class),
61
        ];
62
63
        $innerServiceMock = $this->createMock(SectionServiceInterface::class);
64
65
        $traceableEventDispatcher->addListener(SectionEvents::BEFORE_ASSIGN_SECTION, function (BeforeAssignSectionEvent $event) {
66
            $event->stopPropagation();
67
        }, 10);
68
69
        $service = new SectionService($innerServiceMock, $traceableEventDispatcher);
70
        $service->assignSection(...$parameters);
0 ignored issues
show
Bug introduced by
The call to assignSection() misses a required argument $section.

This check looks for function calls that miss required arguments.

Loading history...
71
72
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
73
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
74
75
        $this->assertSame($calledListeners, [
76
            [SectionEvents::BEFORE_ASSIGN_SECTION, 10],
77
        ]);
78
        $this->assertSame($notCalledListeners, [
79
            [SectionEvents::ASSIGN_SECTION, 0],
80
            [SectionEvents::BEFORE_ASSIGN_SECTION, 0],
81
        ]);
82
    }
83
84 View Code Duplication
    public function testUpdateSectionEvents()
85
    {
86
        $traceableEventDispatcher = $this->getEventDispatcher(
87
            SectionEvents::BEFORE_UPDATE_SECTION,
88
            SectionEvents::UPDATE_SECTION
89
        );
90
91
        $parameters = [
92
            $this->createMock(Section::class),
93
            $this->createMock(SectionUpdateStruct::class),
94
        ];
95
96
        $updatedSection = $this->createMock(Section::class);
97
        $innerServiceMock = $this->createMock(SectionServiceInterface::class);
98
        $innerServiceMock->method('updateSection')->willReturn($updatedSection);
99
100
        $service = new SectionService($innerServiceMock, $traceableEventDispatcher);
101
        $result = $service->updateSection(...$parameters);
0 ignored issues
show
Bug introduced by
The call to updateSection() misses a required argument $sectionUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
102
103
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
104
105
        $this->assertSame($updatedSection, $result);
106
        $this->assertSame($calledListeners, [
107
            [SectionEvents::BEFORE_UPDATE_SECTION, 0],
108
            [SectionEvents::UPDATE_SECTION, 0],
109
        ]);
110
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
111
    }
112
113 View Code Duplication
    public function testReturnUpdateSectionResultInBeforeEvents()
114
    {
115
        $traceableEventDispatcher = $this->getEventDispatcher(
116
            SectionEvents::BEFORE_UPDATE_SECTION,
117
            SectionEvents::UPDATE_SECTION
118
        );
119
120
        $parameters = [
121
            $this->createMock(Section::class),
122
            $this->createMock(SectionUpdateStruct::class),
123
        ];
124
125
        $updatedSection = $this->createMock(Section::class);
126
        $eventUpdatedSection = $this->createMock(Section::class);
127
        $innerServiceMock = $this->createMock(SectionServiceInterface::class);
128
        $innerServiceMock->method('updateSection')->willReturn($updatedSection);
129
130
        $traceableEventDispatcher->addListener(SectionEvents::BEFORE_UPDATE_SECTION, function (BeforeUpdateSectionEvent $event) use ($eventUpdatedSection) {
131
            $event->setUpdatedSection($eventUpdatedSection);
132
        }, 10);
133
134
        $service = new SectionService($innerServiceMock, $traceableEventDispatcher);
135
        $result = $service->updateSection(...$parameters);
0 ignored issues
show
Bug introduced by
The call to updateSection() misses a required argument $sectionUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
136
137
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
138
139
        $this->assertSame($eventUpdatedSection, $result);
140
        $this->assertSame($calledListeners, [
141
            [SectionEvents::BEFORE_UPDATE_SECTION, 10],
142
            [SectionEvents::BEFORE_UPDATE_SECTION, 0],
143
            [SectionEvents::UPDATE_SECTION, 0],
144
        ]);
145
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
146
    }
147
148 View Code Duplication
    public function testUpdateSectionStopPropagationInBeforeEvents()
149
    {
150
        $traceableEventDispatcher = $this->getEventDispatcher(
151
            SectionEvents::BEFORE_UPDATE_SECTION,
152
            SectionEvents::UPDATE_SECTION
153
        );
154
155
        $parameters = [
156
            $this->createMock(Section::class),
157
            $this->createMock(SectionUpdateStruct::class),
158
        ];
159
160
        $updatedSection = $this->createMock(Section::class);
161
        $eventUpdatedSection = $this->createMock(Section::class);
162
        $innerServiceMock = $this->createMock(SectionServiceInterface::class);
163
        $innerServiceMock->method('updateSection')->willReturn($updatedSection);
164
165
        $traceableEventDispatcher->addListener(SectionEvents::BEFORE_UPDATE_SECTION, function (BeforeUpdateSectionEvent $event) use ($eventUpdatedSection) {
166
            $event->setUpdatedSection($eventUpdatedSection);
167
            $event->stopPropagation();
168
        }, 10);
169
170
        $service = new SectionService($innerServiceMock, $traceableEventDispatcher);
171
        $result = $service->updateSection(...$parameters);
0 ignored issues
show
Bug introduced by
The call to updateSection() misses a required argument $sectionUpdateStruct.

This check looks for function calls that miss required arguments.

Loading history...
172
173
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
174
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
175
176
        $this->assertSame($eventUpdatedSection, $result);
177
        $this->assertSame($calledListeners, [
178
            [SectionEvents::BEFORE_UPDATE_SECTION, 10],
179
        ]);
180
        $this->assertSame($notCalledListeners, [
181
            [SectionEvents::UPDATE_SECTION, 0],
182
            [SectionEvents::BEFORE_UPDATE_SECTION, 0],
183
        ]);
184
    }
185
186
    public function testAssignSectionToSubtreeEvents()
187
    {
188
        $traceableEventDispatcher = $this->getEventDispatcher(
189
            SectionEvents::BEFORE_ASSIGN_SECTION_TO_SUBTREE,
190
            SectionEvents::ASSIGN_SECTION_TO_SUBTREE
191
        );
192
193
        $parameters = [
194
            $this->createMock(Location::class),
195
            $this->createMock(Section::class),
196
        ];
197
198
        $innerServiceMock = $this->createMock(SectionServiceInterface::class);
199
200
        $service = new SectionService($innerServiceMock, $traceableEventDispatcher);
201
        $service->assignSectionToSubtree(...$parameters);
0 ignored issues
show
Bug introduced by
The call to assignSectionToSubtree() misses a required argument $section.

This check looks for function calls that miss required arguments.

Loading history...
202
203
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
204
205
        $this->assertSame($calledListeners, [
206
            [SectionEvents::BEFORE_ASSIGN_SECTION_TO_SUBTREE, 0],
207
            [SectionEvents::ASSIGN_SECTION_TO_SUBTREE, 0],
208
        ]);
209
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
210
    }
211
212 View Code Duplication
    public function testAssignSectionToSubtreeStopPropagationInBeforeEvents()
213
    {
214
        $traceableEventDispatcher = $this->getEventDispatcher(
215
            SectionEvents::BEFORE_ASSIGN_SECTION_TO_SUBTREE,
216
            SectionEvents::ASSIGN_SECTION_TO_SUBTREE
217
        );
218
219
        $parameters = [
220
            $this->createMock(Location::class),
221
            $this->createMock(Section::class),
222
        ];
223
224
        $innerServiceMock = $this->createMock(SectionServiceInterface::class);
225
226
        $traceableEventDispatcher->addListener(SectionEvents::BEFORE_ASSIGN_SECTION_TO_SUBTREE, function (BeforeAssignSectionToSubtreeEvent $event) {
227
            $event->stopPropagation();
228
        }, 10);
229
230
        $service = new SectionService($innerServiceMock, $traceableEventDispatcher);
231
        $service->assignSectionToSubtree(...$parameters);
0 ignored issues
show
Bug introduced by
The call to assignSectionToSubtree() misses a required argument $section.

This check looks for function calls that miss required arguments.

Loading history...
232
233
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
234
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
235
236
        $this->assertSame($calledListeners, [
237
            [SectionEvents::BEFORE_ASSIGN_SECTION_TO_SUBTREE, 10],
238
        ]);
239
        $this->assertSame($notCalledListeners, [
240
            [SectionEvents::ASSIGN_SECTION_TO_SUBTREE, 0],
241
            [SectionEvents::BEFORE_ASSIGN_SECTION_TO_SUBTREE, 0],
242
        ]);
243
    }
244
245
    public function testDeleteSectionEvents()
246
    {
247
        $traceableEventDispatcher = $this->getEventDispatcher(
248
            SectionEvents::BEFORE_DELETE_SECTION,
249
            SectionEvents::DELETE_SECTION
250
        );
251
252
        $parameters = [
253
            $this->createMock(Section::class),
254
        ];
255
256
        $innerServiceMock = $this->createMock(SectionServiceInterface::class);
257
258
        $service = new SectionService($innerServiceMock, $traceableEventDispatcher);
259
        $service->deleteSection(...$parameters);
260
261
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
262
263
        $this->assertSame($calledListeners, [
264
            [SectionEvents::BEFORE_DELETE_SECTION, 0],
265
            [SectionEvents::DELETE_SECTION, 0],
266
        ]);
267
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
268
    }
269
270
    public function testDeleteSectionStopPropagationInBeforeEvents()
271
    {
272
        $traceableEventDispatcher = $this->getEventDispatcher(
273
            SectionEvents::BEFORE_DELETE_SECTION,
274
            SectionEvents::DELETE_SECTION
275
        );
276
277
        $parameters = [
278
            $this->createMock(Section::class),
279
        ];
280
281
        $innerServiceMock = $this->createMock(SectionServiceInterface::class);
282
283
        $traceableEventDispatcher->addListener(SectionEvents::BEFORE_DELETE_SECTION, function (BeforeDeleteSectionEvent $event) {
284
            $event->stopPropagation();
285
        }, 10);
286
287
        $service = new SectionService($innerServiceMock, $traceableEventDispatcher);
288
        $service->deleteSection(...$parameters);
289
290
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
291
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
292
293
        $this->assertSame($calledListeners, [
294
            [SectionEvents::BEFORE_DELETE_SECTION, 10],
295
        ]);
296
        $this->assertSame($notCalledListeners, [
297
            [SectionEvents::DELETE_SECTION, 0],
298
            [SectionEvents::BEFORE_DELETE_SECTION, 0],
299
        ]);
300
    }
301
302
    public function testCreateSectionEvents()
303
    {
304
        $traceableEventDispatcher = $this->getEventDispatcher(
305
            SectionEvents::BEFORE_CREATE_SECTION,
306
            SectionEvents::CREATE_SECTION
307
        );
308
309
        $parameters = [
310
            $this->createMock(SectionCreateStruct::class),
311
        ];
312
313
        $section = $this->createMock(Section::class);
314
        $innerServiceMock = $this->createMock(SectionServiceInterface::class);
315
        $innerServiceMock->method('createSection')->willReturn($section);
316
317
        $service = new SectionService($innerServiceMock, $traceableEventDispatcher);
318
        $result = $service->createSection(...$parameters);
319
320
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
321
322
        $this->assertSame($section, $result);
323
        $this->assertSame($calledListeners, [
324
            [SectionEvents::BEFORE_CREATE_SECTION, 0],
325
            [SectionEvents::CREATE_SECTION, 0],
326
        ]);
327
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
328
    }
329
330
    public function testReturnCreateSectionResultInBeforeEvents()
331
    {
332
        $traceableEventDispatcher = $this->getEventDispatcher(
333
            SectionEvents::BEFORE_CREATE_SECTION,
334
            SectionEvents::CREATE_SECTION
335
        );
336
337
        $parameters = [
338
            $this->createMock(SectionCreateStruct::class),
339
        ];
340
341
        $section = $this->createMock(Section::class);
342
        $eventSection = $this->createMock(Section::class);
343
        $innerServiceMock = $this->createMock(SectionServiceInterface::class);
344
        $innerServiceMock->method('createSection')->willReturn($section);
345
346
        $traceableEventDispatcher->addListener(SectionEvents::BEFORE_CREATE_SECTION, function (BeforeCreateSectionEvent $event) use ($eventSection) {
347
            $event->setSection($eventSection);
348
        }, 10);
349
350
        $service = new SectionService($innerServiceMock, $traceableEventDispatcher);
351
        $result = $service->createSection(...$parameters);
352
353
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
354
355
        $this->assertSame($eventSection, $result);
356
        $this->assertSame($calledListeners, [
357
            [SectionEvents::BEFORE_CREATE_SECTION, 10],
358
            [SectionEvents::BEFORE_CREATE_SECTION, 0],
359
            [SectionEvents::CREATE_SECTION, 0],
360
        ]);
361
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
362
    }
363
364
    public function testCreateSectionStopPropagationInBeforeEvents()
365
    {
366
        $traceableEventDispatcher = $this->getEventDispatcher(
367
            SectionEvents::BEFORE_CREATE_SECTION,
368
            SectionEvents::CREATE_SECTION
369
        );
370
371
        $parameters = [
372
            $this->createMock(SectionCreateStruct::class),
373
        ];
374
375
        $section = $this->createMock(Section::class);
376
        $eventSection = $this->createMock(Section::class);
377
        $innerServiceMock = $this->createMock(SectionServiceInterface::class);
378
        $innerServiceMock->method('createSection')->willReturn($section);
379
380
        $traceableEventDispatcher->addListener(SectionEvents::BEFORE_CREATE_SECTION, function (BeforeCreateSectionEvent $event) use ($eventSection) {
381
            $event->setSection($eventSection);
382
            $event->stopPropagation();
383
        }, 10);
384
385
        $service = new SectionService($innerServiceMock, $traceableEventDispatcher);
386
        $result = $service->createSection(...$parameters);
387
388
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
389
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
390
391
        $this->assertSame($eventSection, $result);
392
        $this->assertSame($calledListeners, [
393
            [SectionEvents::BEFORE_CREATE_SECTION, 10],
394
        ]);
395
        $this->assertSame($notCalledListeners, [
396
            [SectionEvents::CREATE_SECTION, 0],
397
            [SectionEvents::BEFORE_CREATE_SECTION, 0],
398
        ]);
399
    }
400
}
401