Completed
Push — state_lookup_optimizations ( 3185f4...e6c717 )
by André
13:19
created

testLoadObjectStatesCached()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 45
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

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