Completed
Push — master ( 955b04...f00653 )
by André
38:12 queued 16:07
created

InMemoryCacheTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
rs 9.9666
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\Tests\InMemory;
10
11
use eZ\Publish\Core\Persistence\Cache\InMemory\InMemoryCache;
12
use PHPUnit\Framework\TestCase;
13
14
/**
15
 * Abstract test case for spi cache impl.
16
 */
17
class InMemoryCacheTest extends TestCase
18
{
19
    /**
20
     * @var \eZ\Publish\Core\Persistence\Cache\InMemory\InMemoryCache
21
     */
22
    protected $cache;
23
24
    /**
25
     * Setup Test.
26
     */
27
    final protected function setUp()
28
    {
29
        parent::setUp();
30
31
        $this->cache = new InMemoryCache(
32
            3000,
33
            8
34
        );
35
    }
36
37
    /**
38
     * Tear down test (properties).
39
     */
40
    final protected function tearDown()
41
    {
42
        $this->cache->clear();
43
44
        unset($this->cache);
45
        unset($GLOBALS['override_time']);
46
        parent::tearDown();
47
    }
48
49
    public function testGetByKey(): void
50
    {
51
        $this->assertNull($this->cache->get('first'));
52
53
        $obj = new \stdClass();
54
        $this->cache->setMulti([$obj], static function ($o) { return ['first']; });
0 ignored issues
show
Unused Code introduced by
The parameter $o 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...
55
56
        $this->assertSame($obj, $this->cache->get('first'));
57
58
        // Test TTL
59
        $GLOBALS['override_time'] = \microtime(true) + 4;
60
        $this->assertNull($this->cache->get('first'));
61
    }
62
63 View Code Duplication
    public function testGetBySecondaryIndex(): void
64
    {
65
        $this->assertNull($this->cache->get('first'));
66
        $this->assertNull($this->cache->get('secondary'));
67
68
        $obj = new \stdClass();
69
        $this->cache->setMulti([$obj], static function ($o) { return ['first', 'secondary']; });
0 ignored issues
show
Unused Code introduced by
The parameter $o 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...
70
71
        $this->assertSame($obj, $this->cache->get('first'));
72
        $this->assertSame($obj, $this->cache->get('secondary'));
73
74
        // Test TTL
75
        $GLOBALS['override_time'] = \microtime(true) + 4;
76
        $this->assertNull($this->cache->get('first'));
77
        $this->assertNull($this->cache->get('secondary'));
78
    }
79
80 View Code Duplication
    public function testGetByList(): void
81
    {
82
        $this->assertNull($this->cache->get('first'));
83
        $this->assertNull($this->cache->get('list'));
84
85
        $obj = new \stdClass();
86
        $this->cache->setMulti([$obj], static function ($o) { return ['first']; }, 'list');
0 ignored issues
show
Unused Code introduced by
The parameter $o 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...
87
88
        $this->assertSame($obj, $this->cache->get('first'));
89
        $this->assertSame([$obj], $this->cache->get('list'));
90
91
        // Test TTL
92
        $GLOBALS['override_time'] = \microtime(true) + 4;
93
        $this->assertNull($this->cache->get('first'));
94
        $this->assertNull($this->cache->get('list'));
95
    }
96
97
    public function testDeleted(): void
98
    {
99
        $obj = new \stdClass();
100
        $this->cache->setMulti([$obj], static function ($o) { return ['first', 'second']; }, 'list');
0 ignored issues
show
Unused Code introduced by
The parameter $o 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...
101
102
        $this->assertSame($obj, $this->cache->get('first'));
103
        $this->assertSame($obj, $this->cache->get('second'));
104
        $this->assertSame([$obj], $this->cache->get('list'));
105
106
        // Delete primary, her we expect secondary index to also start returning null
107
        $this->cache->deleteMulti(['first']);
108
109
        $this->assertNull($this->cache->get('first'));
110
        $this->assertNull($this->cache->get('second'));
111
        $this->assertSame([$obj], $this->cache->get('list'));
112
113
        // Delete list
114
        $this->cache->deleteMulti(['list']);
115
116
        $this->assertNull($this->cache->get('list'));
117
    }
118
119
    public function testClear(): void
120
    {
121
        $obj = new \stdClass();
122
        $this->cache->setMulti([$obj], static function ($o) { return ['first', 'second']; }, 'list');
0 ignored issues
show
Unused Code introduced by
The parameter $o 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...
123
124
        $this->assertSame($obj, $this->cache->get('first'));
125
        $this->assertSame($obj, $this->cache->get('second'));
126
        $this->assertSame([$obj], $this->cache->get('list'));
127
128
        // Clear all cache
129
        $this->cache->clear();
130
131
        $this->assertNull($this->cache->get('first'));
132
        $this->assertNull($this->cache->get('second'));
133
        $this->assertNull($this->cache->get('list'));
134
    }
135
136
    public function testSetWhenReachingSetLimit(): void
137
    {
138
        $obj = new \stdClass();
139
        $this->cache->setMulti([$obj, $obj], static function ($o) { return ['first', 'second']; }, 'list');
0 ignored issues
show
Unused Code introduced by
The parameter $o 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...
140
141
        $this->assertNull($this->cache->get('first'));
142
        $this->assertNull($this->cache->get('second'));
143
        $this->assertNull($this->cache->get('list'));
144
    }
145
146
    public function testSetWhenReachingTotalLimit(): void
147
    {
148
        $obj = new \stdClass();
149
        $this->cache->setMulti([$obj], static function ($o) { return ['first']; });
0 ignored issues
show
Unused Code introduced by
The parameter $o 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...
150
        $this->cache->setMulti([$obj], static function ($o) { return ['second']; });
0 ignored issues
show
Unused Code introduced by
The parameter $o 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...
151
        $this->cache->setMulti([$obj], static function ($o) { return ['third']; });
0 ignored issues
show
Unused Code introduced by
The parameter $o 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...
152
        $this->cache->setMulti([$obj], static function ($o) { return ['fourth']; });
0 ignored issues
show
Unused Code introduced by
The parameter $o 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...
153
        $this->cache->setMulti([$obj], static function ($o) { return ['fifth']; });
0 ignored issues
show
Unused Code introduced by
The parameter $o 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...
154
        $this->cache->setMulti([$obj], static function ($o) { return ['sixth']; });
0 ignored issues
show
Unused Code introduced by
The parameter $o 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...
155
        $this->cache->setMulti([$obj], static function ($o) { return ['seventh']; });
0 ignored issues
show
Unused Code introduced by
The parameter $o 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...
156
        $this->cache->setMulti([$obj], static function ($o) { return ['eight']; });
0 ignored issues
show
Unused Code introduced by
The parameter $o 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...
157
158
        $this->assertNull($this->cache->get('first'));
159
        $this->assertNull($this->cache->get('second'));
160
        $this->assertSame($obj, $this->cache->get('third'));
161
        $this->assertSame($obj, $this->cache->get('fourth'));
162
        $this->assertSame($obj, $this->cache->get('fifth'));
163
        $this->assertSame($obj, $this->cache->get('sixth'));
164
        $this->assertSame($obj, $this->cache->get('seventh'));
165
        $this->assertSame($obj, $this->cache->get('eight'));
166
    }
167
}
168
169
namespace eZ\Publish\Core\Persistence\Cache\InMemory;
170
171
/**
172
 * Overloads microtime(true) calls in InMemoryCache in order to be able to test expiry.
173
 *
174
 * @return float|string
175
 */
176
function microtime($asFloat = false)
177
{
178
    if ($asFloat & isset($GLOBALS['override_time'])) {
179
        return $GLOBALS['override_time'];
180
    }
181
182
    return \microtime($asFloat);
183
}
184