ApcuCacheFactoryTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 3
A testConstructorSetsReloadOnMiss() 0 15 1
A testConstructorSetsTtl() 0 14 1
A testConstructorSetsPrefix() 0 15 1
A testGetInstanceReturnApcuCache() 0 5 1
A tearDown() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\MoTranslator\Tests\Cache;
6
7
use PhpMyAdmin\MoTranslator\Cache\ApcuCache;
8
use PhpMyAdmin\MoTranslator\Cache\ApcuCacheFactory;
9
use PhpMyAdmin\MoTranslator\MoParser;
10
use PHPUnit\Framework\TestCase;
11
12
use function apcu_clear_cache;
13
use function apcu_delete;
14
use function apcu_enabled;
15
use function apcu_fetch;
16
use function function_exists;
17
use function sleep;
18
19
/** @covers \PhpMyAdmin\MoTranslator\Cache\ApcuCacheFactory */
20
class ApcuCacheFactoryTest extends TestCase
21
{
22
    protected function setUp(): void
23
    {
24
        parent::setUp();
25
26
        if (function_exists('apcu_enabled') && apcu_enabled()) {
27
            return;
28
        }
29
30
        $this->markTestSkipped('ACPu extension is not installed and enabled for CLI');
31
    }
32
33
    protected function tearDown(): void
34
    {
35
        parent::tearDown();
36
37
        apcu_clear_cache();
38
    }
39
40
    public function testGetInstanceReturnApcuCache(): void
41
    {
42
        $factory = new ApcuCacheFactory();
43
        $instance = $factory->getInstance(new MoParser(null), 'foo', 'bar');
44
        self::assertInstanceOf(ApcuCache::class, $instance);
45
    }
46
47
    public function testConstructorSetsTtl(): void
48
    {
49
        $locale = 'foo';
50
        $domain = 'bar';
51
        $msgid = 'Column';
52
        $ttl = 1;
53
54
        $factory = new ApcuCacheFactory($ttl);
55
        $parser = new MoParser(__DIR__ . '/../data/little.mo');
56
        $factory->getInstance($parser, $locale, $domain);
57
        sleep($ttl * 2);
58
59
        apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $msgid, $success);
60
        self::assertFalse($success);
61
    }
62
63
    public function testConstructorSetsReloadOnMiss(): void
64
    {
65
        $expected = 'Column';
66
        $locale = 'foo';
67
        $domain = 'bar';
68
        $msgid = 'Column';
69
70
        $factory = new ApcuCacheFactory(0, false);
71
        $parser = new MoParser(__DIR__ . '/../data/little.mo');
72
73
        $instance = $factory->getInstance($parser, $locale, $domain);
74
75
        apcu_delete('mo_' . $locale . '.' . $domain . '.' . $msgid);
76
        $actual = $instance->get($msgid);
77
        self::assertSame($expected, $actual);
78
    }
79
80
    public function testConstructorSetsPrefix(): void
81
    {
82
        $expected = 'Pole';
83
        $locale = 'foo';
84
        $domain = 'bar';
85
        $msgid = 'Column';
86
        $prefix = 'baz_';
87
88
        $factory = new ApcuCacheFactory(0, true, $prefix);
89
        $parser = new MoParser(__DIR__ . '/../data/little.mo');
90
91
        $factory->getInstance($parser, $locale, $domain);
92
93
        $actual = apcu_fetch($prefix . $locale . '.' . $domain . '.' . $msgid);
94
        self::assertSame($expected, $actual);
95
    }
96
}
97