File: EagerTest.php

package info (click to toggle)
matomo-component-cache 2.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 396 kB
  • sloc: php: 1,433; makefile: 12
file content (190 lines) | stat: -rw-r--r-- 5,553 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL v3 or later
 */

namespace Tests\Matomo\Cache;

use Matomo\Cache\Backend\ArrayCache;
use Matomo\Cache\Eager;
use Matomo\Cache\Backend;
use PHPUnit\Framework\TestCase;

/**
 * @covers \Matomo\Cache\Eager
 */
class EagerTest extends TestCase
{
    /**
     * @var Eager
     */
    private $cache;

    /**
     * @var Backend
     */
    private $backend;

    private $storageId  = 'eagercache';
    private $cacheId    = 'testid';
    private $cacheValue = 'exampleValue';

    protected function setUp()
    {
        $this->backend = new ArrayCache();
        $this->backend->doSave($this->storageId, array($this->cacheId => $this->cacheValue));

        $this->cache = new Eager($this->backend, $this->storageId);
    }

    public function test_contains_shouldReturnFalse_IfNoSuchCacheIdExists()
    {
        $this->assertFalse($this->cache->contains('randomid'));
    }

    public function test_contains_shouldReturnTrue_IfSuchCacheIdExists()
    {
        $this->assertTrue($this->cache->contains($this->cacheId));
    }

    public function test_fetch_shouldReturnTheCachedValue_IfCacheIdExists()
    {
        $this->assertEquals($this->cacheValue, $this->cache->fetch($this->cacheId));
    }

    public function test_save_shouldOverwriteAnyValue_IfCacheIdAlreadyExists()
    {
        $this->assertHasCacheEntry($this->cacheId);

        $value = 'anyotherValuE';
        $this->cache->save($this->cacheId, $value);

        $this->assertSame($value, $this->cache->fetch($this->cacheId));
    }

    public function test_save_shouldBeAbleToSetArrays()
    {
        $value = array('anyotherE' => 'anyOtherValUE', 1 => array(2));
        $this->cache->save($this->cacheId, $value);

        $this->assertSame($value, $this->cache->fetch($this->cacheId));
    }

    /**
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage cannot use this cache to cache an object
     */
    public function test_save_shouldFail_IfTryingToSetAnObject()
    {
        $value = (object) array('anyotherE' => 'anyOtherValUE', 1 => array(2));
        $this->cache->save($this->cacheId, $value);

        $this->assertSame($value, $this->cache->fetch($this->cacheId));
    }

    public function test_save_shouldBeAbleToSetNumbers()
    {
        $value = 5.4;
        $this->cache->save($this->cacheId, $value);

        $this->assertSame($value, $this->cache->fetch($this->cacheId));
    }

    public function test_delete_shouldReturnTrue_OnSuccess()
    {
        $this->assertTrue($this->cache->delete($this->cacheId));
    }

    public function test_delete_shouldReturnFalse_IfCacheIdDoesNotExist()
    {
        $this->assertFalse($this->cache->delete('IdoNotExisT'));
    }

    public function test_delete_shouldActuallyDeleteCacheId()
    {
        $this->assertHasCacheEntry($this->cacheId);

        $this->cache->delete($this->cacheId);

        $this->assertHasNotCacheEntry($this->cacheId);
    }

    public function test_delete_shouldNotDeleteAnyOtherCacheIds()
    {
        $this->cache->save('anyother', 'myvalue');
        $this->assertHasCacheEntry($this->cacheId);

        $this->cache->delete($this->cacheId);

        $this->assertHasCacheEntry('anyother');
    }

    public function test_flush_shouldRemoveAllCacheIds()
    {
        $this->assertHasCacheEntry($this->cacheId);
        $this->cache->save('mykey', 'myvalue');
        $this->assertHasCacheEntry('mykey');
        $this->assertTrue($this->backend->doContains($this->storageId));

        $this->cache->flushAll();

        $this->assertHasNotCacheEntry($this->cacheId);
        $this->assertHasNotCacheEntry('mykey');
        $this->assertFalse($this->backend->doContains($this->storageId)); // should also remove the storage entry
    }

    public function test_persistCacheIfNeeded_shouldActuallySaveValuesInBackend_IfThereWasSomethingSet()
    {
        $this->cache->save('mykey', 'myvalue');

        $expected = array($this->cacheId => $this->cacheValue);
        $this->assertEquals($expected, $this->getContentOfStorage());

        $this->cache->persistCacheIfNeeded(400);

        $expected['mykey'] = 'myvalue';

        $this->assertEquals($expected, $this->getContentOfStorage());
    }

    public function test_persistCacheIfNeeded_shouldActuallySaveValuesInBackend_IfThereWasSomethingDelete()
    {
        $this->cache->delete($this->cacheId);

        $expected = array($this->cacheId => $this->cacheValue);
        $this->assertEquals($expected, $this->getContentOfStorage());

        $this->cache->persistCacheIfNeeded(400);

        $this->assertEquals(array(), $this->getContentOfStorage());
    }

    public function test_persistCacheIfNeeded_shouldNotSaveAnyValuesInBackend_IfThereWasNoChange()
    {
        $this->backend->doDelete($this->storageId);
        $this->assertFalse($this->getContentOfStorage());

        $this->cache->persistCacheIfNeeded(400);

        $this->assertFalse($this->getContentOfStorage()); // should not have set the content of cache ($cacheId => $cacheValue)
    }

    private function getContentOfStorage()
    {
        return $this->backend->doFetch($this->storageId);
    }

    private function assertHasCacheEntry($cacheId)
    {
        $this->assertTrue($this->cache->contains($cacheId));
    }

    private function assertHasNotCacheEntry($cacheId)
    {
        $this->assertFalse($this->cache->contains($cacheId));
    }

}