Completed
Push — feature-EZP-25696 ( c75b51...ce24f7 )
by André
24:57 queued 26s
created

ObjectStateHandlerTest::testLoadObjectStates()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 58
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 76
rs 8.9667

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
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Persistence\Cache\Tests;
12
13
use eZ\Publish\SPI\Persistence\Content\ObjectState as SPIObjectState;
14
use eZ\Publish\SPI\Persistence\Content\ObjectState\Group as SPIObjectStateGroup;
15
use eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct as SPIInputStruct;
16
17
/**
18
 * Test case for Persistence\Cache\ObjectStateHandler.
19
 */
20
class ObjectStateHandlerTest extends HandlerTest
21
{
22
    /**
23
     * @covers eZ\Publish\Core\Persistence\Cache\ObjectStateHandler::createGroup
24
     */
25
    public function testCreateGroup()
26
    {
27
        $inputStruct = new SPIInputStruct(
28
            array('identifier' => 'test_state_group', 'name' => 'Test State Group')
29
        );
30
        $expectedGroup = new SPIObjectStateGroup(
31
            array(
32
                'id' => 1,
33
                'identifier' => 'test_state_group',
34
                'name' => 'Test State Group',
35
            )
36
        );
37
        $this->loggerMock->expects($this->once())->method('logCall');
38
39
        $innerHandler = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
40
        $this->persistenceHandlerMock
41
            ->expects($this->once())
42
            ->method('objectStateHandler')
43
            ->will($this->returnValue($innerHandler));
44
45
        $innerHandler
46
            ->expects($this->once())
47
            ->method('createGroup')
48
            ->with($inputStruct)
49
            ->will(
50
                $this->returnValue($expectedGroup)
51
            );
52
53
        $this->cacheMock
54
            ->expects($this->once())
55
            ->method('clear')
56
            ->with('objectstategroup', 'all')
57
            ->will($this->returnValue(null));
58
59
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
60
        $this->cacheMock
61
            ->expects($this->once())
62
            ->method('getItem')
63
            ->with('objectstategroup', 1)
64
            ->will($this->returnValue($cacheItemMock));
65
66
        $cacheItemMock
67
            ->expects($this->once())
68
            ->method('set')
69
            ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Group'))
70
            ->will($this->returnValue($cacheItemMock));
71
72
        $cacheItemMock
73
            ->expects($this->once())
74
            ->method('save')
75
            ->with();
76
77
        $handler = $this->persistenceCacheHandler->objectStateHandler();
78
        $group = $handler->createGroup($inputStruct);
79
        $this->assertEquals($group, $expectedGroup);
80
    }
81
82
    /**
83
     * @covers eZ\Publish\Core\Persistence\Cache\ObjectStateHandler::loadGroup
84
     */
85 View Code Duplication
    public function testLoadGroup()
86
    {
87
        $expectedGroup = new SPIObjectStateGroup(
88
            array(
89
                'id' => 1,
90
                'identifier' => 'test_state_group',
91
                'name' => 'Test State Group',
92
            )
93
        );
94
95
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
96
        $this->cacheMock
97
            ->expects($this->once())
98
            ->method('getItem')
99
            ->with('objectstategroup', 1)
100
            ->will($this->returnValue($cacheItemMock));
101
        $cacheItemMock
102
            ->expects($this->once())
103
            ->method('get')
104
            ->will($this->returnValue(null));
105
        $cacheItemMock
106
            ->expects($this->once())
107
            ->method('isMiss')
108
            ->will($this->returnValue(true));
109
        $cacheItemMock
110
            ->expects($this->once())
111
            ->method('set')
112
            ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Group'))
113
            ->will($this->returnValue($cacheItemMock));
114
        $cacheItemMock
115
            ->expects($this->once())
116
            ->method('save')
117
            ->with();
118
119
        $this->loggerMock->expects($this->once())->method('logCall');
120
121
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
122
        $this->persistenceHandlerMock
123
            ->expects($this->once())
124
            ->method('objectStateHandler')
125
            ->will($this->returnValue($innerHandlerMock));
126
127
        $innerHandlerMock
128
            ->expects($this->once())
129
            ->method('loadGroup')
130
            ->with(1)
131
            ->will($this->returnValue($expectedGroup));
132
133
        $handler = $this->persistenceCacheHandler->objectStateHandler();
134
        $group = $handler->loadGroup(1);
135
        $this->assertEquals($group, $expectedGroup);
136
    }
137
138
    /**
139
     * @covers eZ\Publish\Core\Persistence\Cache\ObjectStateHandler::loadGroup
140
     * @depends testLoadGroup
141
     */
142 View Code Duplication
    public function testLoadGroupHasCache()
143
    {
144
        $expectedGroup = new SPIObjectStateGroup(
145
            array(
146
                'id' => 1,
147
                'identifier' => 'test_state_group',
148
                'name' => 'Test State Group',
149
            )
150
        );
151
152
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
153
        $this->cacheMock
154
            ->expects($this->once())
155
            ->method('getItem')
156
            ->with('objectstategroup', 1)
157
            ->will($this->returnValue($cacheItemMock));
158
        $cacheItemMock
159
            ->expects($this->once())
160
            ->method('get')
161
            ->will($this->returnValue($expectedGroup));
162
163
        $handler = $this->persistenceCacheHandler->objectStateHandler();
164
        $group = $handler->loadGroup(1);
165
        $this->assertEquals($group, $expectedGroup);
166
    }
167
168
    /**
169
     * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadGroupByIdentifier
170
     */
171
    public function testLoadGroupByIdentifier()
172
    {
173
        $expectedGroup = new SPIObjectStateGroup(
174
            array(
175
                'id' => 1,
176
                'identifier' => 'test_state_group',
177
                'name' => 'Test State Group',
178
            )
179
        );
180
181
        $this->loggerMock->expects($this->once())->method('logCall');
182
183
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
184
        $this->persistenceHandlerMock
185
            ->expects($this->once())
186
            ->method('objectStateHandler')
187
            ->will($this->returnValue($innerHandlerMock));
188
189
        $innerHandlerMock
190
            ->expects($this->once())
191
            ->method('loadGroupByIdentifier')
192
            ->with($expectedGroup->identifier)
193
            ->will($this->returnValue($expectedGroup));
194
195
        $group = $this->persistenceCacheHandler->objectStateHandler()
196
            ->loadGroupByIdentifier('test_state_group');
197
        $this->assertEquals($group, $expectedGroup);
198
    }
199
200
    public function generateObjectGroupsArray()
201
    {
202
        $result = array();
203
        for ($i = 1; $i <= 20; ++$i) {
204
            $result[] = new SPIObjectStateGroup(
205
                array(
206
                    'id' => $i,
207
                    'identifier' => "test_state_group_${i}",
208
                    'name' => "Test State Group #${i}",
209
                )
210
            );
211
        }
212
213
        return $result;
214
    }
215
216
    /**
217
     * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadAllGroups
218
     */
219
    public function testLoadAllGroups($offset = 0, $limit = -1)
220
    {
221
        $testGroups = $this->generateObjectGroupsArray();
222
        $testGroupIds = array_map(
223
            function ($group) {
224
                return $group->id;
225
            },
226
            $testGroups
227
        );
228
229
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
230
        $this->cacheMock
231
            ->expects($this->at(0))
232
            ->method('getItem')
233
            ->with('objectstategroup', 'all')
234
            ->will($this->returnValue($cacheItemMock));
235
        $cacheItemMock
236
            ->expects($this->once())
237
            ->method('get')
238
            ->will($this->returnValue(null));
239
        $cacheItemMock
240
            ->expects($this->once())
241
            ->method('isMiss')
242
            ->will($this->returnValue(true));
243
244
        $this->loggerMock->expects($this->once())->method('logCall');
245
246
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
247
        $this->persistenceHandlerMock
248
            ->expects($this->once())
249
            ->method('objectStateHandler')
250
            ->will($this->returnValue($innerHandlerMock));
251
252
        $innerHandlerMock
253
            ->expects($this->once())
254
            ->method('loadAllGroups')
255
            ->with(0, -1)
256
            ->will($this->returnValue($testGroups));
257
258
        foreach ($testGroups as $group) {
259
            $this->cacheMock
260
                ->expects($this->at($group->id))
261
                ->method('getItem')
262
                ->with('objectstategroup', $group->id)
263
                ->will(
264
                    $this->returnCallback(
265
                        function ($cachekey, $i) use ($group) {
0 ignored issues
show
Unused Code introduced by
The parameter $cachekey 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...
Unused Code introduced by
The parameter $i 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...
266
                            $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
267
                            $cacheItemMock
268
                                ->expects($this->once())
269
                                ->method('set')
270
                                ->with($group)
271
                                ->will($this->returnValue($cacheItemMock));
272
273
                            $cacheItemMock
274
                                ->expects($this->once())
275
                                ->method('save')
276
                                ->with();
277
278
                            return $cacheItemMock;
279
                        }
280
                    )
281
                );
282
        }
283
284
        $cacheItemMock
285
            ->expects($this->once())
286
            ->method('set')
287
            ->with($testGroupIds)
288
            ->will($this->returnValue($cacheItemMock));
289
290
        $cacheItemMock
291
            ->expects($this->once())
292
            ->method('save')
293
            ->with();
294
295
        $handler = $this->persistenceCacheHandler->objectStateHandler();
296
        $groups = $handler->loadAllGroups($offset, $limit);
297
298
        $expectedGroups = array_slice($testGroups, $offset, $limit > -1 ?: null);
299
        $this->assertEquals($groups, $expectedGroups);
300
    }
301
302
    /**
303
     * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadAllGroups
304
     */
305
    public function testLoadAllGroupsCached($offset = 0, $limit = -1)
306
    {
307
        $testGroups = $this->generateObjectGroupsArray($offset, $limit);
308
        $testGroupIds = array_map(
309
            function ($group) {
310
                return $group->id;
311
            },
312
            $testGroups
313
        );
314
315
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
316
        $this->cacheMock
317
            ->expects($this->at(0))
318
            ->method('getItem')
319
            ->with('objectstategroup', 'all')
320
            ->will($this->returnValue($cacheItemMock));
321
        $cacheItemMock
322
            ->expects($this->once())
323
            ->method('get')
324
            ->will($this->returnValue($testGroupIds));
325
        $cacheItemMock
326
            ->expects($this->once())
327
            ->method('isMiss')
328
            ->will($this->returnValue(false));
329
330
        $expectedGroups = array_slice($testGroups, $offset, $limit > -1 ?: null);
331
332
        // loadGroup()
333 View Code Duplication
        foreach ($expectedGroups as $i => $group) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
334
            $this->cacheMock
335
                ->expects($this->at($i + 1))
336
                ->method('getItem')
337
                ->with('objectstategroup', $group->id)
338
                ->will(
339
                    $this->returnCallback(
340
                        function ($cachekey, $i) use ($group) {
0 ignored issues
show
Unused Code introduced by
The parameter $cachekey 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...
Unused Code introduced by
The parameter $i 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...
341
                            $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
342
                            $cacheItemMock
343
                                ->expects($this->once())
344
                                ->method('get')
345
                                ->will($this->returnValue($group));
346
347
                            return $cacheItemMock;
348
                        }
349
                    )
350
                );
351
        }
352
353
        $handler = $this->persistenceCacheHandler->objectStateHandler();
354
        $groups = $handler->loadAllGroups($offset, $limit);
355
        $this->assertEquals($groups, $expectedGroups);
356
    }
