Completed
Push — master ( 6a08aa...2252e8 )
by William
03:47
created

PluralFormulaTest::pluralExpressions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 30
c 0
b 0
f 0
rs 9.7666
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
10
/**
11
 * Test for gettext parsing.
12
 */
13
class PluralFormulaTest extends TestCase
14
{
15
    /**
16
     * Test for extractPluralsForms.
17
     *
18
     * @param mixed $header
19
     * @param mixed $expected
20
     *
21
     * @dataProvider pluralExtractionData
22
     */
23
    public function testExtractPluralsForms($header, $expected): void
24
    {
25
        $this->assertEquals(
26
            $expected,
27
            Translator::extractPluralsForms($header)
28
        );
29
    }
30
31
    public function pluralExtractionData(): array
32
    {
33
        return [
34
            // It defaults to a "Western-style" plural header.
35
            [
36
                '',
37
                'nplurals=2; plural=n == 1 ? 0 : 1;',
38
            ],
39
            // Extracting it from the middle of the header works.
40
            [
41
                "Content-type: text/html; charset=UTF-8\n"
42
                . "Plural-Forms: nplurals=1; plural=0;\n"
43
                . "Last-Translator: nobody\n",
44
                ' nplurals=1; plural=0;',
45
            ],
46
            // It's also case-insensitive.
47
            [
48
                "PLURAL-forms: nplurals=1; plural=0;\n",
49
                ' nplurals=1; plural=0;',
50
            ],
51
            // It falls back to default if it's not on a separate line.
52
            [
53
                'Content-type: text/html; charset=UTF-8' // note the missing \n here
54
                . "Plural-Forms: nplurals=1; plural=0;\n"
55
                . "Last-Translator: nobody\n",
56
                'nplurals=2; plural=n == 1 ? 0 : 1;',
57
            ],
58
        ];
59
    }
60
61
    /**
62
     * @param mixed $expr
63
     * @param mixed $expected
64
     *
65
     * @dataProvider pluralCounts
66
     */
67
    public function testPluralCounts($expr, $expected): void
68
    {
69
        $this->assertEquals(
70
            $expected,
71
            Translator::extractPluralCount($expr)
72
        );
73
    }
74
75
    public function pluralCounts(): array
76
    {
77
        return [
78
            [
79
                '',
80
                1,
81
            ],
82
            [
83
                'foo=2; expr',
84
                1,
85
            ],
86
            [
87
                'nplurals=2; epxr',
88
                2,
89
            ],
90
            [
91
                ' nplurals = 3 ; epxr',
92
                3,
93
            ],
94
            [
95
                ' nplurals = 4 ; epxr ; ',
96
                4,
97
            ],
98
            [
99
                'nplurals',
100
                1,
101
            ],
102
        ];
103
    }
104
105
    /**
106
     * @param mixed $expr
107
     * @param mixed $expected
108
     *
109
     * @dataProvider pluralExpressions
110
     */
111
    public function testPluralExpression($expr, $expected): void
112
    {
113
        $this->assertEquals(
114
            $expected,
115
            Translator::sanitizePluralExpression($expr)
116
        );
117
    }
118
119
    public function pluralExpressions(): array
120
    {
121
        return [
122
            [
123
                '',
124
                '',
125
            ],
126
            [
127
                'nplurals=2; plural=n == 1 ? 0 : 1;',
128
                'n == 1 ? 0 : 1',
129
            ],
130
            [
131
                ' nplurals=1; plural=0;',
132
                '0',
133
            ],
134
            [
135
                "nplurals=6; plural=n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5;\n",
136
                'n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5',
137
            ],
138
            [
139
                ' nplurals=1; plural=baz(n);',
140
                '(n)',
141
            ],
142
            [
143
                ' plural=n',
144
                'n',
145
            ],
146
            [
147
                'nplurals',
148
                'n',
149
            ],
150
        ];
151
    }
152
}
153