File: FactoryTest.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 (203 lines) | stat: -rw-r--r-- 6,413 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
191
192
193
194
195
196
197
198
199
200
201
202
203
<?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\DefaultTimeoutDecorated;
use Matomo\Cache\Backend\Factory;
use Matomo\Cache\Backend\File;
use Matomo\Cache\Backend\KeyPrefixDecorated;
use Matomo\Cache\Backend\NullCache;
use Matomo\Cache\Backend\Redis;
use PHPUnit\Framework\TestCase;

/**
 * @covers \Matomo\Cache\Backend\Factory
 */
class FactoryTest extends TestCase
{
    /**
     * @var Factory
     */
    private $factory;

    protected function setUp()
    {
        $this->factory = new Factory();
    }

    public function test_buildArrayCache_ShouldReturnInstanceOfArray()
    {
        $cache = $this->factory->buildArrayCache();
        $this->assertInstanceOf(ArrayCache::class, $cache);
    }

    public function test_buildNullCache_ShouldReturnInstanceOfNull()
    {
        $cache = $this->factory->buildNullCache();
        $this->assertInstanceOf(NullCache::class, $cache);
    }

    public function test_buildFileCache_ShouldReturnInstanceOfFile()
    {
        $cache = $this->factory->buildFileCache(array('directory' => __DIR__));
        $this->assertInstanceOf(File::class, $cache);
    }

    public function test_buildChainedCache_ShouldReturnInstanceOfChained()
    {
        $cache = $this->factory->buildChainedCache(array('backends' => array()));
        $this->assertInstanceOf(Chained::class, $cache);
    }

    public function test_buildBackend_Chained_ShouldActuallyCreateInstancesOfNestedBackends()
    {
        $options = array(
            'backends' => array('array', 'file'),
            'file'     => array('directory' => __DIR__),
            'array'    => array()
        );

        /** @var Chained $cache */
        $cache = $this->factory->buildBackend('chained', $options);

        $backends = $cache->getBackends();

        $this->assertInstanceOf(ArrayCache::class, $backends[0]);
        $this->assertInstanceOf(File::class, $backends[1]);
    }

    public function test_buildRedisCache_ShouldReturnInstanceOfRedis()
    {
        $this->skipTestIfRedisIsNotInstalled();

        $cache = $this->factory->buildRedisCache(array('host' => '127.0.0.1', 'port' => '6379', 'timeout' => 0.0));
        $this->assertInstanceOf(Redis::class, $cache);
    }

    public function test_buildBackend_Redis_ShouldReturnInstanceOfRedis()
    {
        $this->skipTestIfRedisIsNotInstalled();

        $options = array('host' => '127.0.0.1', 'port' => '6379', 'timeout' => 0.0);

        $cache = $this->factory->buildBackend('redis', $options);
        $this->assertInstanceOf(Redis::class, $cache);
    }

    public function test_buildBackend_Redis_ShouldForwardOptionsToRedisInstance()
    {
        $this->skipTestIfRedisIsNotInstalled();

        $options = array('host' => '127.0.0.1', 'port' => '6379', 'timeout' => 4.2, 'database' => 5);

        /** @var Redis $cache */
        $cache = $this->factory->buildBackend('redis', $options);
        $redis = $cache->getRedis();

        $this->assertEquals('127.0.0.1', $redis->getHost());
        $this->assertEquals(6379, $redis->getPort());
        $this->assertEquals(4.2, $redis->getTimeout());
        $this->assertEquals(5, $redis->getDBNum());
    }

    /**
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage RedisCache is not configured
     */
    public function test_buildRedisCache_ShouldFail_IfPortIsMissing()
    {
        $this->factory->buildRedisCache(array('host' => '127.0.0.1'));
    }

    /**
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage RedisCache is not configured
     */
    public function test_buildRedisCache_ShouldFail_IfHostIsMissing()
    {
        $this->factory->buildRedisCache(array('port' => '6379'));
    }

    public function test_buildBackend_ArrayCache_ShouldReturnInstanceOfArray()
    {
        $cache = $this->factory->buildBackend('array', array());
        $this->assertInstanceOf(ArrayCache::class, $cache);
    }

    public function test_buildBackend_NullCache_ShouldReturnInstanceOfNull()
    {
        $cache = $this->factory->buildBackend('null', array());
        $this->assertInstanceOf(NullCache::class, $cache);
    }

    public function test_buildBackend_FileCache_ShouldReturnInstanceOfFile()
    {
        $cache = $this->factory->buildBackend('file', array('directory' => __DIR__));
        $this->assertInstanceOf(File::class, $cache);
    }

    public function test_buildBackend_Chained_ShouldReturnInstanceOfChained()
    {
        $cache = $this->factory->buildBackend('chained', array('backends' => array()));
        $this->assertInstanceOf(Chained::class, $cache);
    }

    /**
     * @expectedException \Matomo\Cache\Backend\Factory\BackendNotFoundException
     */
    public function test_buildBackend_ShouldThrowException_IfInvalidTypeGiven()
    {
        $this->factory->buildBackend('noTValId', array());
    }

    private function skipTestIfRedisIsNotInstalled()
    {
        if (!extension_loaded('redis')) {
            $this->markTestSkipped('The test ' . __METHOD__ . ' requires the use of redis');
        }
    }


    public function test_buildBackend_Chained_ShouldCreateInstancesOfNestedDecorators()
    {}

    public function test_buildBackend_Decorated_DefaultTimeoutDecorated_ShouldActuallyCreateInstanceOfNestedBackend()
    {
        $options = array(
            'backend' => 'array',
            'array'    => array(),
            'defaultTimeout' => 555
        );

        /** @var DefaultTimeoutDecorated $cache */
        $cache = $this->factory->buildBackend('defaultTimeout', $options);


        $backend = $cache->getBackend();
        $this->assertInstanceOf(ArrayCache::class, $backend);
    }

    public function test_buildBackend_Decorated_KeyPrefixDecorated_ShouldActuallyCreateInstanceOfNestedBackend()
    {
        $options = array(
            'backend' => 'array',
            'array'    => array(),
            'keyPrefix' => '555'
        );

        /** @var KeyPrefixDecorated $cache */
        $cache = $this->factory->buildBackend('keyPrefix', $options);


        $backend = $cache->getBackend();
        $this->assertInstanceOf(ArrayCache::class, $backend);
    }
}