357
358
    /**
359
     * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadAllGroups
360
     */
361
    public function testLoadAllGroupsWithOffsetLimit()
362
    {
363
        $this->testLoadAllGroups(7, 5);
364
    }
365
366
    /**
367
     * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadAllGroups
368
     */
369
    public function testLoadAllGroupsCachedWithOffsetLimit()
370
    {
371
        $this->testLoadAllGroupsCached(7, 5);
372
    }
373
374
    /**
375
     * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadObjectStates
376
     */
377
    public function testLoadObjectStates()
378
    {
379
        $testStates = array(
380
            new SPIObjectState(
381
                array(
382
                    'id' => 1,
383
                    'identifier' => 'test_state_1_group1',
384
                    'groupId' => 1,
385
                )
386
            ),
387
            new SPIObjectState(
388
                array(
389
                    'id' => 2,
390
                    'identifier' => 'test_state_2_group1',
391
                    'groupId' => 1,
392
                )
393
            ),
394
            new SPIObjectState(
395
                array(
396
                    'id' => 3,
397
                    'identifier' => 'test_state_3_group1',
398
                    'groupId' => 1,
399
                )
400
            ),
401
        );
402
        $testStateIds = array_map(
403
            function ($state) {
404
                return $state->id;
405
            },
406
            $testStates
407
        );
408
409
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
410
        $this->cacheMock
411
            ->expects($this->at(0))
412
            ->method('getItem')
413
            ->with('objectstate', 'byGroup', 1)
414
            ->will($this->returnValue($cacheItemMock));
415
        $cacheItemMock
416
            ->expects($this->once())
417
            ->method('get')
418
            ->will($this->returnValue(null));
419
        $cacheItemMock
420
            ->expects($this->once())
421
            ->method('isMiss')
422
            ->will($this->returnValue(true));
423
424
        $this->loggerMock->expects($this->once())->method('logCall');
425
426
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
427
        $this->persistenceHandlerMock
428
            ->expects($this->once())
429
            ->method('objectStateHandler')
430
            ->will($this->returnValue($innerHandlerMock));
431
432
        $innerHandlerMock
433
            ->expects($this->once())
434
            ->method('loadObjectStates')
435
            ->with(1)
436
            ->will($this->returnValue($testStates));
437
438
        $cacheItemMock
439
            ->expects($this->once())
440
            ->method('set')
441
            ->with($testStateIds)
442
            ->will($this->returnValue($cacheItemMock));
443
444
        $cacheItemMock
445
            ->expects($this->once())
446
            ->method('save')
447
            ->with();
448
449
        $handler = $this->persistenceCacheHandler->objectStateHandler();
450
        $states = $handler->loadObjectStates(1);
451
        $this->assertEquals($states, $testStates);
452
    }
