MoFilesTest::testExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\MoTranslator\Tests;
6
7
use PhpMyAdmin\MoTranslator\Cache\InMemoryCache;
8
use PhpMyAdmin\MoTranslator\MoParser;
9
use PhpMyAdmin\MoTranslator\Translator;
10
use PHPUnit\Framework\TestCase;
11
12
use function basename;
13
use function glob;
14
use function str_contains;
15
16
/**
17
 * Test for MO files parsing.
18
 */
19
class MoFilesTest extends TestCase
20
{
21
    /** @dataProvider provideMoFiles */
22
    public function testMoFileTranslate(string $filename): void
23
    {
24
        $parser = $this->getTranslator($filename);
25
        self::assertSame(
26
            'Pole',
27
            $parser->gettext('Column'),
28
        );
29
        // Non existing string
30
        self::assertSame(
31
            'Column parser',
32
            $parser->gettext('Column parser'),
33
        );
34
    }
35
36
    /** @dataProvider provideMoFiles */
37
    public function testMoFilePlurals(string $filename): void
38
    {
39
        $parser = $this->getTranslator($filename);
40
        $expected2 = '%d sekundy';
41
        if (str_contains($filename, 'invalid-formula.mo') || str_contains($filename, 'lessplurals.mo')) {
42
            $expected0 = '%d sekunda';
43
            $expected2 = '%d sekunda';
44
        } elseif (str_contains($filename, 'plurals.mo') || str_contains($filename, 'noheader.mo')) {
45
            $expected0 = '%d sekundy';
46
        } else {
47
            $expected0 = '%d sekund';
48
        }
49
50
        self::assertSame($expected0, $parser->ngettext('%d second', '%d seconds', 0));
51
        self::assertSame('%d sekunda', $parser->ngettext('%d second', '%d seconds', 1));
52
        self::assertSame($expected2, $parser->ngettext('%d second', '%d seconds', 2));
53
        self::assertSame($expected0, $parser->ngettext('%d second', '%d seconds', 5));
54
        self::assertSame($expected0, $parser->ngettext('%d second', '%d seconds', 10));
55
        // Non existing string
56
        self::assertSame('"%d" seconds', $parser->ngettext('"%d" second', '"%d" seconds', 10));
57
    }
58
59
    /** @dataProvider provideMoFiles */
60
    public function testMoFileContext(string $filename): void
61
    {
62
        $parser = $this->getTranslator($filename);
63
        self::assertSame('Tabulka', $parser->pgettext('Display format', 'Table'));
64
    }
65
66
    /** @dataProvider provideNotTranslatedFiles */
67
    public function testMoFileNotTranslated(string $filename): void
68
    {
69
        $parser = $this->getTranslator($filename);
70
        self::assertSame('%d second', $parser->ngettext('%d second', '%d seconds', 1));
71
    }
72
73
    /** @return list<array{string}> */
0 ignored issues
show
Bug introduced by
The type PhpMyAdmin\MoTranslator\Tests\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
74
    public static function provideMoFiles(): array
75
    {
76
        return self::getFiles('./tests/data/*.mo');
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::getFiles('./tests/data/*.mo') returns the type array|array<mixed,array> which is incompatible with the documented return type PhpMyAdmin\MoTranslator\Tests\list.
Loading history...
77
    }
78
79
    /** @return list<array{string}> */
80
    public static function provideErrorMoFiles(): array
81
    {
82
        return self::getFiles('./tests/data/error/*.mo');
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::getFiles('./tests/data/error/*.mo') returns the type array|array<mixed,array> which is incompatible with the documented return type PhpMyAdmin\MoTranslator\Tests\list.
Loading history...
83
    }
84
85
    /** @return list<array{string}> */
86
    public static function provideNotTranslatedFiles(): array
87
    {
88
        return self::getFiles('./tests/data/not-translated/*.mo');
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::getFiles('....a/not-translated/*.mo') returns the type array|array<mixed,array> which is incompatible with the documented return type PhpMyAdmin\MoTranslator\Tests\list.
Loading history...
89
    }
90
91
    /** @dataProvider provideErrorMoFiles */
92
    public function testEmptyMoFile(string $file): void
93
    {
94
        $parser = new MoParser($file);
95
        $translator = new Translator(new InMemoryCache($parser));
96
        if (basename($file) === 'magic.mo') {
97
            self::assertSame(Translator::ERROR_BAD_MAGIC, $parser->error);
98
        } else {
99
            self::assertSame(Translator::ERROR_READING, $parser->error);
100
        }
101
102
        self::assertSame('Table', $translator->pgettext('Display format', 'Table'));
103
        self::assertSame('"%d" seconds', $translator->ngettext('"%d" second', '"%d" seconds', 10));
104
    }
105
106
    /** @dataProvider provideMoFiles */
107
    public function testExists(string $file): void
108
    {
109
        $parser = $this->getTranslator($file);
110
        self::assertTrue($parser->exists('Column'));
111
        self::assertFalse($parser->exists('Column parser'));
112
    }
113
114
    /**
115
     * @param string $pattern path names pattern to match
116
     *
117
     * @return list<array{string}>
118
     */
119
    private static function getFiles(string $pattern): array
120
    {
121
        $files = glob($pattern);
122
        if ($files === false) {
123
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array() returns the type array which is incompatible with the documented return type PhpMyAdmin\MoTranslator\Tests\list.
Loading history...
124
        }
125
126
        $result = [];
127
        foreach ($files as $file) {
128
            $result[] = [$file];
129
        }
130
131
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type array|array<mixed,array> which is incompatible with the documented return type PhpMyAdmin\MoTranslator\Tests\list.
Loading history...
132
    }
133
134
    private function getTranslator(string $filename): Translator
135
    {
136
        return new Translator(new InMemoryCache(new MoParser($filename)));
137
    }
138
}
139