Completed
Push — in-memory-cache ( 3245f0...8b8156 )
by André
20:40
created

testGetItemsWithGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 19
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Persistence\Cache\Adapter\Tests;
10
11
use eZ\Publish\Core\Persistence\Cache\Adapter\InMemoryCacheAdapter;
12
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
13
use Symfony\Component\Cache\CacheItem;
14
use PHPUnit\Framework\TestCase;
15
16
/**
17
 * Abstract test case for spi cache impl.
18
 */
19
class InMemoryCacheAdapterTest extends TestCase
20
{
21
    /**
22
     * @var \eZ\Publish\Core\Persistence\Cache\Adapter\InMemoryCacheAdapter
23
     */
24
    protected $cache;
25
26
    /**
27
     * @var \Symfony\Component\Cache\Adapter\TagAwareAdapterInterface|\PHPUnit\Framework\MockObject\MockObject
28
     */
29
    protected $cacheMock;
30
31
    /**
32
     * @var \Closure
33
     */
34
    private $cacheItemsClosure;
35
36
    /**
37
     * Setup the HandlerTest.
38
     */
39
    final protected function setUp()
40
    {
41
        parent::setUp();
42
43
        $this->cacheMock = $this->createMock(TagAwareAdapterInterface::class);
44
45
        $this->cache = new InMemoryCacheAdapter(
46
            $this->cacheMock,
47
            20,
48
            3
49
        );
50
51
        $this->cacheItemsClosure = \Closure::bind(
52
            function ($key, $value, $isHit, $defaultLifetime = 0) {
53
                $item = new CacheItem();
54
                $item->key = $key;
0 ignored issues
show
Bug introduced by
The property key cannot be accessed from this context as it is declared protected in class Symfony\Component\Cache\CacheItem.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
55
                $item->value = $value;
0 ignored issues
show
Bug introduced by
The property value cannot be accessed from this context as it is declared protected in class Symfony\Component\Cache\CacheItem.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
56
                $item->isHit = $isHit;
0 ignored issues
show
Bug introduced by
The property isHit cannot be accessed from this context as it is declared protected in class Symfony\Component\Cache\CacheItem.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
57
                $item->defaultLifetime = $defaultLifetime;
0 ignored issues
show
Bug introduced by
The property defaultLifetime cannot be accessed from this context as it is declared protected in class Symfony\Component\Cache\CacheItem.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
58
59
                return $item;
60
            },
61
            null,
62
            CacheItem::class
63
        );
64
    }
65
66
    /**
67
     * Tear down test (properties).
68
     */
69
    final protected function tearDown()
70
    {
71
        unset($this->cache);
72
        unset($this->cacheMock);
73
        unset($this->cacheItemsClosure);
74
        parent::tearDown();
75
    }
76
77
    public function testGetItemOnlyCalledOnce()
78
    {
79
        $item = $this->getCacheItem('some_key', true);
80
81
        $this->cacheMock
82
            ->expects($this->once())
83
            ->method('getItem')
84
            ->with('some_key')
85
            ->willReturn($item);
86
87
        $returnedItem = $this->cache->getItem('some_key');
88
        $this->assertSame($item, $returnedItem);
89
90
        $returnedItem = $this->cache->getItem('some_key');
91
        $this->assertSame($item, $returnedItem);
92
    }
93
94 View Code Duplication
    public function testGetItemsOnlyCalledOnce()
95
    {
96
        $items = [
97
            'first_key' => $this->getCacheItem('first_key', true),
98
            'second_key' => $this->getCacheItem('second_key', true),
99
        ];
100
101
        $this->cacheMock
102
            ->expects($this->once())
103
            ->method('getItems')
104
            ->with(['first_key', 'second_key'])
105
            ->willReturn($items);
106
107
        $returnedItems = iterator_to_array($this->cache->getItems(['first_key', 'second_key']));
108
        $this->assertSame($items, $returnedItems);
109
110
        $returnedItems = iterator_to_array($this->cache->getItems(['first_key', 'second_key']));
111
        $this->assertSame($items, $returnedItems);
112
    }
113
114
    /**
115
     * Symfony uses generators with getItems() so we need to make sure we handle that.
116
     */
117 View Code Duplication
    public function testGetItemsWithGenerator()
118
    {
119
        $items = [
120
            'first_key' => $this->getCacheItem('first_key', true),
121
            'second_key' => $this->getCacheItem('second_key', true),
122
        ];
123
124
        $this->cacheMock
125
            ->expects($this->once())
126
            ->method('getItems')
127
            ->with(['first_key', 'second_key'])
128
            ->willReturn($this->arrayAsGenerator($items));
129
130
        $returnedItems = iterator_to_array($this->cache->getItems(['first_key', 'second_key']));
131
        $this->assertSame($items, $returnedItems);
132
133
        $returnedItems = iterator_to_array($this->cache->getItems(['first_key', 'second_key']));
134
        $this->assertSame($items, $returnedItems);
135
    }
136
137
    /**
138
     * @param $key
139
     * @param null $value If null the cache item will be assumed to be a cache miss here.
140
     * @param int $defaultLifetime
141
     *
142
     * @return CacheItem
143
     */
144
    final protected function getCacheItem($key, $value = null, $defaultLifetime = 0)
145
    {
146
        $cacheItemsClosure = $this->cacheItemsClosure;
147
148
        return $cacheItemsClosure($key, $value, (bool)$value, $defaultLifetime);
149
    }
150
151
    final protected function arrayAsGenerator(array $array)
152
    {
153
        foreach ($array as $key => $item) {
154
            yield $key => $item;
155
        }
156
    }
157
}
158