453
454
    /**
455
     * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadObjectStates
456
     */
457
    public function testLoadObjectStatesCached()
458
    {
459
        $testStates = array(
460
            new SPIObjectState(
461
                array(
462
                    'id' => 1,
463
                    'identifier' => 'test_state_1_group1',
464
                    'groupId' => 1,
465
                )
466
            ),
467
            new SPIObjectState(
468
                array(
469
                    'id' => 2,
470
                    'identifier' => 'test_state_2_group1',
471
                    'groupId' => 1,
472
                )
473
            ),
474
            new SPIObjectState(
475
                array(
476
                    'id' => 3,
477
                    'identifier' => 'test_state_3_group1',
478
                    'groupId' => 1,
479
                )
480
            ),
481
        );
482
        $testStateIds = array_map(
483
            function ($state) {
484
                return $state->id;
485
            },
486
            $testStates
487
        );
488
489
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
490
        $this->cacheMock
491
            ->expects($this->at(0))
492
            ->method('getItem')
493
            ->with('objectstate', 'byGroup', 1)
494
            ->will($this->returnValue($cacheItemMock));
495
        $cacheItemMock
496
            ->expects($this->once())
497
            ->method('get')
498
            ->will($this->returnValue($testStateIds));
499
        $cacheItemMock
500
            ->expects($this->once())
501
            ->method('isMiss')
502
            ->will($this->returnValue(false));
503
504
        // load()
505 View Code Duplication
        foreach ($testStates as $i => $state) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
506
            $this->cacheMock
507
                ->expects($this->at($i + 1))
508
                ->method('getItem')
509
                ->with('objectstate', $state->id)
510
                ->will(
511
                    $this->returnCallback(
512
                        function ($cachekey, $i) use ($state) {
0 ignored issues
show
Unused Code introduced by
The parameter $cachekey 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...
Unused Code introduced by
The parameter $i 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...
513
                            $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
514
                            $cacheItemMock
515
                                ->expects($this->once())
516
                                ->method('get')
517
                                ->will($this->returnValue($state));
518
519
                            return $cacheItemMock;
520
                        }
521
                    )
522
                );
523
        }
