File: ApcuCacheFactoryTest.php

package info (click to toggle)
phpmyadmin-motranslator 5.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,928 kB
  • sloc: php: 1,760; makefile: 9
file content (99 lines) | stat: -rw-r--r-- 2,754 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
<?php

declare(strict_types=1);

namespace PhpMyAdmin\MoTranslator\Tests\Cache;

use PhpMyAdmin\MoTranslator\Cache\ApcuCache;
use PhpMyAdmin\MoTranslator\Cache\ApcuCacheFactory;
use PhpMyAdmin\MoTranslator\MoParser;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use PHPUnit\Framework\TestCase;

use function apcu_clear_cache;
use function apcu_delete;
use function apcu_enabled;
use function apcu_fetch;
use function function_exists;
use function sleep;

#[CoversClass(ApcuCacheFactory::class)]
#[RequiresPhpExtension('apcu')]
class ApcuCacheFactoryTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        if (function_exists('apcu_enabled') && apcu_enabled()) {
            return;
        }

        $this->markTestSkipped('ACPu extension is not installed and enabled for CLI');
    }

    protected function tearDown(): void
    {
        parent::tearDown();

        apcu_clear_cache();
    }

    public function testGetInstanceReturnApcuCache(): void
    {
        $factory = new ApcuCacheFactory();
        $instance = $factory->getInstance(new MoParser(null), 'foo', 'bar');
        self::assertInstanceOf(ApcuCache::class, $instance);
    }

    public function testConstructorSetsTtl(): void
    {
        $locale = 'foo';
        $domain = 'bar';
        $msgid = 'Column';
        $ttl = 1;

        $factory = new ApcuCacheFactory($ttl);
        $parser = new MoParser(__DIR__ . '/../data/little.mo');
        $factory->getInstance($parser, $locale, $domain);
        sleep($ttl * 2);

        apcu_fetch('mo_' . $locale . '.' . $domain . '.' . $msgid, $success);
        self::assertFalse($success);
    }

    public function testConstructorSetsReloadOnMiss(): void
    {
        $expected = 'Column';
        $locale = 'foo';
        $domain = 'bar';
        $msgid = 'Column';

        $factory = new ApcuCacheFactory(0, false);
        $parser = new MoParser(__DIR__ . '/../data/little.mo');

        $instance = $factory->getInstance($parser, $locale, $domain);

        apcu_delete('mo_' . $locale . '.' . $domain . '.' . $msgid);
        $actual = $instance->get($msgid);
        self::assertSame($expected, $actual);
    }

    public function testConstructorSetsPrefix(): void
    {
        $expected = 'Pole';
        $locale = 'foo';
        $domain = 'bar';
        $msgid = 'Column';
        $prefix = 'baz_';

        $factory = new ApcuCacheFactory(0, true, $prefix);
        $parser = new MoParser(__DIR__ . '/../data/little.mo');

        $factory->getInstance($parser, $locale, $domain);

        $actual = apcu_fetch($prefix . $locale . '.' . $domain . '.' . $msgid);
        self::assertSame($expected, $actual);
    }
}