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

ObjectStateHandlerTest::testUpdateGroup()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 27
nc 1
nop 0
dl 0
loc 37
rs 8.8571
c 0
b 0
f 0
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\ObjectState as SPIObjectState;
12
use eZ\Publish\SPI\Persistence\Content\ObjectState\Handler as SPIObjectStateHandler;
13
use eZ\Publish\SPI\Persistence\Content\ObjectState\Group as SPIObjectStateGroup;
14
use eZ\Publish\SPI\Persistence\Content\ObjectState\InputStruct as SPIInputStruct;
15
use Stash\Interfaces\ItemInterface;
16
17
/**
18
 * Test case for Persistence\Cache\ObjectStateHandler.
19
 */
20
class ObjectStateHandlerTest extends HandlerTest
21
{
22
    protected function getCacheItemMock()
23
    {
24
        return $this->createMock(ItemInterface::class);
25
    }
26
27
    protected function getSPIObjectStateHandler()
28
    {
29
        return $this->createMock(SPIObjectStateHandler::class);
30
    }
31
32
    /**
33
     * @covers \eZ\Publish\Core\Persistence\Cache\ObjectStateHandler::createGroup
34
     */
35
    public function testCreateGroup()
36
    {
37
        $inputStruct = new SPIInputStruct(
38
            array('identifier' => 'test_state_group', 'name' => 'Test State Group')
39
        );
40
        $expectedGroup = new SPIObjectStateGroup(
41
            array(
42
                'id' => 1,
43
                'identifier' => 'test_state_group',
44
                'name' => 'Test State Group',
45
            )
46
        );
47
        $this->loggerMock->expects($this->once())->method('logCall');
48
49
        $innerHandler = $this->getSPIObjectStateHandler();
50
        $this->persistenceHandlerMock
51
            ->expects($this->once())
52
            ->method('objectStateHandler')
53
            ->will($this->returnValue($innerHandler));
54
55
        $innerHandler
56
            ->expects($this->once())
57
            ->method('createGroup')
58
            ->with($inputStruct)
59
            ->will(
60
                $this->returnValue($expectedGroup)
61
            );
62
63
        $this->cacheMock
64
            ->expects($this->once())
65
            ->method('clear')
66
            ->with('objectstategroup', 'all')
67
            ->will($this->returnValue(null));
68
69
        $cacheItemMock = $this->getCacheItemMock();
70
        $this->cacheMock
71
            ->expects($this->once())
72
            ->method('getItem')
73
            ->with('objectstategroup', 1)
74
            ->will($this->returnValue($cacheItemMock));
75
76
        $cacheItemMock
77
            ->expects($this->once())
78
            ->method('set')
79
            ->with($this->isInstanceOf(SPIObjectStateGroup::class))
80
            ->will($this->returnValue($cacheItemMock));
81
82
        $cacheItemMock
83
            ->expects($this->once())
84
            ->method('save')
85
            ->with();
86
87
        $handler = $this->persistenceCacheHandler->objectStateHandler();
88
        $group = $handler->createGroup($inputStruct);
89
        $this->assertEquals($group, $expectedGroup);
90
    }
91
92
    /**
93
     * @covers \eZ\Publish\Core\Persistence\Cache\ObjectStateHandler::loadGroup
94
     */
95 View Code Duplication
    public function testLoadGroup()
96
    {
97
        $expectedGroup = new SPIObjectStateGroup(
98
            array(
99
                'id' => 1,
100
                'identifier' => 'test_state_group',
101
                'name' => 'Test State Group',
102
            )
103
        );
104
105
        $cacheItemMock = $this->getCacheItemMock();
106
        $this->cacheMock
107
            ->expects($this->once())
108
            ->method('getItem')
109
            ->with('objectstategroup', 1)
110
            ->will($this->returnValue($cacheItemMock));
111
        $cacheItemMock
112
            ->expects($this->once())
113
            ->method('get')
114
            ->will($this->returnValue(null));
115
        $cacheItemMock
116
            ->expects($this->once())
117
            ->method('isMiss')
118
            ->will($this->returnValue(true));
119
        $cacheItemMock
120
            ->expects($this->once())
121
            ->method('set')
122
            ->with($this->isInstanceOf(SPIObjectStateGroup::class))
123
            ->will($this->returnValue($cacheItemMock));
124
        $cacheItemMock
125
            ->expects($this->once())
126
            ->method('save')
127
            ->with();
128
129
        $this->loggerMock->expects($this->once())->method('logCall');
130
131
        $innerHandlerMock = $this->getSPIObjectStateHandler();
132
        $this->persistenceHandlerMock
133
            ->expects($this->once())
134
            ->method('objectStateHandler')
135
            ->will($this->returnValue($innerHandlerMock));
136
137
        $innerHandlerMock
138
            ->expects($this->once())
139
            ->method('loadGroup')
140
            ->with(1)
141
            ->will($this->returnValue($expectedGroup));
142
143
        $handler = $this->persistenceCacheHandler->objectStateHandler();
144
        $group = $handler->loadGroup(1);
145
        $this->assertEquals($group, $expectedGroup);
146
    }
147
148
    /**
149
     * @covers \eZ\Publish\Core\Persistence\Cache\ObjectStateHandler::loadGroup
150
     * @depends testLoadGroup
151
     */
152 View Code Duplication
    public function testLoadGroupHasCache()
153
    {
154
        $expectedGroup = new SPIObjectStateGroup(
155
            array(
156
                'id' => 1,
157
                'identifier' => 'test_state_group',
158
                'name' => 'Test State Group',
159
            )
160
        );
161
162
        $cacheItemMock = $this->getCacheItemMock();
163
        $this->cacheMock
164
            ->expects($this->once())
165
            ->method('getItem')
166
            ->with('objectstategroup', 1)
167
            ->will($this->returnValue($cacheItemMock));
168
        $cacheItemMock
169
            ->expects($this->once())
170
            ->method('get')
171
            ->will($this->returnValue($expectedGroup));
172
173
        $handler = $this->persistenceCacheHandler->objectStateHandler();
174
        $group = $handler->loadGroup(1);
175
        $this->assertEquals($group, $expectedGroup);
176
    }
177
178
    /**
179
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadGroupByIdentifier
180
     */
181
    public function testLoadGroupByIdentifier()
182
    {
183
        $expectedGroup = new SPIObjectStateGroup(
184
            array(
185
                'id' => 1,
186
                'identifier' => 'test_state_group',
187
                'name' => 'Test State Group',
188
            )
189
        );
190
191
        $this->loggerMock->expects($this->once())->method('logCall');
192
193
        $innerHandlerMock = $this->getSPIObjectStateHandler();
194
        $this->persistenceHandlerMock
195
            ->expects($this->once())
196
            ->method('objectStateHandler')
197
            ->will($this->returnValue($innerHandlerMock));
198
199
        $innerHandlerMock
200
            ->expects($this->once())
201
            ->method('loadGroupByIdentifier')
202
            ->with($expectedGroup->identifier)
203
            ->will($this->returnValue($expectedGroup));
204
205
        $group = $this->persistenceCacheHandler->objectStateHandler()
206
            ->loadGroupByIdentifier('test_state_group');
207
        $this->assertEquals($group, $expectedGroup);
208
    }
209
210
    public function generateObjectGroupsArray()
211
    {
212
        $result = array();
213
        for ($i = 1; $i <= 20; ++$i) {
214
            $result[] = new SPIObjectStateGroup(
215
                array(
216
                    'id' => $i,
217
                    'identifier' => "test_state_group_${i}",
218
                    'name' => "Test State Group #${i}",
219
                )
220
            );
221
        }
222
223
        return $result;
224
    }
225
226
    /**
227
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadAllGroups
228
     */
229
    public function testLoadAllGroups($offset = 0, $limit = -1)
230
    {
231
        $testGroups = $this->generateObjectGroupsArray();
232
233
        $cacheItemMock = $this->getCacheItemMock();
234
        $this->cacheMock
235
            ->expects($this->at(0))
236
            ->method('getItem')
237
            ->with('objectstategroup', 'all')
238
            ->will($this->returnValue($cacheItemMock));
239
        $cacheItemMock
240
            ->expects($this->once())
241
            ->method('get')
242
            ->will($this->returnValue(null));
243
        $cacheItemMock
244
            ->expects($this->once())
245
            ->method('isMiss')
246
            ->will($this->returnValue(true));
247
248
        $this->loggerMock->expects($this->once())->method('logCall');
249
250
        $innerHandlerMock = $this->getSPIObjectStateHandler();
251
        $this->persistenceHandlerMock
252
            ->expects($this->once())
253
            ->method('objectStateHandler')
254
            ->will($this->returnValue($innerHandlerMock));
255
256
        $innerHandlerMock
257
            ->expects($this->once())
258
            ->method('loadAllGroups')
259
            ->with(0, -1)
260
            ->will($this->returnValue($testGroups));
261
262
//        foreach ($testGroups as $group) {
263
//            $this->cacheMock
264
//                ->expects($this->at($group->id))
265
//                ->method('getItem')
266
//                ->with('objectstategroup', $group->id)
267
//                ->will(
268
//                    $this->returnCallback(
269
//                        function ($cachekey, $i) use ($group) {
270
//                            $cacheItemMock = $this->getCacheItemMock();
271
//                            $cacheItemMock
272
//                                ->expects($this->once())
273
//                                ->method('set')
274
//                                ->with($group)
275
//                                ->will($this->returnValue($cacheItemMock));
276
//
277
//                            $cacheItemMock
278
//                                ->expects($this->once())
279
//                                ->method('save')
280
//                                ->with();
281
//
282
//                            return $cacheItemMock;
283
//                        }
284
//                    )
285
//                );
286
//        }
287
288
        $cacheItemMock
289
            ->expects($this->once())
290
            ->method('set')
291
            ->with($testGroups)
292
            ->will($this->returnValue($cacheItemMock));
293
294
        $cacheItemMock
295
            ->expects($this->once())
296
            ->method('save')
297
            ->with();
298
299
        $handler = $this->persistenceCacheHandler->objectStateHandler();
300
        $groups = $handler->loadAllGroups($offset, $limit);
301
302
        $expectedGroups = array_slice($testGroups, $offset, $limit > -1 ?: null);
303
        $this->assertEquals($groups, $expectedGroups);
304
    }
305
306
    /**
307
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadAllGroups
308
     */
309
    public function testLoadAllGroupsCached($offset = 0, $limit = -1)
310
    {
311
        $testGroups = $this->generateObjectGroupsArray($offset, $limit);
312
313
        $cacheItemMock = $this->getCacheItemMock();
314
        $this->cacheMock
315
            ->expects($this->at(0))
316
            ->method('getItem')
317
            ->with('objectstategroup', 'all')
318
            ->will($this->returnValue($cacheItemMock));
319
        $cacheItemMock
320
            ->expects($this->once())
321
            ->method('get')
322
            ->will($this->returnValue($testGroups));
323
        $cacheItemMock
324
            ->expects($this->once())
325
            ->method('isMiss')
326
            ->will($this->returnValue(false));
327
328
        $expectedGroups = array_slice($testGroups, $offset, $limit > -1 ?: null);
329
330
        $handler = $this->persistenceCacheHandler->objectStateHandler();
331
        $groups = $handler->loadAllGroups($offset, $limit);
332
        $this->assertEquals($groups, $expectedGroups);
333
    }
334
335
    /**
336
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadAllGroups
337
     */
338
    public function testLoadAllGroupsWithOffsetLimit()
339
    {
340
        $this->testLoadAllGroups(7, 5);
341
    }
342
343
    /**
344
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadAllGroups
345
     */
346
    public function testLoadAllGroupsCachedWithOffsetLimit()
347
    {
348
        $this->testLoadAllGroupsCached(7, 5);
349
    }
350
351
    /**
352
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadObjectStates
353
     */
354
    public function testLoadObjectStates()
355
    {
356
        $testStates = array(
357
            new SPIObjectState(
358
                array(
359
                    'id' => 1,
360
                    'identifier' => 'test_state_1_group1',
361
                    'groupId' => 1,
362
                )
363
            ),
364
            new SPIObjectState(
365
                array(
366
                    'id' => 2,
367
                    'identifier' => 'test_state_2_group1',
368
                    'groupId' => 1,
369
                )
370
            ),
371
            new SPIObjectState(
372
                array(
373
                    'id' => 3,
374
                    'identifier' => 'test_state_3_group1',
375
                    'groupId' => 1,
376
                )
377
            ),
378
        );
379
380
        $cacheItemMock = $this->getCacheItemMock();
381
        $this->cacheMock
382
            ->expects($this->at(0))
383
            ->method('getItem')
384
            ->with('objectstate', 'byGroup', 1)
385
            ->will($this->returnValue($cacheItemMock));
386
        $cacheItemMock
387
            ->expects($this->once())
388
            ->method('get')
389
            ->will($this->returnValue(null));
390
        $cacheItemMock
391
            ->expects($this->once())
392
            ->method('isMiss')
393
            ->will($this->returnValue(true));
394
395
        $this->loggerMock->expects($this->once())->method('logCall');
396
397
        $innerHandlerMock = $this->getSPIObjectStateHandler();
398
        $this->persistenceHandlerMock
399
            ->expects($this->once())
400
            ->method('objectStateHandler')
401
            ->will($this->returnValue($innerHandlerMock));
402
403
        $innerHandlerMock
404
            ->expects($this->once())
405
            ->method('loadObjectStates')
406
            ->with(1)
407
            ->will($this->returnValue($testStates));
408
409
        $cacheItemMock
410
            ->expects($this->once())
411
            ->method('set')
412
            ->with($testStates)
413
            ->will($this->returnValue($cacheItemMock));
414
415
        $cacheItemMock
416
            ->expects($this->once())
417
            ->method('save')
418
            ->with();
419
420
        $handler = $this->persistenceCacheHandler->objectStateHandler();
421
        $states = $handler->loadObjectStates(1);
422
        $this->assertEquals($states, $testStates);
423
    }
424
425
    /**
426
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadObjectStates
427
     */
428
    public function testLoadObjectStatesCached()
429
    {
430
        $testStates = array(
431
            new SPIObjectState(
432
                array(
433
                    'id' => 1,
434
                    'identifier' => 'test_state_1_group1',
435
                    'groupId' => 1,
436
                )
437
            ),
438
            new SPIObjectState(
439
                array(
440
                    'id' => 2,
441
                    'identifier' => 'test_state_2_group1',
442
                    'groupId' => 1,
443
                )
444
            ),
445
            new SPIObjectState(
446
                array(
447
                    'id' => 3,
448
                    'identifier' => 'test_state_3_group1',
449
                    'groupId' => 1,
450
                )
451
            ),
452
        );
453
454
        $cacheItemMock = $this->getCacheItemMock();
455
        $this->cacheMock
456
            ->expects($this->at(0))
457
            ->method('getItem')
458
            ->with('objectstate', 'byGroup', 1)
459
            ->will($this->returnValue($cacheItemMock));
460
        $cacheItemMock
461
            ->expects($this->once())
462
            ->method('get')
463
            ->will($this->returnValue($testStates));
464
        $cacheItemMock
465
            ->expects($this->once())
466
            ->method('isMiss')
467
            ->will($this->returnValue(false));
468
469
        $handler = $this->persistenceCacheHandler->objectStateHandler();
470
        $states = $handler->loadObjectStates(1);
471
        $this->assertEquals($states, $testStates);
472
    }
473
474
    /**
475
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::updateGroup
476
     */
477
    public function testUpdateGroup()
478
    {
479
        $inputStruct = new SPIInputStruct(
480
            array('identifier' => 'test_state_group', 'name' => 'Test State Group (New)')
481
        );
482
        $expectedGroup = new SPIObjectStateGroup(
483
            array(
484
                'id' => 1,
485
                'identifier' => 'test_state_group_new',
486
                'name' => 'Test State Group (New)',
487
            )
488
        );
489
490
        $this->loggerMock->expects($this->once())->method('logCall');
491
492
        $innerHandlerMock = $this->getSPIObjectStateHandler();
493
        $this->persistenceHandlerMock
494
            ->expects($this->once())
495
            ->method('objectStateHandler')
496
            ->will($this->returnValue($innerHandlerMock));
497
498
        $innerHandlerMock
499
            ->expects($this->once())
500
            ->method('updateGroup')
501
            ->with(1, $inputStruct)
502
            ->will($this->returnValue($expectedGroup));
503
504
        $cacheItemMock = $this->getCacheItemMock();
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...
505
        $this->cacheMock
506
            ->expects($this->at(0))
507
            ->method('clear')
508
            ->with('objectstategroup', 1);
509
510
        $handler = $this->persistenceCacheHandler->objectStateHandler();
511
        $newGroup = $handler->updateGroup(1, $inputStruct);
512
        $this->assertEquals($newGroup, $expectedGroup);
513
    }
514
515
    /**
516
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::deleteGroup
517
     */
518
    public function testDeleteGroup()
519
    {
520
        $this->loggerMock->expects($this->once())->method('logCall');
521
522
        $innerHandlerMock = $this->getSPIObjectStateHandler();
523
        $this->persistenceHandlerMock
524
            ->expects($this->once())
525
            ->method('objectStateHandler')
526
            ->will($this->returnValue($innerHandlerMock));
527
528
        $innerHandlerMock
529
            ->expects($this->once())
530
            ->method('deleteGroup')
531
            ->with(1);
532
533
        $cacheItemMock = $this->getCacheItemMock();
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...
534
        $this->cacheMock
535
            ->expects($this->at(0))
536
            ->method('clear')
537
            ->with('objectstategroup', 'all');
538
        $this->cacheMock
539
            ->expects($this->at(1))
540
            ->method('clear')
541
            ->with('objectstategroup', 1);
542
        $this->cacheMock
543
            ->expects($this->at(2))
544
            ->method('clear')
545
            ->with('objectstate', 'byGroup', 1);
546
547
        $handler = $this->persistenceCacheHandler->objectStateHandler();
548
        $handler->deleteGroup(1);
549
    }
550
551
    /**
552
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::create
553
     */
554
    public function testCreate()
555
    {
556
        $inputStruct = new SPIInputStruct(
557
            array('identifier' => 'test_state', 'name' => 'Test State')
558
        );
559
        $expectedState = new SPIObjectState(
560
            array(
561
                'id' => 1,
562
                'identifier' => 'test_state',
563
                'name' => 'Test State',
564
                'groupId' => 1,
565
            )
566
        );
567
568
        $this->loggerMock->expects($this->once())->method('logCall');
569
570
        $innerHandlerMock = $this->getSPIObjectStateHandler();
571
        $this->persistenceHandlerMock
572
            ->expects($this->once())
573
            ->method('objectStateHandler')
574
            ->will($this->returnValue($innerHandlerMock));
575
576
        $innerHandlerMock
577
            ->expects($this->once())
578
            ->method('create')
579
            ->with(1, $inputStruct)
580
            ->will($this->returnValue($expectedState));
581
582
        $cacheItemMock = $this->getCacheItemMock();
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...
583
        $this->cacheMock
584
            ->expects($this->at(0))
585
            ->method('clear')
586
            ->with('objectstate', 'byGroup', 1);
587
588
        $handler = $this->persistenceCacheHandler->objectStateHandler();
589
        $state = $handler->create(1, $inputStruct);
590
        $this->assertEquals($state, $expectedState);
591
    }
592
593
    /**
594
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::load
595
     */
596 View Code Duplication
    public function testLoad()
597
    {
598
        $expectedState = new SPIObjectState(
599
            array(
600
                'id' => 1,
601
                'identifier' => 'test_state',
602
                'name' => 'Test State',
603
                'groupId' => 1,
604
            )
605
        );
606
607
        $cacheItemMock = $this->getCacheItemMock();
608
        $this->cacheMock
609
            ->expects($this->once())
610
            ->method('getItem')
611
            ->with('objectstate', 1)
612
            ->will($this->returnValue($cacheItemMock));
613
        $cacheItemMock
614
            ->expects($this->once())
615
            ->method('get')
616
            ->will($this->returnValue(null));
617
        $cacheItemMock
618
            ->expects($this->once())
619
            ->method('isMiss')
620
            ->will($this->returnValue(true));
621
        $cacheItemMock
622
            ->expects($this->once())
623
            ->method('set')
624
            ->with($this->isInstanceOf(SPIObjectState::class))
625
            ->will($this->returnValue($cacheItemMock));
626
        $cacheItemMock
627
            ->expects($this->once())
628
            ->method('save')
629
            ->with();
630
631
        $this->loggerMock->expects($this->once())->method('logCall');
632
633
        $innerHandlerMock = $this->getSPIObjectStateHandler();
634
        $this->persistenceHandlerMock
635
            ->expects($this->once())
636
            ->method('objectStateHandler')
637
            ->will($this->returnValue($innerHandlerMock));
638
639
        $innerHandlerMock
640
            ->expects($this->once())
641
            ->method('load')
642
            ->with(1)
643
            ->will($this->returnValue($expectedState));
644
645
        $handler = $this->persistenceCacheHandler->objectStateHandler();
646
        $state = $handler->load(1);
647
        $this->assertEquals($state, $expectedState);
648
    }
649
650
    /**
651
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::load
652
     * @depends testLoad
653
     */
654 View Code Duplication
    public function testLoadCached()
655
    {
656
        $expectedState = new SPIObjectState(
657
            array(
658
                'id' => 1,
659
                'identifier' => 'test_state',
660
                'name' => 'Test State',
661
                'groupId' => 1,
662
            )
663
        );
664
665
        $cacheItemMock = $this->getCacheItemMock();
666
        $this->cacheMock
667
            ->expects($this->once())
668
            ->method('getItem')
669
            ->with('objectstate', 1)
670
            ->will($this->returnValue($cacheItemMock));
671
        $cacheItemMock
672
            ->expects($this->once())
673
            ->method('get')
674
            ->will($this->returnValue($expectedState));
675
676
        $handler = $this->persistenceCacheHandler->objectStateHandler();
677
        $state = $handler->load(1);
678
        $this->assertEquals($state, $expectedState);
679
    }
680
681
    /**
682
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::loadByIdentifier
683
     */
684
    public function testLoadByIdentifier()
685
    {
686
        $expectedState = new SPIObjectState(
687
            array(
688
                'id' => 1,
689
                'identifier' => 'test_state',
690
                'name' => 'Test State',
691
                'groupId' => 1,
692
            )
693
        );
694
695
        $this->loggerMock->expects($this->once())->method('logCall');
696
697
        $innerHandlerMock = $this->getSPIObjectStateHandler();
698
        $this->persistenceHandlerMock
699
            ->expects($this->once())
700
            ->method('objectStateHandler')
701
            ->will($this->returnValue($innerHandlerMock));
702
703
        $innerHandlerMock
704
            ->expects($this->once())
705
            ->method('loadByIdentifier')
706
            ->with($expectedState->identifier, $expectedState->groupId)
707
            ->will($this->returnValue($expectedState));
708
709
        $handler = $this->persistenceCacheHandler->objectStateHandler();
710
        $state = $handler->loadByIdentifier($expectedState->identifier, $expectedState->groupId);
711
        $this->assertEquals($state, $expectedState);
712
    }
713
714
    /**
715
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::update
716
     */
717
    public function testUpdate()
718
    {
719
        $inputStruct = new SPIInputStruct(
720
            array('identifier' => 'test_state_new', 'name' => 'Test State (new)')
721
        );
722
723
        $expectedState = new SPIObjectState(
724
            array(
725
                'id' => 1,
726
                'identifier' => 'test_state',
727
                'name' => 'Test State',
728
                'groupId' => 1,
729
            )
730
        );
731
732
        $this->loggerMock->expects($this->once())->method('logCall');
733
734
        $innerHandlerMock = $this->getSPIObjectStateHandler();
735
        $this->persistenceHandlerMock
736
            ->expects($this->once())
737
            ->method('objectStateHandler')
738
            ->will($this->returnValue($innerHandlerMock));
739
740
        $innerHandlerMock
741
            ->expects($this->once())
742
            ->method('update')
743
            ->with($expectedState->id, $inputStruct)
744
            ->will($this->returnValue($expectedState));
745
746
        $cacheItemMock = $this->getCacheItemMock();
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...
747
        $this->cacheMock
748
            ->expects($this->at(0))
749
            ->method('clear')
750
            ->with('objectstate', $expectedState->id);
751
752
        $handler = $this->persistenceCacheHandler->objectStateHandler();
753
        $state = $handler->update($expectedState->id, $inputStruct);
754
        $this->assertEquals($state, $expectedState);
755
    }
756
757
    /**
758
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::setPriority
759
     */
760
    public function testSetPriority()
761
    {
762
        $expectedState = new SPIObjectState(
763
            array(
764
                'id' => 1,
765
                'identifier' => 'test_state',
766
                'name' => 'Test State',
767
                'groupId' => 1,
768
                'priority' => 1,
769
            )
770
        );
771
772
        $this->loggerMock->expects($this->once())->method('logCall');
773
774
        $innerHandlerMock = $this->getSPIObjectStateHandler();
775
        $this->persistenceHandlerMock
776
            ->expects($this->once())
777
            ->method('objectStateHandler')
778
            ->will($this->returnValue($innerHandlerMock));
779
780
        $innerHandlerMock
781
            ->expects($this->once())
782
            ->method('setPriority')
783
            ->with($expectedState->id, 1)
784
            ->will($this->returnValue($expectedState));
785
786
        $cacheItemMock = $this->getCacheItemMock();
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...
787
        $this->cacheMock
788
            ->expects($this->at(0))
789
            ->method('clear')
790
            ->with('objectstate', $expectedState->id);
791
792
        $handler = $this->persistenceCacheHandler->objectStateHandler();
793
        $state = $handler->setPriority($expectedState->id, 1);
794
        $this->assertEquals($state, $expectedState);
795
    }
796
797
    /**
798
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::delete
799
     */
800
    public function testDelete()
801
    {
802
        $this->loggerMock->expects($this->once())->method('logCall');
803
804
        $innerHandlerMock = $this->getSPIObjectStateHandler();
805
        $this->persistenceHandlerMock
806
            ->expects($this->once())
807
            ->method('objectStateHandler')
808
            ->will($this->returnValue($innerHandlerMock));
809
810
        $innerHandlerMock
811
            ->expects($this->once())
812
            ->method('delete')
813
            ->with(1);
814
815
        $cacheItemMock = $this->getCacheItemMock();
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...
816
        $this->cacheMock
817
            ->expects($this->at(0))
818
            ->method('clear')
819
            ->with('objectstate', 1);
820
        $this->cacheMock
821
            ->expects($this->at(1))
822
            ->method('clear')
823
            ->with('objectstate', 'byGroup');
824
825
        $handler = $this->persistenceCacheHandler->objectStateHandler();
826
        $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...
827
    }
828
829
    /**
830
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::setContentState
831
     */
832
    public function testSetContentState()
833
    {
834
        $this->loggerMock->expects($this->once())->method('logCall');
835
836
        $innerHandlerMock = $this->getSPIObjectStateHandler();
837
        $this->persistenceHandlerMock
838
            ->expects($this->once())
839
            ->method('objectStateHandler')
840
            ->will($this->returnValue($innerHandlerMock));
841
842
        $innerHandlerMock
843
            ->expects($this->once())
844
            ->method('setContentState')
845
            ->with(10, 1, 2);
846
847
        $cacheItemMock = $this->getCacheItemMock();
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...
848
        $this->cacheMock
849
            ->expects($this->at(0))
850
            ->method('clear')
851
            ->with('objectstate', 'byContent', 10, 1);
852
853
        $handler = $this->persistenceCacheHandler->objectStateHandler();
854
        $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...
855
    }
856
857
    /**
858
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentState
859
     */
860
    public function testGetContentState()
861
    {
862
        $expectedState = new SPIObjectState(
863
            array(
864
                'id' => 1,
865
                'identifier' => 'test_state',
866
                'name' => 'Test State',
867
                'groupId' => 1,
868
                'priority' => 1,
869
            )
870
        );
871
872
        $cacheItemMock = $this->getCacheItemMock();
873
        $this->cacheMock
874
            ->expects($this->at(0))
875
            ->method('getItem')
876
            ->with('objectstate', 'byContent', 10, 1)
877
            ->will($this->returnValue($cacheItemMock));
878
        $cacheItemMock
879
            ->expects($this->once())
880
            ->method('get')
881
            ->will($this->returnValue(null));
882
        $cacheItemMock
883
            ->expects($this->once())
884
            ->method('isMiss')
885
            ->will($this->returnValue(true));
886
        $cacheItemMock
887
            ->expects($this->once())
888
            ->method('set')
889
            ->with(1)
890
            ->will($this->returnValue($cacheItemMock));
891
        $cacheItemMock
892
            ->expects($this->once())
893
            ->method('save')
894
            ->with();
895
896
        $this->loggerMock->expects($this->once())->method('logCall');
897
898
        $innerHandlerMock = $this->getSPIObjectStateHandler();
899
        $this->persistenceHandlerMock
900
            ->expects($this->once())
901
            ->method('objectStateHandler')
902
            ->will($this->returnValue($innerHandlerMock));
903
904
        $innerHandlerMock
905
            ->expects($this->once())
906
            ->method('getContentState')
907
            ->with(10, 1)
908
            ->will($this->returnValue($expectedState));
909
910
        $handler = $this->persistenceCacheHandler->objectStateHandler();
911
        $state = $handler->getContentState(10, 1, 2);
912
        $this->assertEquals($state, $expectedState);
913
    }
914
915
    /**
916
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentState
917
     */
918
    public function testGetContentStateCached()
919
    {
920
        $expectedState = new SPIObjectState(
921
            array(
922
                'id' => 1,
923
                'identifier' => 'test_state',
924
                'name' => 'Test State',
925
                'groupId' => 1,
926
            )
927
        );
928
929
        $cacheItemMock = $this->getCacheItemMock();
930
        $this->cacheMock
931
            ->expects($this->at(0))
932
            ->method('getItem')
933
            ->with('objectstate', 'byContent', 10, 1)
934
            ->will($this->returnValue($cacheItemMock));
935
        $cacheItemMock
936
            ->expects($this->once())
937
            ->method('get')
938
            ->will($this->returnValue(1));
939
        $cacheItemMock
940
            ->expects($this->once())
941
            ->method('isMiss')
942
            ->will($this->returnValue(false));
943
944
        // load()
945
        $this->cacheMock
946
            ->expects($this->at(1))
947
            ->method('getItem')
948
            ->with('objectstate', $expectedState->id)
949
            ->will(
950
                $this->returnCallback(
951
                    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...
952
                        $cacheItemMock = $this->getCacheItemMock();
953
                        $cacheItemMock
954
                            ->expects($this->once())
955
                            ->method('get')
956
                            ->will($this->returnValue($expectedState));
957
958
                        return $cacheItemMock;
959
                    }
960
                )
961
            );
962
963
        $handler = $this->persistenceCacheHandler->objectStateHandler();
964
        $state = $handler->getContentState(10, 1);
965
        $this->assertEquals($state, $expectedState);
966
    }
967
968
    /**
969
     * @covers \eZ\Publish\SPI\Persistence\Content\ObjectState\Handler::getContentCount
970
     */
971
    public function testGetContentCount()
972
    {
973
        $expectedCount = 2;
974
975
        $this->loggerMock->expects($this->once())->method('logCall');
976
977
        $innerHandlerMock = $this->getSPIObjectStateHandler();
978
        $this->persistenceHandlerMock
979
            ->expects($this->once())
980
            ->method('objectStateHandler')
981
            ->will($this->returnValue($innerHandlerMock));
982
983
        $innerHandlerMock
984
            ->expects($this->once())
985
            ->method('getContentCount')
986
            ->with(1)
987
            ->will($this->returnValue($expectedCount));
988
989
        //$this->logger->logCall( __METHOD__, array( 'stateId' => $stateId ) );
990
991
        $handler = $this->persistenceCacheHandler->objectStateHandler();
992
        $count = $handler->getContentCount(1);
993
        $this->assertEquals($count, $expectedCount);
994
    }
995
}
996