summaryrefslogtreecommitdiff
path: root/tests/Unit/Helpers/Translation/TranslationServiceProviderTest.php
blob: 91307bdda3214aa99c1d96343399ec48c3648652 (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
<?php

namespace Engelsystem\Test\Unit\Helpers\Translation;

use Engelsystem\Config\Config;
use Engelsystem\Helpers\Translation\TranslationServiceProvider;
use Engelsystem\Helpers\Translation\Translator;
use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\HttpFoundation\Session\Session;

class TranslationServiceProviderTest extends ServiceProviderTest
{
    /**
     * @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::register()
     */
    public function testRegister(): void
    {
        $defaultLocale = 'fo_OO';
        $locale = 'te_ST.WTF-9';
        $locales = ['fo_OO' => 'Foo', 'fo_OO.BAR' => 'Foo (Bar)', 'te_ST.WTF-9' => 'WTF\'s Testing?'];
        $config = new Config(['locales' => $locales, 'default_locale' => $defaultLocale]);

        $app = $this->getApp(['make', 'instance', 'get']);
        /** @var Session|MockObject $session */
        $session = $this->createMock(Session::class);
        /** @var Translator|MockObject $translator */
        $translator = $this->createMock(Translator::class);

        /** @var TranslationServiceProvider|MockObject $serviceProvider */
        $serviceProvider = $this->getMockBuilder(TranslationServiceProvider::class)
            ->setConstructorArgs([$app])
            ->setMethods(['setLocale'])
            ->getMock();

        $app->expects($this->exactly(2))
            ->method('get')
            ->withConsecutive(['config'], ['session'])
            ->willReturnOnConsecutiveCalls($config, $session);

        $session->expects($this->once())
            ->method('get')
            ->with('locale', $defaultLocale)
            ->willReturn($locale);
        $session->expects($this->once())
            ->method('set')
            ->with('locale', $locale);

        $app->expects($this->once())
            ->method('make')
            ->with(
                Translator::class,
                [
                    'locale'                => $locale,
                    'locales'               => $locales,
                    'fallbackLocale'        => 'en_US',
                    'getTranslatorCallback' => [$serviceProvider, 'getTranslator'],
                    'localeChangeCallback'  => [$serviceProvider, 'setLocale'],
                ]
            )
            ->willReturn($translator);

        $app->expects($this->exactly(2))
            ->method('instance')
            ->withConsecutive(
                [Translator::class, $translator],
                ['translator', $translator]
            );

        $serviceProvider->register();
    }

    /**
     * @covers \Engelsystem\Helpers\Translation\TranslationServiceProvider::getTranslator()
     */
    public function testGetTranslator(): void
    {
        $app = $this->getApp(['get']);
        $serviceProvider = new TranslationServiceProvider($app);

        $this->setExpects($app, 'get', ['path.lang'], __DIR__ . '/Assets');

        // Get translator
        $translator = $serviceProvider->getTranslator('fo_OO');
        $this->assertEquals('Foo Bar!', $translator->gettext('foo.bar'));

        // Retry from cache
        $serviceProvider->getTranslator('fo_OO');
    }
}