Completed
Push — master ( 28c8cd...7e44c6 )
by André
18:53
created

SectionHandlerTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 407
Duplicated Lines 69.53 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 283
loc 407
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 6

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getSPISectionHandlerMock() 0 4 1
B testAssign() 31 31 1
A testAssignmentsCount() 22 22 1
A testPoliciesCount() 22 22 1
A testCountRoleAssignmentsUsingSection() 22 22 1
A testCreate() 47 47 1
B testDelete() 27 27 1
A testLoadCacheIsMiss() 0 52 1
B testLoadHasCache() 37 37 1
A testLoadAll() 0 21 1
B testLoadByIdentifier() 28 28 1
A testUpdate() 47 47 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\SPI\Persistence\Content\Section as SPISection;
12
use eZ\Publish\SPI\Persistence\Content\Section\Handler as SPISectionHandler;
13
use Stash\Interfaces\ItemInterface;
14
15
/**
16
 * Test case for Persistence\Cache\SectionHandler.
17
 */
18
class SectionHandlerTest extends HandlerTest
19
{
20
    protected function getSPISectionHandlerMock()
21
    {
22
        return $this->createMock(SPISectionHandler::class);
23
    }
24
25
    /**
26
     * @covers \eZ\Publish\Core\Persistence\Cache\SectionHandler::assign
27
     */
28 View Code Duplication
    public function testAssign()
29
    {
30
        $this->loggerMock->expects($this->once())->method('logCall');
31
32
        $this->cacheMock
33
            ->expects($this->at(0))
34
            ->method('clear')
35
            ->with('content', 44)
36
            ->will($this->returnValue(null));
37
38
        $this->cacheMock
39
            ->expects($this->at(1))
40
            ->method('clear')
41
            ->with('content', 'info', 44)
42
            ->will($this->returnValue(null));
43
44
        $innerHandler = $this->getSPISectionHandlerMock();
45
        $this->persistenceHandlerMock
46
            ->expects($this->once())
47
            ->method('sectionHandler')
48
            ->will($this->returnValue($innerHandler));
49
50
        $innerHandler
51
            ->expects($this->once())
52
            ->method('assign')
53
            ->with(33, 44)
54
            ->will($this->returnValue(null));
55
56
        $handler = $this->persistenceCacheHandler->sectionHandler();
57
        $handler->assign(33, 44);
58
    }
59
60
    /**
61
     * @covers \eZ\Publish\Core\Persistence\Cache\SectionHandler::assignmentsCount
62
     */
63 View Code Duplication
    public function testAssignmentsCount()
64
    {
65
        $this->loggerMock->expects($this->once())->method('logCall');
66
        $this->cacheMock
67
            ->expects($this->never())
68
            ->method($this->anything());
69
70
        $innerHandler = $this->getSPISectionHandlerMock();
71
        $this->persistenceHandlerMock
72
            ->expects($this->once())
73
            ->method('sectionHandler')
74
            ->will($this->returnValue($innerHandler));
75
76
        $innerHandler
77
            ->expects($this->once())
78
            ->method('assignmentsCount')
79
            ->with(33)
80
            ->will($this->returnValue(null));
81
82
        $handler = $this->persistenceCacheHandler->sectionHandler();
83
        $handler->assignmentsCount(33);
84
    }
85
86
    /**
87
     * @covers \eZ\Publish\Core\Persistence\Cache\SectionHandler::policiesCount
88
     */
89 View Code Duplication
    public function testPoliciesCount()
90
    {
91
        $this->loggerMock->expects($this->once())->method('logCall');
92
        $this->cacheMock
93
            ->expects($this->never())
94
            ->method($this->anything());
95
96
        $innerHandler = $this->getSPISectionHandlerMock();
97
        $this->persistenceHandlerMock
98
            ->expects($this->once())
99
            ->method('sectionHandler')
100
            ->will($this->returnValue($innerHandler));
101
102
        $innerHandler
103
            ->expects($this->once())
104
            ->method('policiesCount')
105
            ->with(1)
106
            ->will($this->returnValue(7));
107
108
        $handler = $this->persistenceCacheHandler->sectionHandler();
109
        $handler->policiesCount(1);
110
    }
111
112
    /**
113
     * @covers \eZ\Publish\Core\Persistence\Cache\SectionHandler::countRoleAssignmentsUsingSection
114
     */
115 View Code Duplication
    public function testCountRoleAssignmentsUsingSection()
116
    {
117
        $this->loggerMock->expects($this->once())->method('logCall');
118
        $this->cacheMock
119
            ->expects($this->never())
120
            ->method($this->anything());
121
122
        $innerHandler = $this->getSPISectionHandlerMock();
123
        $this->persistenceHandlerMock
124
            ->expects($this->once())
125
            ->method('sectionHandler')
126
            ->will($this->returnValue($innerHandler));
127
128
        $innerHandler
129
            ->expects($this->once())
130
            ->method('countRoleAssignmentsUsingSection')
131
            ->with(1)
132
            ->will($this->returnValue(0));
133
134
        $handler = $this->persistenceCacheHandler->sectionHandler();
135
        $handler->countRoleAssignmentsUsingSection(1);
136
    }
137
138
    /**
139
     * @covers \eZ\Publish\Core\Persistence\Cache\SectionHandler::create
140
     */
141 View Code Duplication
    public function testCreate()
142
    {
143
        $this->loggerMock->expects($this->once())->method('logCall');
144
145
        $innerHandlerMock = $this->getSPISectionHandlerMock();
146
        $this->persistenceHandlerMock
147
            ->expects($this->once())
148
            ->method('sectionHandler')
149
            ->will($this->returnValue($innerHandlerMock));
150
151
        $innerHandlerMock
152
            ->expects($this->once())
153
            ->method('create')
154
            ->with('Intranet', 'intranet')
155
            ->will(
156
                $this->returnValue(
157
                    new SPISection(
158
                        array('id' => 33, 'name' => 'Intranet', 'identifier' => 'intranet')
159
                    )
160
                )
161
            );
162
163
        $cacheItemMock = $this->createMock(ItemInterface::class);
164
        $this->cacheMock
165
            ->expects($this->once())
166
            ->method('getItem')
167
            ->with('section', 33)
168
            ->will($this->returnValue($cacheItemMock));
169
170
        $cacheItemMock
171
            ->expects($this->once())
172
            ->method('set')
173
            ->with($this->isInstanceOf(SPISection::class))
174
            ->will($this->returnValue($cacheItemMock));
175
176
        $cacheItemMock
177
            ->expects($this->once())
178
            ->method('save')
179
            ->with();
180
181
        $cacheItemMock
182
            ->expects($this->never())
183
            ->method('get');
184
185
        $handler = $this->persistenceCacheHandler->sectionHandler();
186
        $handler->create('Intranet', 'intranet');
187
    }
188
189
    /**
190
     * @covers \eZ\Publish\Core\Persistence\Cache\SectionHandler::delete
191
     */
192 View Code Duplication
    public function testDelete()
193
    {
194
        $this->loggerMock->expects($this->once())->method('logCall');
195
196
        $innerHandlerMock = $this->getSPISectionHandlerMock();
197
        $this->persistenceHandlerMock
198
            ->expects($this->once())
199
            ->method('sectionHandler')
200
            ->will($this->returnValue($innerHandlerMock));
201
202
        $innerHandlerMock
203
            ->expects($this->once())
204
            ->method('delete')
205
            ->with(33)
206
            ->will(
207
                $this->returnValue(true)
208
            );
209
210
        $this->cacheMock
211
            ->expects($this->once())
212
            ->method('clear')
213
            ->with('section', 33)
214
            ->will($this->returnValue(true));
215
216
        $handler = $this->persistenceCacheHandler->sectionHandler();
217
        $handler->delete(33);
218
    }
219
220
    /**
221
     * @covers \eZ\Publish\Core\Persistence\Cache\SectionHandler::load
222
     */
223
    public function testLoadCacheIsMiss()
224
    {
225
        $this->loggerMock->expects($this->once())->method('logCall');
226
        $cacheItemMock = $this->createMock(ItemInterface::class);
227
        $this->cacheMock
228
            ->expects($this->once())
229
            ->method('getItem')
230
            ->with('section', 33)
231
            ->will($this->returnValue($cacheItemMock));
232
233
        $cacheItemMock
234
            ->expects($this->once())
235
            ->method('isMiss')
236
            ->will($this->returnValue(true));
237
238
        $innerHandlerMock = $this->getSPISectionHandlerMock();
239
        $this->persistenceHandlerMock
240
            ->expects($this->once())
241
            ->method('sectionHandler')
242
            ->will($this->returnValue($innerHandlerMock));
243
244
        $innerHandlerMock
245
            ->expects($this->once())
246
            ->method('load')
247
            ->with(33)
248
            ->will(
249
                $this->returnValue(
250
                    new SPISection(
251
                        array('id' => 33, 'name' => 'Intranet', 'identifier' => 'intranet')
252
                    )
253
                )
254
            );
255
256
        $cacheItemMock
257
            ->expects($this->once())
258
            ->method('set')
259
            ->with($this->isInstanceOf(SPISection::class))
260
            ->will($this->returnValue($cacheItemMock));
261
262
        $cacheItemMock
263
            ->expects($this->once())
264
            ->method('save')
265
            ->with();
266
267
        $cacheItemMock
268
            ->expects($this->once())
269
            ->method('get')
270
            ->will($this->returnValue(null));
271
272
        $handler = $this->persistenceCacheHandler->sectionHandler();
273
        $handler->load(33);
274
    }
275
276
    /**
277
     * @covers \eZ\Publish\Core\Persistence\Cache\SectionHandler::load
278
     */
279 View Code Duplication
    public function testLoadHasCache()
280
    {
281
        $this->loggerMock->expects($this->never())->method($this->anything());
282
        $cacheItemMock = $this->createMock(ItemInterface::class);
283
        $this->cacheMock
284
            ->expects($this->once())
285
            ->method('getItem')
286
            ->with('section', 33)
287
            ->will($this->returnValue($cacheItemMock));
288
289
        $cacheItemMock
290
            ->expects($this->once())
291
            ->method('isMiss')
292
            ->will($this->returnValue(false));
293
294
        $this->persistenceHandlerMock
295
            ->expects($this->never())
296
            ->method('sectionHandler');
297
298
        $cacheItemMock
299
            ->expects($this->once())
300
            ->method('get')
301
            ->will(
302
                $this->returnValue(
303
                    new SPISection(
304
                        array('id' => 33, 'name' => 'Intranet', 'identifier' => 'intranet')
305
                    )
306
                )
307
            );
308
309
        $cacheItemMock
310
            ->expects($this->never())
311
            ->method('set');
312
313
        $handler = $this->persistenceCacheHandler->sectionHandler();
314
        $handler->load(33);
315
    }
316
317
    /**
318
     * @covers \eZ\Publish\Core\Persistence\Cache\SectionHandler::loadAll
319
     */
320
    public function testLoadAll()
321
    {
322
        $this->loggerMock->expects($this->once())->method('logCall');
323
        $this->cacheMock
324
            ->expects($this->never())
325
            ->method($this->anything());
326
327
        $innerHandler = $this->getSPISectionHandlerMock();
328
        $this->persistenceHandlerMock
329
            ->expects($this->once())
330
            ->method('sectionHandler')
331
            ->will($this->returnValue($innerHandler));
332
333
        $innerHandler
334
            ->expects($this->once())
335
            ->method('loadAll')
336
            ->will($this->returnValue(array()));
337
338
        $handler = $this->persistenceCacheHandler->sectionHandler();
339
        $handler->loadAll();
340
    }
341
342
    /**
343
     * @covers \eZ\Publish\Core\Persistence\Cache\SectionHandler::loadByIdentifier
344
     */
345 View Code Duplication
    public function testLoadByIdentifier()
346
    {
347
        $this->loggerMock->expects($this->once())->method('logCall');
348
        $this->cacheMock
349
            ->expects($this->never())
350
            ->method($this->anything());
351
352
        $innerHandler = $this->getSPISectionHandlerMock();
353
        $this->persistenceHandlerMock
354
            ->expects($this->once())
355
            ->method('sectionHandler')
356
            ->will($this->returnValue($innerHandler));
357
358
        $innerHandler
359
            ->expects($this->once())
360
            ->method('loadByIdentifier')
361
            ->with('intranet')
362
            ->will(
363
                $this->returnValue(
364
                    new SPISection(
365
                        array('id' => 33, 'name' => 'Intranet', 'identifier' => 'intranet')
366
                    )
367
                )
368
            );
369
370
        $handler = $this->persistenceCacheHandler->sectionHandler();
371
        $handler->loadByIdentifier('intranet');
372
    }
373
374
    /**
375
     * @covers \eZ\Publish\Core\Persistence\Cache\SectionHandler::update
376
     */
377 View Code Duplication
    public function testUpdate()
378
    {
379
        $this->loggerMock->expects($this->once())->method('logCall');
380
381
        $innerHandler = $this->getSPISectionHandlerMock();
382
        $this->persistenceHandlerMock
383
            ->expects($this->once())
384
            ->method('sectionHandler')
385
            ->will($this->returnValue($innerHandler));
386
387
        $innerHandler
388
            ->expects($this->once())
389
            ->method('update')
390
            ->with(33, 'Old Intranet', 'old_intranet')
391
            ->will(
392
                $this->returnValue(
393
                    new SPISection(
394
                        array('id' => 33, 'name' => 'Old Intranet', 'identifier' => 'old_intranet')
395
                    )
396
                )
397
            );
398
399
        $cacheItemMock = $this->createMock(ItemInterface::class);
400
        $this->cacheMock
401
            ->expects($this->once())
402
            ->method('getItem')
403
            ->with('section', 33)
404
            ->will($this->returnValue($cacheItemMock));
405
406
        $cacheItemMock
407
            ->expects($this->once())
408
            ->method('set')
409
            ->with($this->isInstanceOf(SPISection::class))
410
            ->will($this->returnValue($cacheItemMock));
411
412
        $cacheItemMock
413
            ->expects($this->once())
414
            ->method('save')
415
            ->with();
416
417
        $cacheItemMock
418
            ->expects($this->never())
419
            ->method('get');
420
421
        $handler = $this->persistenceCacheHandler->sectionHandler();
422
        $handler->update(33, 'Old Intranet', 'old_intranet');
423
    }
424
}
425