Passed
Pull Request — master (#43)
by Buster
09:23
created

Psr6CacheFactoryTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetInstanceReturnsPsr6Cache() 0 5 1
A setUp() 0 5 1
A tearDown() 0 5 1
A testConstructorSetsTtl() 0 16 1
A testConstructorSetsReloadOnMiss() 0 16 1
A testConstructorSetsPrefix() 0 15 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\MoTranslator\Tests\Cache;
6
7
use PhpMyAdmin\MoTranslator\Cache\Psr6Cache;
8
use PhpMyAdmin\MoTranslator\Cache\Psr6CacheFactory;
9
use PhpMyAdmin\MoTranslator\MoParser;
10
use PHPUnit\Framework\TestCase;
11
use Psr\Cache\CacheItemPoolInterface;
12
use Symfony\Component\Cache\Adapter\ArrayAdapter;
13
14
use function md5;
15
use function sleep;
16
17
/**
18
 * @covers \PhpMyAdmin\MoTranslator\Cache\Psr6CacheFactory
19
 */
20
class Psr6CacheFactoryTest extends TestCase
21
{
22
    /** @var CacheItemPoolInterface */
23
    protected $psr6Cache;
24
25
    protected function setUp(): void
26
    {
27
        parent::setUp();
28
29
        $this->psr6Cache = new ArrayAdapter();
30
    }
31
32
    protected function tearDown(): void
33
    {
34
        parent::tearDown();
35
36
        $this->psr6Cache->clear();
37
    }
38
39
    public function testGetInstanceReturnsPsr6Cache(): void
40
    {
41
        $factory = new Psr6CacheFactory($this->psr6Cache);
42
        $instance = $factory->getInstance(new MoParser(null), 'foo', 'bar');
43
        $this->assertInstanceOf(Psr6Cache::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 Psr6CacheFactory($this->psr6Cache, $ttl);
54
55
        $parser = new MoParser(__DIR__ . '/../data/little.mo');
56
        $factory->getInstance($parser, $locale, $domain);
57
58
        sleep($ttl * 2);
59
60
        $cacheItem = $this->psr6Cache->getItem('mo_' . $locale . '.' . $domain . '.' . md5($msgid));
61
        $this->assertFalse($cacheItem->isHit());
62
    }
63
64
    public function testConstructorSetsReloadOnMiss(): void
65
    {
66
        $expected = 'Column';
67
        $locale = 'foo';
68
        $domain = 'bar';
69
        $msgid = 'Column';
70
71
        $factory = new Psr6CacheFactory($this->psr6Cache, 0, false);
72
        $parser = new MoParser(__DIR__ . '/../data/little.mo');
73
74
        $instance = $factory->getInstance($parser, $locale, $domain);
75
76
        $this->psr6Cache->deleteItem('mo_' . $locale . '.' . $domain . '.' . md5($msgid));
77
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 Psr6CacheFactory($this->psr6Cache, 0, true, $prefix);
91
        $parser = new MoParser(__DIR__ . '/../data/little.mo');
92
93
        $factory->getInstance($parser, $locale, $domain);
94
95
        $actual = $this->psr6Cache->getItem($prefix . $locale . '.' . $domain . '.' . md5($msgid))->get();
96
        $this->assertSame($expected, $actual);
97
    }
98
}
99