Completed
Push — master ( 260ec9...add83c )
by
unknown
18:59
created

TrashHandlerTest::testDeleteTrashItem()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 55
rs 8.9818
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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