Passed
Push — master ( b5772b...7a8e6a )
by William
10:52
created

PluralTest::dataProviderPluralForms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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