summaryrefslogtreecommitdiff
path: root/tests/Unit/Helpers/TranslatorTest.php
blob: 396d2b65819199205d47440bfbd1f047995c9e21 (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
<?php

namespace Engelsystem\Test\Unit\Helpers;

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

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

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

        $translator = new Translator($locale, $locales, $callable);

        $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.ER-01'));
    }

    /**
     * @covers \Engelsystem\Helpers\Translator::translate()
     */
    public function testTranslate()
    {
        /** @var Translator|MockObject $translator */
        $translator = $this->getMockBuilder(Translator::class)
            ->setConstructorArgs(['de_DE.UTF-8', ['de_DE.UTF-8' => 'Deutsch']])
            ->setMethods(['translateGettext'])
            ->getMock();
        $translator->expects($this->once())
            ->method('translateGettext')
            ->with('My favourite number is %u!')
            ->willReturn('Meine Lieblingszahl ist die %u!');

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