524
525
        $handler = $this->persistenceCacheHandler->objectStateHandler();
526
        $states = $handler->loadObjectStates(1);
527
        $this->assertEquals($states, $testStates);
528
    }
529
530
    /**
531
     * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::updateGroup
532
     */
533
    public function testUpdateGroup()
534
    {
535
        $inputStruct = new SPIInputStruct(
536
            array('identifier' => 'test_state_group', 'name' => 'Test State Group (New)')
537
        );
538
        $expectedGroup = new SPIObjectStateGroup(
539
            array(
540
                'id' => 1,
541
                'identifier' => 'test_state_group_new',
542
                'name' => 'Test State Group (New)',
543
            )
544
        );
545
546
        $this->loggerMock->expects($this->once())->method('logCall');
547
548
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
549
        $this->persistenceHandlerMock
550
            ->expects($this->once())
551
            ->method('objectStateHandler')
552
            ->will($this->returnValue($innerHandlerMock));
553
554
        $innerHandlerMock
555
            ->expects($this->once())
556
            ->method('updateGroup')
557
            ->with(1, $inputStruct)
558
            ->will($this->returnValue($expectedGroup));
559
560
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
0 ignored issues
show
Unused Code introduced by
$cacheItemMock is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
561
        $this->cacheMock
562
            ->expects($this->at(0))
563
            ->method('clear')
564
            ->with('objectstategroup', 1);
565
566
        $handler = $this->persistenceCacheHandler->objectStateHandler();
567
        $newGroup = $handler->updateGroup(1, $inputStruct);
568
        $this->assertEquals($newGroup, $expectedGroup);
569
    }
