summaryrefslogtreecommitdiff
path: root/tests/Unit/Helpers/Translation/TranslatorTest.php
blob: c173209a6f40c4db095a48571fc88c9d74f3172f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php

namespace Engelsystem\Test\Unit\Helpers\Translation;

use Engelsystem\Helpers\Translation\GettextTranslator;
use Engelsystem\Helpers\Translation\TranslationNotFound;
use Engelsystem\Helpers\Translation\Translator;
use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit\Framework\MockObject\MockObject;
use stdClass;

class TranslatorTest extends ServiceProviderTest
{
    /**
     * @covers \Engelsystem\Helpers\Translation\Translator::__construct
     * @covers \Engelsystem\Helpers\Translation\Translator::getLocale
     * @covers \Engelsystem\Helpers\Translation\Translator::getLocales
     * @covers \Engelsystem\Helpers\Translation\Translator::hasLocale
     * @covers \Engelsystem\Helpers\Translation\Translator::setLocale
     * @covers \Engelsystem\Helpers\Translation\Translator::setLocales
     */
    public function testInit()
    {
        $locales = ['te_ST' => 'Tests', 'fo_OO' => 'SomeFOO'];
        $locale = 'te_ST';

        /** @var callable|MockObject $localeChange */
        $localeChange = $this->getMockBuilder(stdClass::class)
            ->setMethods(['__invoke'])
            ->getMock();
        $localeChange->expects($this->exactly(2))
            ->method('__invoke')
            ->withConsecutive(['te_ST'], ['fo_OO']);

        $translator = new Translator($locale, 'fo_OO', function () { }, $locales, $localeChange);

        $this->assertEquals($locales, $translator->getLocales());
        $this->assertEquals($locale, $translator->getLocale());

        $translator->setLocale('fo_OO');
        $this->assertEquals('fo_OO', $translator->getLocale());

        $newLocales = ['lo_RM' => 'Lorem', 'ip_SU-M' => 'Ipsum'];
        $translator->setLocales($newLocales);
        $this->assertEquals($newLocales, $translator->getLocales());

        $this->assertTrue($translator->hasLocale('ip_SU-M'));
        $this->assertFalse($translator->hasLocale('te_ST'));
    }

    /**
     * @covers \Engelsystem\Helpers\Translation\Translator::translate
     */
    public function testTranslate()
    {
        /** @var Translator|MockObject $translator */
        $translator = $this->getMockBuilder(Translator::class)
            ->setConstructorArgs(['de_DE', 'en_US', function () { }, ['de_DE' => 'Deutsch']])
            ->setMethods(['translateText'])
            ->getMock();
        $translator->expects($this->exactly(2))
            ->method('translateText')
            ->withConsecutive(['gettext', ['Hello!'], []], ['gettext', ['My favourite number is %u!'], [3]])
            ->willReturnOnConsecutiveCalls('Hallo!', 'Meine Lieblingszahl ist die 3!');

        $return = $translator->translate('Hello!');
        $this->assertEquals('Hallo!', $return);

        $return = $translator->translate('My favourite number is %u!', [3]);
        $this->assertEquals('Meine Lieblingszahl ist die 3!', $return);
    }

    /**
     * @covers \Engelsystem\Helpers\Translation\Translator::translatePlural
     */
    public function testTranslatePlural()
    {
        /** @var Translator|MockObject $translator */
        $translator = $this->getMockBuilder(Translator::class)
            ->setConstructorArgs(['de_DE', 'en_US', function () { }, ['de_DE' => 'Deutsch']])
            ->setMethods(['translateText'])
            ->getMock();
        $translator->expects($this->once())
            ->method('translateText')
            ->with('ngettext', ['%s apple', '%s apples', 2], [2])
            ->willReturn('2 Äpfel');

        $return = $translator->translatePlural('%s apple', '%s apples', 2, [2]);
        $this->assertEquals('2 Äpfel', $return);
    }

    /**
     * @covers \Engelsystem\Helpers\Translation\Translator::translatePlural
     * @covers \Engelsystem\Helpers\Translation\Translator::translateText
     * @covers \Engelsystem\Helpers\Translation\Translator::replaceText
     */
    public function testReplaceText()
    {
        /** @var GettextTranslator|MockObject $gtt */
        $gtt = $this->createMock(GettextTranslator::class);
        /** @var callable|MockObject $getTranslator */
        $getTranslator = $this->getMockBuilder(stdClass::class)
            ->setMethods(['__invoke'])
            ->getMock();
        $getTranslator->expects($this->exactly(5))
            ->method('__invoke')
            ->withConsecutive(['te_ST'], ['fo_OO'], ['te_ST'], ['fo_OO'], ['te_ST'])
            ->willReturn($gtt);

        $i = 0;
        $gtt->expects($this->exactly(4))
            ->method('gettext')
            ->willReturnCallback(function () use (&$i) {
                $i++;
                if ($i != 4) {
                    throw new TranslationNotFound();
                }

                return 'Lorem %s???';
            });
        $this->setExpects($gtt, 'ngettext', ['foo.barf'], 'Lorem %s!');

        $translator = new Translator('te_ST', 'fo_OO', $getTranslator, ['te_ST' => 'Test', 'fo_OO' => 'Foo']);

        // No translation
        $this->assertEquals('foo.bar', $translator->translate('foo.bar'));

        // Fallback translation
        $this->assertEquals('Lorem test2???', $translator->translate('foo.batz', ['test2']));

        // Successful translation
        $this->assertEquals('Lorem test3!', $translator->translatePlural('foo.barf', 'foo.bar2', 3, ['test3']));
    }
}