File: ChainedTest.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 (59 lines) | stat: -rw-r--r-- 1,676 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
<?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\Backend;

use Matomo\Cache\Backend\ArrayCache;
use Matomo\Cache\Backend\Chained;
use Matomo\Cache\Backend\NullCache;
use PHPUnit\Framework\TestCase;

/**
 * @covers \Matomo\Cache\Backend\Chained
 */
class ChainedTest extends TestCase
{

    public function test_constructor_getbackends_shouldMakeSureToHaveProperIndex()
    {
        $arrayCache = new ArrayCache();
        $nullCache  = new NullCache();

        $backends = array(0 => $arrayCache, 2 => $nullCache);
        $cache = $this->createChainedCache($backends);

        $result = $cache->getBackends();
        $this->assertEquals(array($arrayCache, $nullCache), $result);
    }

    public function test_doFetch_shouldPopulateOtherCaches()
    {
        $cacheId = 'myid';
        $cacheValue = 'mytest';

        $arrayCache1 = new ArrayCache();
        $arrayCache2 = new ArrayCache();
        $arrayCache2->doSave($cacheId, $cacheValue);
        $arrayCache3 = new ArrayCache();

        $cache = $this->createChainedCache(array($arrayCache1, $arrayCache2, $arrayCache3));
        $this->assertEquals($cacheValue, $cache->doFetch($cacheId)); // should find the value from second cache

        // should populate previous cache
        $this->assertEquals($cacheValue, $arrayCache1->doFetch($cacheId));

        // should not populate slower cache
        $this->assertFalse($arrayCache3->doContains('myid'));
    }

    private function createChainedCache($backends)
    {
        return new Chained($backends);
    }

}