570
571
    /**
572
     * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::deleteGroup
573
     */
574
    public function testDeleteGroup()
575
    {
576
        $this->loggerMock->expects($this->once())->method('logCall');
577
578
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
579
        $this->persistenceHandlerMock
580
            ->expects($this->once())
581
            ->method('objectStateHandler')
582
            ->will($this->returnValue($innerHandlerMock));
583
584
        $innerHandlerMock
585
            ->expects($this->once())
586
            ->method('deleteGroup')
587
            ->with(1);
588
589
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
0 ignored issues
show
Unused Code introduced by
$cacheItemMock is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
590
        $this->cacheMock
591
            ->expects($this->at(0))
592
            ->method('clear')
593
            ->with('objectstategroup', 'all');
594
        $this->cacheMock
595
            ->expects($this->at(1))
596
            ->method('clear')
597
            ->with('objectstategroup', 1);
598
        $this->cacheMock
599
            ->expects($this->at(2))
600
            ->method('clear')
601
            ->with('objectstate', 'byGroup', 1);
602
603
        $handler = $this->persistenceCacheHandler->objectStateHandler();
604
        $handler->deleteGroup(1);
605
    }
606
607
    /**
608
     * @covers eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::create
609
     */
610
    public function testCreate()
611
    {
612
        $inputStruct = new SPIInputStruct(
613
            array('identifier' => 'test_state', 'name' => 'Test State')
614
        );
615
        $expectedState = new SPIObjectState(
616
            array(
617
                'id' => 1,
618
                'identifier' => 'test_state',
619
                'name' => 'Test State',
620
                'groupId' => 1,
621
            )
622
        );
623
624
        $this->loggerMock->expects($this->once())->method('logCall');
625
626
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
627
        $this->persistenceHandlerMock
628
            ->expects($this->once())
629
            ->method('objectStateHandler')
630
            ->will($this->returnValue($innerHandlerMock));
631
632
        $innerHandlerMock
633
            ->expects($this->once())
634
            ->method('create')
635
            ->with(1, $inputStruct)
636
            ->will($this->returnValue($expectedState));
637
638
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
0 ignored issues
show
Unused Code introduced by
$cacheItemMock is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
639
        $this->cacheMock
640
            ->expects($this->at(0))
641
            ->method('clear')
642
            ->with('objectstate', 'byGroup', 1);
643
644
        $handler = $this->persistenceCacheHandler->objectStateHandler();
645
        $state = $handler->create(1, $inputStruct);
646
        $this->assertEquals($state, $expectedState);
647
    }
648
649
    /**
650
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::load
651
     */
652 View Code Duplication
    public function testLoad()
