PluralTest::testNgettextSelectString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 20
c 3
b 0
f 0
dl 0
loc 23
rs 9.6
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 chr;
13
use function implode;
14
15
/**
16
 * Test for gettext parsing.
17
 */
18
class PluralTest extends TestCase
19
{
20
    /**
21
     * Test for npgettext.
22
     *
23
     * @param int    $number   Number
24
     * @param string $expected Expected output
25
     *
26
     * @dataProvider providerTestNpgettext
27
     */
28
    public function testNpgettext(int $number, string $expected): void
29
    {
30
        $parser = $this->getTranslator('');
31
        $result = $parser->npgettext('context', "%d pig went to the market\n", "%d pigs went to the market\n", $number);
32
        self::assertSame($expected, $result);
33
    }
34
35
    /**
36
     * Data provider for test_npgettext.
37
     *
38
     * @return list<array{int, 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...
39
     */
40
    public static function providerTestNpgettext(): array
41
    {
42
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(array(1, '%... went to the market ')) returns the type array<integer,array<integer,integer|string>> which is incompatible with the documented return type PhpMyAdmin\MoTranslator\Tests\list.
Loading history...
43
            [
44
                1,
45
                "%d pig went to the market\n",
46
            ],
47
            [
48
                2,
49
                "%d pigs went to the market\n",
50
            ],
51
        ];
52
    }
53
54
    /**
55
     * Test for ngettext
56
     */
57
    public function testNgettext(): void
58
    {
59
        $parser = $this->getTranslator('');
60
        $translationKey = implode(chr(0), ["%d pig went to the market\n", "%d pigs went to the market\n"]);
61
        $parser->setTranslation($translationKey, '');
62
        $result = $parser->ngettext("%d pig went to the market\n", "%d pigs went to the market\n", 1);
63
        self::assertSame('', $result);
64
    }
65
66
    /** @return list<array{string}> */
67
    public static function dataProviderPluralForms(): array
68
    {
69
        return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(array('Plur...= 6 || n % 10 == 9);')) returns the type array<integer,array<integer,string>> which is incompatible with the documented return type PhpMyAdmin\MoTranslator\Tests\list.
Loading history...
70
            ['Plural-Forms: nplurals=2; plural=n != 1;'],
71
            ['Plural-Forms: nplurals=1; plural=0;'],
72
            ['Plural-Forms: nplurals=2; plural=(n > 1);'],
73
            [
74
                'Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n'
75
                . '%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;',
76
            ],
77
            ['Plural-Forms: nplurals=2; plural=n >= 2 && (n < 11 || n > 99);'],
78
            ['Plural-Forms: nplurals=4; plural=n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3;'],
79
            ['Plural-Forms: nplurals=3; plural=n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2;'],
80
            [
81
                'Plural-Forms: nplurals=2; plural=n != 1 && n != 2 && n != 3 &'
82
            . '& (n % 10 == 4 || n % 10 == 6 || n % 10 == 9);',
83
            ],
84
        ];
85
    }
86
87
    /**
88
     * Test for ngettext
89
     *
90
     * @see https://github.com/phpmyadmin/motranslator/issues/37
91
     *
92
     * @dataProvider dataProviderPluralForms
93
     */
94
    public function testNgettextSelectString(string $pluralForms): void
95
    {
96
        $parser = $this->getTranslator('');
97
        $parser->setTranslation(
98
            '',
99
            "Project-Id-Version: phpMyAdmin 5.1.0-dev\n"
100
            . "Report-Msgid-Bugs-To: [email protected]\n"
101
            . "PO-Revision-Date: 2020-09-01 09:12+0000\n"
102
            . "Last-Translator: William Desportes <[email protected]>\n"
103
            . 'Language-Team: English (United Kingdom) '
104
            . "<https:\/\/hosted.weblate.org\/projects\/phpmyadmin\/master\/en_GB\/>\n"
105
            . "Language: en_GB\n"
106
            . "MIME-Version: 1.0\n"
107
            . "Content-Type: text\/plain; charset=UTF-8\n"
108
            . "Content-Transfer-Encoding: 8bit\n"
109
            . $pluralForms . "\n"
110
            . "X-Generator: Weblate 4.2.1-dev\n"
111
            . '',
112
        );
113
        $translationKey = implode(chr(0), ["%d pig went to the market\n", "%d pigs went to the market\n"]);
114
        $parser->setTranslation($translationKey, 'ok');
115
        $result = $parser->ngettext("%d pig went to the market\n", "%d pigs went to the market\n", 1);
116
        self::assertSame('ok', $result);
117
    }
118
119
    private function getTranslator(string $filename): Translator
120
    {
121
        return new Translator(new InMemoryCache(new MoParser($filename)));
122
    }
123
}
124