TranslatorTest::testConstructorWithFilenameParam()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\MoTranslator\Tests;
6
7
use PhpMyAdmin\MoTranslator\Cache\CacheInterface;
8
use PhpMyAdmin\MoTranslator\Cache\InMemoryCache;
9
use PhpMyAdmin\MoTranslator\CacheException;
10
use PhpMyAdmin\MoTranslator\MoParser;
11
use PhpMyAdmin\MoTranslator\Translator;
12
use PHPUnit\Framework\MockObject\MockObject;
13
use PHPUnit\Framework\TestCase;
14
15
/**
16
 * Test for translator API
17
 */
18
class TranslatorTest extends TestCase
19
{
20
    public function testConstructorWithFilenameParam(): void
21
    {
22
        $expected = 'Pole';
23
        $translator = new Translator(__DIR__ . '/data/little.mo');
24
        $actual = $translator->gettext('Column');
25
        self::assertSame($expected, $actual);
26
    }
27
28
    public function testConstructorWithNullParam(): void
29
    {
30
        $expected = 'Column';
31
        $translator = new Translator(null);
32
        $actual = $translator->gettext($expected);
33
        self::assertSame($expected, $actual);
34
    }
35
36
    /**
37
     * Test on empty gettext
38
     */
39
    public function testGettext(): void
40
    {
41
        $translator = $this->getTranslator('');
42
        self::assertSame('Test', $translator->gettext('Test'));
43
    }
44
45
    /**
46
     * Test set a translation
47
     */
48
    public function testSetTranslation(): void
49
    {
50
        $translator = $this->getTranslator('');
51
        $translator->setTranslation('Test', 'Translation');
52
        self::assertSame('Translation', $translator->gettext('Test'));
53
    }
54
55
    /**
56
     * Test get and set all translations
57
     */
58
    public function testGetSetTranslations(): void
59
    {
60
        $transTable = ['Test' => 'Translation'];
61
        $translator = $this->getTranslator('');
62
        $translator->setTranslations($transTable);
63
        self::assertSame('Translation', $translator->gettext('Test'));
64
        self::assertSame($transTable, $translator->getTranslations());
65
        $translator = $this->getTranslator(null);
66
        $translator->setTranslations($transTable);
67
        self::assertSame($transTable, $translator->getTranslations());
68
        self::assertSame('Translation', $translator->gettext('Test'));
69
        $transTable = [
70
            'Test' => 'Translation',
71
            'shouldIWriteTests' => 'as much as possible',
72
            'is it hard' => 'it depends',
73
        ];
74
        $translator = $this->getTranslator('');
75
        $translator->setTranslations($transTable);
76
        self::assertSame($transTable, $translator->getTranslations());
77
        self::assertSame('as much as possible', $translator->gettext('shouldIWriteTests'));
78
        $translator = $this->getTranslator(null);
79
        $translator->setTranslations($transTable);
80
        self::assertSame($transTable, $translator->getTranslations());
81
        self::assertSame('it depends', $translator->gettext('is it hard'));
82
    }
83
84
    public function testGetTranslationsThrowsException(): void
85
    {
86
        /** @var CacheInterface&MockObject $cache */
87
        $cache = $this->createMock(CacheInterface::class);
88
        $translator = new Translator($cache);
89
90
        $this->expectException(CacheException::class);
91
        $translator->getTranslations();
92
    }
93
94
    private function getTranslator(string|null $filename): Translator
95
    {
96
        return new Translator(new InMemoryCache(new MoParser($filename)));
97
    }
98
}
99