653
    {
654
        $expectedState = new SPIObjectState(
655
            array(
656
                'id' => 1,
657
                'identifier' => 'test_state',
658
                'name' => 'Test State',
659
                'groupId' => 1,
660
            )
661
        );
662
663
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
664
        $this->cacheMock
665
            ->expects($this->once())
666
            ->method('getItem')
667
            ->with('objectstate', 1)
668
            ->will($this->returnValue($cacheItemMock));
669
        $cacheItemMock
670
            ->expects($this->once())
671
            ->method('get')
672
            ->will($this->returnValue(null));
673
        $cacheItemMock
674
            ->expects($this->once())
675
            ->method('isMiss')
676
            ->will($this->returnValue(true));
677
        $cacheItemMock
678
            ->expects($this->once())
679
            ->method('set')
680
            ->with($this->isInstanceOf('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState'))
681
            ->will($this->returnValue($cacheItemMock));
682
        $cacheItemMock
683
            ->expects($this->once())
684
            ->method('save')
685
            ->with();
686
687
        $this->loggerMock->expects($this->once())->method('logCall');
688
689
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
690
        $this->persistenceHandlerMock
691
            ->expects($this->once())
692
            ->method('objectStateHandler')
693
            ->will($this->returnValue($innerHandlerMock));
694
695
        $innerHandlerMock
696
            ->expects($this->once())
697
            ->method('load')
698
            ->with(1)
699
            ->will($this->returnValue($expectedState));
700
701
        $handler = $this->persistenceCacheHandler->objectStateHandler();
702
        $state = $handler->load(1);
703
        $this->assertEquals($state, $expectedState);
704
    }
705
706
    /**
707
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::load
708
     * @depends testLoad
709
     */
710 View Code Duplication
    public function testLoadCached()
711
    {
712
        $expectedState = new SPIObjectState(
713
            array(
714
                'id' => 1,
715
                'identifier' => 'test_state',
716
                'name' => 'Test State',
717
                'groupId' => 1,
718
            )
719
        );
720
721
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
722
        $this->cacheMock
723
            ->expects($this->once())
724
            ->method('getItem')
725
            ->with('objectstate', 1)
726
            ->will($this->returnValue($cacheItemMock));
727
        $cacheItemMock
728
            ->expects($this->once())
729
            ->method('get')
730
            ->will($this->returnValue($expectedState));
731
732
        $handler = $this->persistenceCacheHandler->objectStateHandler();
733
        $state = $handler->load(1);
734
        $this->assertEquals($state, $expectedState);
735
    }
736
737
    /**
738
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadByIdentifier
739
     */
740
    public function testLoadByIdentifier()
741
    {
742
        $expectedState = new SPIObjectState(
743
            array(
744
                'id' => 1,
745
                'identifier' => 'test_state',
746
                'name' => 'Test State',
747
                'groupId' => 1,
748
            )
749
        );
750
751
        $this->loggerMock->expects($this->once())->method('logCall');
752
753
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
754
        $this->persistenceHandlerMock
755
            ->expects($this->once())
756
            ->method('objectStateHandler')
757
            ->will($this->returnValue($innerHandlerMock));
758
759
        $innerHandlerMock
760
            ->expects($this->once())
761
            ->method('loadByIdentifier')
762
            ->with($expectedState->identifier, $expectedState->groupId)
763
            ->will($this->returnValue($expectedState));
764
765
        $handler = $this->persistenceCacheHandler->objectStateHandler();
766
        $state = $handler->loadByIdentifier($expectedState->identifier, $expectedState->groupId);
767
        $this->assertEquals($state, $expectedState);
768
    }
769
770
    /**
771
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::update
772
     */
773
    public function testUpdate()
774
    {
775
        $inputStruct = new SPIInputStruct(
776
            array('identifier' => 'test_state_new', 'name' => 'Test State (new)')
777
        );
778
779
        $expectedState = new SPIObjectState(
780
            array(
781
                'id' => 1,
782
                'identifier' => 'test_state',
783
                'name' => 'Test State',
784
                'groupId' => 1,
785
            )
786
        );
787
788
        $this->loggerMock->expects($this->once())->method('logCall');
789
790
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
791
        $this->persistenceHandlerMock
792
            ->expects($this->once())
793
            ->method('objectStateHandler')
794
            ->will($this->returnValue($innerHandlerMock));
795
796
        $innerHandlerMock
797
            ->expects($this->once())
798
            ->method('update')
799
            ->with($expectedState->id, $inputStruct)
800
            ->will($this->returnValue($expectedState));
801
802
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
0 ignored issues
show
Unused Code introduced by
$cacheItemMock is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
803
        $this->cacheMock
804
            ->expects($this->at(0))
805
            ->method('clear')
806
            ->with('objectstate', $expectedState->id);
807
808
        $handler = $this->persistenceCacheHandler->objectStateHandler();
809
        $state = $handler->update($expectedState->id, $inputStruct);
810
        $this->assertEquals($state, $expectedState);
811
    }
