Completed
Push — master ( cfe8d7...fcc746 )
by André
19:36 queued 06:48
created

HandlerTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 191
Duplicated Lines 37.7 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 72
loc 191
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 7

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testUpdateUrl() 0 18 1
B testFind() 0 41 1
A testLoadByIdWithoutUrlData() 18 18 1
A testLoadByIdWithUrlData() 18 18 1
A testLoadByUrlWithoutUrlData() 18 18 1
A testLoadByUrlWithUrlData() 18 18 1
A testFindUsages() 0 13 1
A getUrl() 0 8 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\Persistence\Legacy\Tests\URL;
8
9
use eZ\Publish\API\Repository\Values\URL\Query\Criterion;
10
use eZ\Publish\API\Repository\Values\URL\Query\SortClause;
11
use eZ\Publish\API\Repository\Values\URL\URLQuery;
12
use eZ\Publish\Core\Persistence\Legacy\URL\Gateway;
13
use eZ\Publish\Core\Persistence\Legacy\URL\Handler;
14
use eZ\Publish\Core\Persistence\Legacy\URL\Mapper;
15
use eZ\Publish\SPI\Persistence\URL\URL;
16
use eZ\Publish\SPI\Persistence\URL\URLUpdateStruct;
17
use PHPUnit\Framework\TestCase;
18
19
class HandlerTest extends TestCase
20
{
21
    /**
22
     * @var \eZ\Publish\Core\Persistence\Legacy\URL\Gateway|\PHPUnit_Framework_MockObject_MockObject
23
     */
24
    private $gateway;
25
26
    /**
27
     * @var \eZ\Publish\Core\Persistence\Legacy\URL\Mapper|\PHPUnit_Framework_MockObject_MockObject
28
     */
29
    private $mapper;
30
31
    /**
32
     * @var \eZ\Publish\Core\Persistence\Legacy\URL\Handler
33
     */
34
    private $handler;
35
36
    protected function setUp()
37
    {
38
        parent::setUp();
39
        $this->gateway = $this->createMock(Gateway::class);
40
        $this->mapper = $this->createMock(Mapper::class);
41
        $this->handler = new Handler($this->gateway, $this->mapper);
42
    }
43
44
    public function testUpdateUrl()
45
    {
46
        $urlUpdateStruct = new URLUpdateStruct();
47
        $url = $this->getUrl(1, 'http://ez.no');
48
49
        $this->mapper
50
            ->expects($this->once())
51
            ->method('createURLFromUpdateStruct')
52
            ->with($urlUpdateStruct)
53
            ->willReturn($url);
54
55
        $this->gateway
56
            ->expects($this->once())
57
            ->method('updateUrl')
58
            ->with($url);
59
60
        $this->assertEquals($url, $this->handler->updateUrl($url->id, $urlUpdateStruct));
61
    }
62
63
    public function testFind()
64
    {
65
        $query = new URLQuery();
66
        $query->filter = new Criterion\Validity(true);
67
        $query->sortClauses = [
68
            new SortClause\Id(),
69
        ];
70
        $query->offset = 2;
71
        $query->limit = 10;
72
73
        $results = [
74
            'count' => 1,
75
            'rows' => [
76
                [
77
                    'id' => 1,
78
                    'url' => 'http://ez.no',
79
                ],
80
            ],
81
        ];
82
83
        $expected = [
84
            'count' => 1,
85
            'items' => [
86
                $this->getUrl(1, 'http://ez.no'),
87
            ],
88
        ];
89
90
        $this->gateway
91
            ->expects($this->once())
92
            ->method('find')
93
            ->with($query->filter, $query->offset, $query->limit, $query->sortClauses)
94
            ->willReturn($results);
95
96
        $this->mapper
97
            ->expects($this->once())
98
            ->method('extractURLsFromRows')
99
            ->with($results['rows'])
100
            ->willReturn($expected['items']);
101
102
        $this->assertEquals($expected, $this->handler->find($query));
103
    }
104
105
    /**
106
     * @expectedException \eZ\Publish\Core\Base\Exceptions\NotFoundException
107
     */
108 View Code Duplication
    public function testLoadByIdWithoutUrlData()
109
    {
110
        $id = 1;
111
112
        $this->gateway
113
            ->expects($this->once())
114
            ->method('loadUrlData')
115
            ->with($id)
116
            ->willReturn([]);
117
118
        $this->mapper
119
            ->expects($this->once())
120
            ->method('extractURLsFromRows')
121
            ->with([])
122
            ->willReturn([]);
123
124
        $this->handler->loadById($id);
125
    }
126
127 View Code Duplication
    public function testLoadByIdWithUrlData()
128
    {
129
        $url = $this->getUrl(1, 'http://ez.no');
130
131
        $this->gateway
132
            ->expects($this->once())
133
            ->method('loadUrlData')
134
            ->with($url->id)
135
            ->willReturn([$url]);
136
137
        $this->mapper
138
            ->expects($this->once())
139
            ->method('extractURLsFromRows')
140
            ->with([$url])
141
            ->willReturn([$url]);
142
143
        $this->assertEquals($url, $this->handler->loadById($url->id));
144
    }
145
146
    /**
147
     * @expectedException \eZ\Publish\Core\Base\Exceptions\NotFoundException
148
     */
149 View Code Duplication
    public function testLoadByUrlWithoutUrlData()
150
    {
151
        $url = 'http://ez.no';
152
153
        $this->gateway
154
            ->expects($this->once())
155
            ->method('loadUrlDataByUrl')
156
            ->with($url)
157
            ->willReturn([]);
158
159
        $this->mapper
160
            ->expects($this->once())
161
            ->method('extractURLsFromRows')
162
            ->with([])
163
            ->willReturn([]);
164
165
        $this->handler->loadByUrl($url);
166
    }
167
168 View Code Duplication
    public function testLoadByUrlWithUrlData()
169
    {
170
        $url = $this->getUrl(1, 'http://ez.no');
171
172
        $this->gateway
173
            ->expects($this->once())
174
            ->method('loadUrlDataByUrl')
175
            ->with($url->url)
176
            ->willReturn([$url]);
177
178
        $this->mapper
179
            ->expects($this->once())
180
            ->method('extractURLsFromRows')
181
            ->with([$url])
182
            ->willReturn([$url]);
183
184
        $this->assertEquals($url, $this->handler->loadByUrl($url->url));
185
    }
186
187
    public function testFindUsages()
188
    {
189
        $url = $this->getUrl();
190
        $ids = [1, 2, 3];
191
192
        $this->gateway
193
            ->expects($this->once())
194
            ->method('findUsages')
195
            ->with($url->id)
196
            ->will($this->returnValue($ids));
197
198
        $this->assertEquals($ids, $this->handler->findUsages($url->id));
199
    }
200
201
    private function getUrl($id = 1, $urlAddr = 'http://ez.no')
0 ignored issues
show
Unused Code introduced by
The parameter $urlAddr is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
202
    {
203
        $url = new URL();
204
        $url->id = $id;
205
        $url->url = $url;
0 ignored issues
show
Documentation Bug introduced by
It seems like $url of type object<eZ\Publish\SPI\Persistence\URL\URL> is incompatible with the declared type string of property $url.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
206
207
        return $url;
208
    }
209
}
210