Completed
Push — in-memory-cache ( 3245f0 )
by André
18:20
created

InMemoryCacheAdapterTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 9
Ratio 34.62 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 9
loc 26
rs 9.504
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 View Code Duplication
            function ($key, $value, $isHit, $defaultLifetime = 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
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
    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 = $this->cache->getItems(['first_key', 'second_key']);
108
        $this->assertSame($items, $returnedItems);
109
110
        $returnedItems = $this->cache->getItems(['first_key', 'second_key']);
111
        $this->assertSame($items, $returnedItems);
112
    }
113
114
    /**
115
     * @param $key
116
     * @param null $value If null the cache item will be assumed to be a cache miss here.
117
     * @param int $defaultLifetime
118
     *
119
     * @return CacheItem
120
     */
121
    final protected function getCacheItem($key, $value = null, $defaultLifetime = 0)
122
    {
123
        $cacheItemsClosure = $this->cacheItemsClosure;
124
125
        return $cacheItemsClosure($key, $value, (bool)$value, $defaultLifetime);
126
    }
127
}
128