812
813
    /**
814
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::setPriority
815
     */
816
    public function testSetPriority()
817
    {
818
        $expectedState = new SPIObjectState(
819
            array(
820
                'id' => 1,
821
                'identifier' => 'test_state',
822
                'name' => 'Test State',
823
                'groupId' => 1,
824
                'priority' => 1,
825
            )
826
        );
827
828
        $this->loggerMock->expects($this->once())->method('logCall');
829
830
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
831
        $this->persistenceHandlerMock
832
            ->expects($this->once())
833
            ->method('objectStateHandler')
834
            ->will($this->returnValue($innerHandlerMock));
835
836
        $innerHandlerMock
837
            ->expects($this->once())
838
            ->method('setPriority')
839
            ->with($expectedState->id, 1)
840
            ->will($this->returnValue($expectedState));
841
842
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
0 ignored issues
show
Unused Code introduced by
$cacheItemMock is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
843
        $this->cacheMock
844
            ->expects($this->at(0))
845
            ->method('clear')
846
            ->with('objectstate', $expectedState->id);
847
848
        $handler = $this->persistenceCacheHandler->objectStateHandler();
849
        $state = $handler->setPriority($expectedState->id, 1);
850
        $this->assertEquals($state, $expectedState);
851
    }
852
853
    /**
854
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::delete
855
     */
856
    public function testDelete()
857
    {
858
        $this->loggerMock->expects($this->once())->method('logCall');
859
860
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
861
        $this->persistenceHandlerMock
862
            ->expects($this->once())
863
            ->method('objectStateHandler')
864
            ->will($this->returnValue($innerHandlerMock));
865
866
        $innerHandlerMock
867
            ->expects($this->once())
868
            ->method('delete')
869
            ->with(1);
870
871
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
0 ignored issues
show
Unused Code introduced by
$cacheItemMock is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
872
        $this->cacheMock
873
            ->expects($this->at(0))
874
            ->method('clear')
875
            ->with('objectstate', 1);
876
        $this->cacheMock
877
            ->expects($this->at(1))
878
            ->method('clear')
879
            ->with('objectstate', 'byGroup');
880
881
        $handler = $this->persistenceCacheHandler->objectStateHandler();
882
        $state = $handler->delete(1);
0 ignored issues
show
Unused Code introduced by
$state is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
883
    }
884
885
    /**
886
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::setContentState
887
     */
888
    public function testSetContentState()
889
    {
890
        $this->loggerMock->expects($this->once())->method('logCall');
891
892
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
893
        $this->persistenceHandlerMock
894
            ->expects($this->once())
895
            ->method('objectStateHandler')
896
            ->will($this->returnValue($innerHandlerMock));
897
898
        $innerHandlerMock
899
            ->expects($this->once())
900
            ->method('setContentState')
901
            ->with(10, 1, 2);
902
903
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
0 ignored issues
show
Unused Code introduced by
$cacheItemMock is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
904
        $this->cacheMock
905
            ->expects($this->at(0))
906
            ->method('clear')
907
            ->with('objectstate', 'byContent', 10, 1);
908
909
        $handler = $this->persistenceCacheHandler->objectStateHandler();
910
        $state = $handler->setContentState(10, 1, 2);
0 ignored issues
show
Unused Code introduced by
$state is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
911
    }
912
913
    /**
914
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentState
915
     */
916
    public function testGetContentState()
