Completed
Push — ezp-29724 ( 495423...21f3e8 )
by
unknown
46:15 queued 21:42
created

TrashHandlerTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 223
Duplicated Lines 39.91 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 89
loc 223
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 7

8 Methods

Rating   Name   Duplication   Size   Complexity  
A providerForCachedLoadMethods() 0 6 1
A testRecover() 0 52 1
A testTrashSubtree() 0 51 1
A getHandlerMethodName() 0 4 1
A getHandlerClassName() 0 4 1
A providerForUnCachedMethods() 0 7 1
A testDeleteTrashItem() 45 45 1
A testEmptyTrash() 44 44 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
 * File contains Test class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Cache\Tests;
10
11
use eZ\Publish\Core\Persistence\Cache\ContentHandler;
12
use eZ\Publish\Core\Persistence\Cache\LocationHandler;
13
use eZ\Publish\SPI\Persistence\Content\Location;
14
use eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler as TrashHandler;
15
use eZ\Publish\SPI\Persistence\Content\Location\Trashed;
16
use eZ\Publish\SPI\Persistence\Content\Relation;
17
18
/**
19
 * Test case for Persistence\Cache\SectionHandler.
20
 */
21
class TrashHandlerTest extends AbstractCacheHandlerTest
22
{
23
    public function getHandlerMethodName(): string
24
    {
25
        return 'trashHandler';
26
    }
27
28
    public function getHandlerClassName(): string
29
    {
30
        return TrashHandler::class;
31
    }
32
33
    public function providerForUnCachedMethods(): array
34
    {
35
        // string $method, array $arguments, array? $tags, string? $key
36
        return [
37
            ['loadTrashItem', [6]],
38
        ];
39
    }
40
41
    public function providerForCachedLoadMethods(): array
42
    {
43
        // string $method, array $arguments, string $key, mixed? $data
44
        return [
45
        ];
46
    }
47
48
    public function testRecover()
49
    {
50
        $originalLocationId = 6;
51
        $targetLocationId = 2;
52
        $contentId = 42;
53
54
        $tags = [
55
            'content-' . $contentId,
56
            'content-fields-' . $contentId,
57
            'location-' . $originalLocationId,
58
            'location-path-' . $originalLocationId,
59
        ];
60
61
        $handlerMethodName = $this->getHandlerMethodName();
62
63
        $this->loggerMock->expects($this->once())->method('logCall');
64
65
        $innerHandler = $this->createMock($this->getHandlerClassName());
66
        $contentHandlerMock = $this->createMock(ContentHandler::class);
67
        $locationHandlerMock = $this->createMock(LocationHandler::class);
68
69
        $locationHandlerMock
70
            ->method('load')
71
            ->will($this->returnValue(new Location(['id' => $originalLocationId, 'contentId' => $contentId])));
72
73
        $this->persistenceHandlerMock
74
            ->method('contentHandler')
75
            ->will($this->returnValue($contentHandlerMock));
76
77
        $this->persistenceHandlerMock
78
            ->method('locationHandler')
79
            ->will($this->returnValue($locationHandlerMock));
80
81
        $this->persistenceHandlerMock
82
            ->expects($this->once())
83
            ->method($handlerMethodName)
84
            ->will($this->returnValue($innerHandler));
85
86
        $innerHandler
87
            ->expects($this->once())
88
            ->method('recover')
89
            ->with($originalLocationId, $targetLocationId)
90
            ->will($this->returnValue(null));
91
92
        $this->cacheMock
93
            ->expects($this->once())
94
            ->method('invalidateTags')
95
            ->with($tags);
96
97
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
98
        $handler->recover($originalLocationId, $targetLocationId);
99
    }
100
101
    public function testTrashSubtree()
102
    {
103
        $locationId = 6;
104
        $contentId = 42;
105
106
        $tags = [
107
            'content-' . $contentId,
108
            'content-fields-' . $contentId,
109
            'location-' . $locationId,
110
            'location-path-' . $locationId,
111
        ];
112
113
        $handlerMethodName = $this->getHandlerMethodName();
114
115
        $this->loggerMock->expects($this->once())->method('logCall');
116
117
        $innerHandler = $this->createMock($this->getHandlerClassName());
118
        $contentHandlerMock = $this->createMock(ContentHandler::class);
119
        $locationHandlerMock = $this->createMock(LocationHandler::class);
120
121
        $locationHandlerMock
122
            ->method('load')
123
            ->will($this->returnValue(new Location(['id' => $locationId, 'contentId' => $contentId])));
124
125
        $this->persistenceHandlerMock
126
            ->method('contentHandler')
127
            ->will($this->returnValue($contentHandlerMock));
128
129
        $this->persistenceHandlerMock
130
            ->method('locationHandler')
131
            ->will($this->returnValue($locationHandlerMock));
132
133
        $this->persistenceHandlerMock
134
            ->expects($this->once())
135
            ->method($handlerMethodName)
136
            ->will($this->returnValue($innerHandler));
137
138
        $innerHandler
139
            ->expects($this->once())
140
            ->method('trashSubtree')
141
            ->with($locationId)
142
            ->will($this->returnValue(null));
143
144
        $this->cacheMock
145
            ->expects($this->once())
146
            ->method('invalidateTags')
147
            ->with($tags);
148
149
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
150
        $handler->trashSubtree($locationId);
151
    }
152
153 View Code Duplication
    public function testDeleteTrashItem()
154
    {
155
        $trashedId = 6;
156
        $contentId = 42;
157
        $relationSourceContentId = 42;
158
159
        $handlerMethodName = $this->getHandlerMethodName();
160
161
        $innerHandler = $this->createMock($this->getHandlerClassName());
162
163
        $innerHandler
164
            ->expects($this->once())
165
            ->method('loadTrashItem')
166
            ->with($trashedId)
167
            ->will($this->returnValue(new Trashed(['id' => $trashedId, 'contentId' => $contentId])));
168
169
        $this->persistenceHandlerMock
170
            ->method($handlerMethodName)
171
            ->will($this->returnValue($innerHandler));
172
173
        $contentHandlerMock = $this->createMock(ContentHandler::class);
174
175
        $contentHandlerMock
176
            ->expects($this->once())
177
            ->method('loadReverseRelations')
178
            ->with($contentId)
179
            ->will($this->returnValue([new Relation(['sourceContentId' => $relationSourceContentId])]));
180
181
        $this->persistenceHandlerMock
182
            ->method('contentHandler')
183
            ->will($this->returnValue($contentHandlerMock));
184
185
        $tags = [
186
            'content-fields-' . $relationSourceContentId,
187
        ];
188
189
        $this->cacheMock
190
            ->expects($this->once())
191
            ->method('invalidateTags')
192
            ->with($tags);
193
194
        /** @var \eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler $handler */
195
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
196
        $handler->deleteTrashItem($trashedId);
197
    }
198
199 View Code Duplication
    public function testEmptyTrash()
200
    {
201
        $trashedId = 6;
202
        $contentId = 42;
203
        $relationSourceContentId = 42;
204
205
        $handlerMethodName = $this->getHandlerMethodName();
206
207
        $innerHandler = $this->createMock($this->getHandlerClassName());
208
209
        $innerHandler
210
            ->expects($this->once())
211
            ->method('findTrashItems')
212
            ->will($this->returnValue([new Trashed(['id' => $trashedId, 'contentId' => $contentId])]));
213
214
        $this->persistenceHandlerMock
215
            ->method($handlerMethodName)
216
            ->will($this->returnValue($innerHandler));
217
218
        $contentHandlerMock = $this->createMock(ContentHandler::class);
219
220
        $contentHandlerMock
221
            ->expects($this->once())
222
            ->method('loadReverseRelations')
223
            ->with($contentId)
224
            ->will($this->returnValue([new Relation(['sourceContentId' => $relationSourceContentId])]));
225
226
        $this->persistenceHandlerMock
227
            ->method('contentHandler')
228
            ->will($this->returnValue($contentHandlerMock));
229
230
        $tags = [
231
            'content-fields-' . $relationSourceContentId,
232
        ];
233
234
        $this->cacheMock
235
            ->expects($this->once())
236
            ->method('invalidateTags')
237
            ->with($tags);
238
239
        /** @var \eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler $handler */
240
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
241
        $handler->emptyTrash();
242
    }
243
}
244