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

URLServiceTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 104
Duplicated Lines 95.19 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 99
loc 104
c 0
b 0
f 0
rs 10
wmc 3
lcom 1
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testUpdateUrlEvents() 28 28 1
A testReturnUpdateUrlResultInBeforeEvents() 34 34 1
A testUpdateUrlStopPropagationInBeforeEvents() 37 37 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\Events\URL\BeforeUpdateUrlEvent as BeforeUpdateUrlEventInterface;
10
use eZ\Publish\API\Repository\Events\URL\UpdateUrlEvent as UpdateUrlEventInterface;
11
use eZ\Publish\API\Repository\URLService as URLServiceInterface;
12
use eZ\Publish\API\Repository\Values\URL\URL;
13
use eZ\Publish\API\Repository\Values\URL\URLUpdateStruct;
14
use eZ\Publish\Core\Event\URLService;
15
16
class URLServiceTest extends AbstractServiceTest
17
{
18 View Code Duplication
    public function testUpdateUrlEvents()
19
    {
20
        $traceableEventDispatcher = $this->getEventDispatcher(
21
            BeforeUpdateUrlEventInterface::class,
22
            UpdateUrlEventInterface::class
23
        );
24
25
        $parameters = [
26
            $this->createMock(URL::class),
27
            $this->createMock(URLUpdateStruct::class),
28
        ];
29
30
        $updatedUrl = $this->createMock(URL::class);
31
        $innerServiceMock = $this->createMock(URLServiceInterface::class);
32
        $innerServiceMock->method('updateUrl')->willReturn($updatedUrl);
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...
33
34
        $service = new URLService($innerServiceMock, $traceableEventDispatcher);
35
        $result = $service->updateUrl(...$parameters);
0 ignored issues
show
Bug introduced by
The call to updateUrl() misses a required argument $struct.

This check looks for function calls that miss required arguments.

Loading history...
36
37
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
38
39
        $this->assertSame($updatedUrl, $result);
40
        $this->assertSame($calledListeners, [
41
            [BeforeUpdateUrlEventInterface::class, 0],
42
            [UpdateUrlEventInterface::class, 0],
43
        ]);
44
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
45
    }
46
47 View Code Duplication
    public function testReturnUpdateUrlResultInBeforeEvents()
48
    {
49
        $traceableEventDispatcher = $this->getEventDispatcher(
50
            BeforeUpdateUrlEventInterface::class,
51
            UpdateUrlEventInterface::class
52
        );
53
54
        $parameters = [
55
            $this->createMock(URL::class),
56
            $this->createMock(URLUpdateStruct::class),
57
        ];
58
59
        $updatedUrl = $this->createMock(URL::class);
60
        $eventUpdatedUrl = $this->createMock(URL::class);
61
        $innerServiceMock = $this->createMock(URLServiceInterface::class);
62
        $innerServiceMock->method('updateUrl')->willReturn($updatedUrl);
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...
63
64
        $traceableEventDispatcher->addListener(BeforeUpdateUrlEventInterface::class, function (BeforeUpdateUrlEventInterface $event) use ($eventUpdatedUrl) {
65
            $event->setUpdatedUrl($eventUpdatedUrl);
66
        }, 10);
67
68
        $service = new URLService($innerServiceMock, $traceableEventDispatcher);
69
        $result = $service->updateUrl(...$parameters);
0 ignored issues
show
Bug introduced by
The call to updateUrl() misses a required argument $struct.

This check looks for function calls that miss required arguments.

Loading history...
70
71
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
72
73
        $this->assertSame($eventUpdatedUrl, $result);
74
        $this->assertSame($calledListeners, [
75
            [BeforeUpdateUrlEventInterface::class, 10],
76
            [BeforeUpdateUrlEventInterface::class, 0],
77
            [UpdateUrlEventInterface::class, 0],
78
        ]);
79
        $this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
80
    }
81
82 View Code Duplication
    public function testUpdateUrlStopPropagationInBeforeEvents()
83
    {
84
        $traceableEventDispatcher = $this->getEventDispatcher(
85
            BeforeUpdateUrlEventInterface::class,
86
            UpdateUrlEventInterface::class
87
        );
88
89
        $parameters = [
90
            $this->createMock(URL::class),
91
            $this->createMock(URLUpdateStruct::class),
92
        ];
93
94
        $updatedUrl = $this->createMock(URL::class);
95
        $eventUpdatedUrl = $this->createMock(URL::class);
96
        $innerServiceMock = $this->createMock(URLServiceInterface::class);
97
        $innerServiceMock->method('updateUrl')->willReturn($updatedUrl);
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...
98
99
        $traceableEventDispatcher->addListener(BeforeUpdateUrlEventInterface::class, function (BeforeUpdateUrlEventInterface $event) use ($eventUpdatedUrl) {
100
            $event->setUpdatedUrl($eventUpdatedUrl);
101
            $event->stopPropagation();
102
        }, 10);
103
104
        $service = new URLService($innerServiceMock, $traceableEventDispatcher);
105
        $result = $service->updateUrl(...$parameters);
0 ignored issues
show
Bug introduced by
The call to updateUrl() misses a required argument $struct.

This check looks for function calls that miss required arguments.

Loading history...
106
107
        $calledListeners = $this->getListenersStack($traceableEventDispatcher->getCalledListeners());
108
        $notCalledListeners = $this->getListenersStack($traceableEventDispatcher->getNotCalledListeners());
109
110
        $this->assertSame($eventUpdatedUrl, $result);
111
        $this->assertSame($calledListeners, [
112
            [BeforeUpdateUrlEventInterface::class, 10],
113
        ]);
114
        $this->assertSame($notCalledListeners, [
115
            [BeforeUpdateUrlEventInterface::class, 0],
116
            [UpdateUrlEventInterface::class, 0],
117
        ]);
118
    }
119
}
120