Passed
Pull Request — master (#42)
by
unknown
12:42
created

ApcuCacheFactoryTest   A

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
class ApcuCacheFactoryTest extends TestCase
20
{
21
    protected function setUp(): void
22
    {
23
        parent::setUp();
24
25
        if (function_exists('apcu_enabled') && apcu_enabled()) {
26
            return;
27
        }
28
29
        $this->markTestSkipped('ACPu extension is not installed and enabled for CLI');
30
    }
31
32
    protected function tearDown(): void
33
    {
34
        parent::tearDown();
35
36
        apcu_clear_cache();
37
    }
38
39
    public function testGetInstanceReturnApcuCache(): void
40
    {
41
        $factory = new ApcuCacheFactory();
42
        $instance = $factory->getInstance(new MoParser(null), 'foo', 'bar');
43
        $this->assertInstanceOf(ApcuCache::class, $instance);
44
    }
45
46
    public function testConstructorSetsTtl(): void
47
    {
48
        $locale = 'foo';
49
        $domain = 'bar';
50
        $msgid = 'Column';
51
        $ttl = 1;
52
53
        $factory = new ApcuCacheFactory($ttl);
54
        $parser = new MoParser(__DIR__ . '/../data/little.mo');
55
        $factory->getInstance($parser, $locale, $domain);
56
        sleep($ttl * 2);
57
58
        apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $msgid, $success);
59
        $this->assertFalse($success);
60
    }
61
62
    public function testConstructorSetsReloadOnMiss(): void
63
    {
64
        $expected = 'Column';
65
        $locale = 'foo';
66
        $domain = 'bar';
67
        $msgid = 'Column';
68
69
        $factory = new ApcuCacheFactory(0, false);
70
        $parser = new MoParser(__DIR__ . '/../data/little.mo');
71
72
        $instance = $factory->getInstance($parser, $locale, $domain);
73
74
        apcu_delete('mo_' . $locale . '.' . $domain . '.' . $msgid);
75
        $actual = $instance->get($msgid);
76
        $this->assertSame($expected, $actual);
77
    }
78
79
    public function testConstructorSetsPrefix(): void
80
    {
81
        $expected = 'Pole';
82
        $locale = 'foo';
83
        $domain = 'bar';
84
        $msgid = 'Column';
85
        $prefix = 'baz_';
86
87
        $factory = new ApcuCacheFactory(0, true, $prefix);
88
        $parser = new MoParser(__DIR__ . '/../data/little.mo');
89
90
        $factory->getInstance($parser, $locale, $domain);
91
92
        $actual = apcu_fetch($prefix . $locale . '.' . $domain . '.' . $msgid);
93
        $this->assertSame($expected, $actual);
94
    }
95
}
96