Completed
Push — reduce_cache_tags ( 8b6ee4 )
by André
19:52
created

TrashHandlerTest::testDeleteTrashItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 54
rs 9.0036
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
            'location-path-' . $originalLocationId,
58
        ];
59
60
        $handlerMethodName = $this->getHandlerMethodName();
61
62
        $this->loggerMock->expects($this->once())->method('logCall');
63
64
        $innerHandler = $this->createMock($this->getHandlerClassName());
65
        $contentHandlerMock = $this->createMock(ContentHandler::class);
66
        $locationHandlerMock = $this->createMock(LocationHandler::class);
67
68
        $locationHandlerMock
69
            ->method('load')
70
            ->will($this->returnValue(new Location(['id' => $originalLocationId, 'contentId' => $contentId])));
71
72
        $this->persistenceHandlerMock
73
            ->method('contentHandler')
74
            ->will($this->returnValue($contentHandlerMock));
75
76
        $this->persistenceHandlerMock
77
            ->method('locationHandler')
78
            ->will($this->returnValue($locationHandlerMock));
79
80
        $this->persistenceHandlerMock
81
            ->expects($this->once())
82
            ->method($handlerMethodName)
83
            ->will($this->returnValue($innerHandler));
84
85
        $innerHandler
86
            ->expects($this->once())
87
            ->method('recover')
88
            ->with($originalLocationId, $targetLocationId)
89
            ->will($this->returnValue(null));
90
91
        $this->cacheMock
92
            ->expects($this->once())
93
            ->method('invalidateTags')
94
            ->with($tags);
95
96
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
97
        $handler->recover($originalLocationId, $targetLocationId);
98
    }
99
100
    public function testTrashSubtree()
101
    {
102
        $locationId = 6;
103
        $contentId = 42;
104
105
        $tags = [
106
            'content-' . $contentId,
107
            'location-path-' . $locationId,
108
        ];
109
110
        $handlerMethodName = $this->getHandlerMethodName();
111
112
        $this->loggerMock->expects($this->once())->method('logCall');
113
114
        $innerHandler = $this->createMock($this->getHandlerClassName());
115
        $contentHandlerMock = $this->createMock(ContentHandler::class);
116
        $locationHandlerMock = $this->createMock(LocationHandler::class);
117
118
        $locationHandlerMock
119
            ->method('load')
120
            ->will($this->returnValue(new Location(['id' => $locationId, 'contentId' => $contentId])));
121
122
        $this->persistenceHandlerMock
123
            ->method('contentHandler')
124
            ->will($this->returnValue($contentHandlerMock));
125
126
        $this->persistenceHandlerMock
127
            ->method('locationHandler')
128
            ->will($this->returnValue($locationHandlerMock));
129
130
        $this->persistenceHandlerMock
131
            ->expects($this->once())
132
            ->method($handlerMethodName)
133
            ->will($this->returnValue($innerHandler));
134
135
        $innerHandler
136
            ->expects($this->once())
137
            ->method('trashSubtree')
138
            ->with($locationId)
139
            ->will($this->returnValue(null));
140
141
        $this->cacheMock
142
            ->expects($this->once())
143
            ->method('invalidateTags')
144
            ->with($tags);
145
146
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
147
        $handler->trashSubtree($locationId);
148
    }
149
150
    public function testDeleteTrashItem()
151
    {
152
        $trashedId = 6;
153
        $contentId = 42;
154
        $relationSourceContentId = 44;
155
156
        $handlerMethodName = $this->getHandlerMethodName();
157
158
        $innerHandler = $this->createMock($this->getHandlerClassName());
159
160
        $trashed = new Trashed(['id' => $trashedId, 'contentId' => $contentId]);
161
        $innerHandler
162
            ->expects($this->once())
163
            ->method('deleteTrashItem')
164
            ->with($trashedId)
165
            ->will($this->returnValue(new TrashItemDeleteResult(['trashItemId' => $trashedId, 'contentId' => $contentId])));
166
167
        $innerHandler
168
            ->expects($this->once())
169
            ->method('loadTrashItem')
170
            ->with($trashedId)
171
            ->will($this->returnValue($trashed));
172
173
        $this->persistenceHandlerMock
174
            ->method($handlerMethodName)
175
            ->will($this->returnValue($innerHandler));
176
177
        $contentHandlerMock = $this->createMock(ContentHandler::class);
178
179
        $contentHandlerMock
180
            ->expects($this->once())
181
            ->method('loadReverseRelations')
182
            ->with($contentId)
183
            ->will($this->returnValue([new Relation(['sourceContentId' => $relationSourceContentId])]));
184
185
        $this->persistenceHandlerMock
186
            ->method('contentHandler')
187
            ->will($this->returnValue($contentHandlerMock));
188
189
        $tags = [
190
            'content-' . $contentId,
191
            'location-path-' . $trashedId,
192
            'content-' . $relationSourceContentId,
193
        ];
194
195
        $this->cacheMock
196
            ->expects($this->once())
197
            ->method('invalidateTags')
198
            ->with($tags);
199
200
        /** @var \eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler $handler */
201
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
202
        $handler->deleteTrashItem($trashedId);
203
    }
204
205
    public function testEmptyTrash()
206
    {
207
        $trashedId = 6;
208
        $contentId = 42;
209
        $relationSourceContentId = 44;
210
211
        $handlerMethodName = $this->getHandlerMethodName();
212
213
        $innerHandler = $this->createMock($this->getHandlerClassName());
214
215
        $innerHandler
216
            ->expects($this->once())
217
            ->method('findTrashItems')
218
            ->will($this->returnValue([new Trashed(['id' => $trashedId, 'contentId' => $contentId])]));
219
220
        $this->persistenceHandlerMock
221
            ->method($handlerMethodName)
222
            ->will($this->returnValue($innerHandler));
223
224
        $contentHandlerMock = $this->createMock(ContentHandler::class);
225
226
        $contentHandlerMock
227
            ->expects($this->once())
228
            ->method('loadReverseRelations')
229
            ->with($contentId)
230
            ->will($this->returnValue([new Relation(['sourceContentId' => $relationSourceContentId])]));
231
232
        $this->persistenceHandlerMock
233
            ->method('contentHandler')
234
            ->will($this->returnValue($contentHandlerMock));
235
236
        $tags = [
237
            'content-' . $relationSourceContentId,
238
            'content-' . $contentId,
239
            'location-path-' . $trashedId,
240
        ];
241
242
        $this->cacheMock
243
            ->expects($this->once())
244
            ->method('invalidateTags')
245
            ->with($tags);
246
247
        /** @var \eZ\Publish\SPI\Persistence\Content\Location\Trash\Handler $handler */
248
        $handler = $this->persistenceCacheHandler->$handlerMethodName();
249
        $handler->emptyTrash();
250
    }
251
}
252