Passed
Push — master ( c39d60...cea13f )
by William
12:30
created

PluralTest::testNgettextSelectString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 27
rs 9.568
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
     * Test for ngettext
74
     *
75
     * @see https://github.com/phpmyadmin/motranslator/issues/37
76
     */
77
    public function testNgettextSelectString(): void
78
    {
79
        $parser = new Translator('');
80
        $parser->setTranslation(
81
            '',
82
            "Project-Id-Version: phpMyAdmin 5.1.0-dev\n"
83
            . "Report-Msgid-Bugs-To: [email protected]\n"
84
            . "PO-Revision-Date: 2020-09-01 09:12+0000\n"
85
            . "Last-Translator: William Desportes <[email protected]>\n"
86
            . 'Language-Team: English (United Kingdom) '
87
            . "<https:\/\/hosted.weblate.org\/projects\/phpmyadmin\/master\/en_GB\/>\n"
88
            . "Language: en_GB\n"
89
            . "MIME-Version: 1.0\n"
90
            . "Content-Type: text\/plain; charset=UTF-8\n"
91
            . "Content-Transfer-Encoding: 8bit\n"
92
            . "Plural-Forms: nplurals=2; plural=n != 1;\n"
93
            . "X-Generator: Weblate 4.2.1-dev\n"
94
            . ''
95
        );
96
        $translationKey = implode(chr(0), ["%d pig went to the market\n", "%d pigs went to the market\n"]);
97
        $parser->setTranslation($translationKey, 'ok');
98
        $result = $parser->ngettext(
99
            "%d pig went to the market\n",
100
            "%d pigs went to the market\n",
101
            1
102
        );
103
        $this->assertSame('ok', $result);
104
    }
105
}
106