Passed
Push — master ( 122983...91227d )
by William
02:34 queued 26s
created

ApcuCacheFactoryTest::setUp()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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