917
    {
918
        $expectedState = new SPIObjectState(
919
            array(
920
                'id' => 1,
921
                'identifier' => 'test_state',
922
                'name' => 'Test State',
923
                'groupId' => 1,
924
                'priority' => 1,
925
            )
926
        );
927
928
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
929
        $this->cacheMock
930
            ->expects($this->at(0))
931
            ->method('getItem')
932
            ->with('objectstate', 'byContent', 10, 1)
933
            ->will($this->returnValue($cacheItemMock));
934
        $cacheItemMock
935
            ->expects($this->once())
936
            ->method('get')
937
            ->will($this->returnValue(null));
938
        $cacheItemMock
939
            ->expects($this->once())
940
            ->method('isMiss')
941
            ->will($this->returnValue(true));
942
        $cacheItemMock
943
            ->expects($this->once())
944
            ->method('set')
945
            ->with(1)
946
            ->will($this->returnValue($cacheItemMock));
947
        $cacheItemMock
948
            ->expects($this->once())
949
            ->method('save')
950
            ->with();
951
952
        $this->loggerMock->expects($this->once())->method('logCall');
953
954
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
955
        $this->persistenceHandlerMock
956
            ->expects($this->once())
957
            ->method('objectStateHandler')
958
            ->will($this->returnValue($innerHandlerMock));
959
960
        $innerHandlerMock
961
            ->expects($this->once())
962
            ->method('getContentState')
963
            ->with(10, 1)
964
            ->will($this->returnValue($expectedState));
965
966
        $handler = $this->persistenceCacheHandler->objectStateHandler();
967
        $state = $handler->getContentState(10, 1, 2);
968
        $this->assertEquals($state, $expectedState);
969
    }
970
971
    /**
972
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentState
973
     */
974
    public function testGetContentStateCached()
975
    {
976
        $expectedState = new SPIObjectState(
977
            array(
978
                'id' => 1,
979
                'identifier' => 'test_state',
980
                'name' => 'Test State',
981
                'groupId' => 1,
982
            )
983
        );
984
985
        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
986
        $this->cacheMock
987
            ->expects($this->at(0))
988
            ->method('getItem')
989
            ->with('objectstate', 'byContent', 10, 1)
990
            ->will($this->returnValue($cacheItemMock));
991
        $cacheItemMock
992
            ->expects($this->once())
993
            ->method('get')
994
            ->will($this->returnValue(1));
995
        $cacheItemMock
996
            ->expects($this->once())
997
            ->method('isMiss')
998
            ->will($this->returnValue(false));
999
1000
        // load()
1001
        $this->cacheMock
1002
            ->expects($this->at(1))
1003
            ->method('getItem')
1004
            ->with('objectstate', $expectedState->id)
1005
            ->will(
1006
                $this->returnCallback(
1007
                    function ($cachekey, $i) use ($expectedState) {
0 ignored issues
show
Unused Code introduced by
The parameter $cachekey 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...
Unused Code introduced by
The parameter $i 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...
1008
                        $cacheItemMock = $this->getMock('Stash\Interfaces\ItemInterface');
1009
                        $cacheItemMock
1010
                            ->expects($this->once())
1011
                            ->method('get')
1012
                            ->will($this->returnValue($expectedState));
1013
1014
                        return $cacheItemMock;
1015
                    }
1016
                )
1017
            );
1018
1019
        $handler = $this->persistenceCacheHandler->objectStateHandler();
1020
        $state = $handler->getContentState(10, 1);
1021
        $this->assertEquals($state, $expectedState);
1022
    }
1023
1024
    /**
1025
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentCount
1026
     */
1027
    public function testGetContentCount()
1028
    {
1029
        $expectedCount = 2;
1030
1031
        $this->loggerMock->expects($this->once())->method('logCall');
1032
1033
        $innerHandlerMock = $this->getMock('eZ\\Publish\\SPI\\Persistence\\Content\\ObjectState\\Handler');
1034
        $this->persistenceHandlerMock
1035
            ->expects($this->once())
1036
            ->method('objectStateHandler')
1037
            ->will($this->returnValue($innerHandlerMock));
1038
1039
        $innerHandlerMock
1040
            ->expects($this->once())
1041
            ->method('getContentCount')
1042
            ->with(1)
1043
            ->will($this->returnValue($expectedCount));
1044
1045
        //$this->logger->logCall( __METHOD__, array( 'stateId' => $stateId ) );
1046
1047
        $handler = $this->persistenceCacheHandler->objectStateHandler();
1048
        $count = $handler->getContentCount(1);
1049
        $this->assertEquals($count, $expectedCount);
1050
    }
1051
}
1052