Passed
Pull Request — master (#19)
by Alexander
01:23
created

TranslatorTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 7
eloc 59
c 6
b 0
f 2
dl 0
loc 122
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A formatMessage() 0 7 2
A testTranslation() 0 30 2
A getTranslations() 0 6 1
A testFallbackLocale() 0 29 1
A testMissingEventTriggered() 0 31 1
1
<?php
2
3
namespace Yii\I18n\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Yiisoft\I18n\MessageFormatterInterface;
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Yiisoft\I18n\Event\MissingTranslationEvent;
9
use Yiisoft\I18n\MessageReaderInterface;
10
use Yiisoft\I18n\Translator\Translator;
11
12
final class TranslatorTest extends TestCase
13
{
14
    /**
15
     * @dataProvider getTranslations
16
     * @param string|null $message
17
     * @param string|null $translation
18
     * @param string|null $expected
19
     * @param array $parameters
20
     * @param string|null $category
21
     */
22
    public function testTranslation(?string $message, ?string $translation, ?string $expected, array $parameters, ?string $category): void
23
    {
24
        $messageReader = $this->getMockBuilder(MessageReaderInterface::class)->getMock();
25
        $messageReader
26
            ->method('all')
27
            ->willReturn([$message => $translation]);
28
29
        $messageFormatter = null;
30
        if ([] !== $parameters) {
31
            $messageFormatter = $this->getMockBuilder(MessageFormatterInterface::class)->getMock();
32
            $messageFormatter
33
                ->method('format')
34
                ->willReturn($this->formatMessage($translation, $parameters));
0 ignored issues
show
Bug introduced by
It seems like $translation can also be of type null; however, parameter $message of Yii\I18n\Tests\TranslatorTest::formatMessage() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
                ->willReturn($this->formatMessage(/** @scrutinizer ignore-type */ $translation, $parameters));
Loading history...
35
        }
36
37
        /**
38
         * @var $translator Translator
39
         */
40
        $translator = $this->getMockBuilder(Translator::class)
41
            ->setConstructorArgs(
42
                [
43
                    $this->createMock(EventDispatcherInterface::class),
44
                    $messageReader,
45
                    $messageFormatter
46
                ]
47
            )
48
            ->enableProxyingToOriginalMethods()
49
            ->getMock();
50
51
        $this->assertEquals($expected, $translator->translate($message, $parameters, $category));
52
    }
53
54
    public function testFallbackLocale(): void
55
    {
56
        $category = 'test';
57
        $message = 'test';
58
        $fallbackMessage = 'test de locale';
59
60
        $messageReader = $this->getMockBuilder(MessageReaderInterface::class)->getMock();
61
        $messageReader
62
            ->method('all')
63
            ->will($this->onConsecutiveCalls([], ['test' => $fallbackMessage]));
64
65
        /**
66
         * @var $translator Translator
67
         */
68
        $translator = $this->getMockBuilder(Translator::class)
69
            ->setConstructorArgs(
70
                [
71
                    $this->createMock(EventDispatcherInterface::class),
72
                    $messageReader,
73
                ]
74
            )
75
            ->enableProxyingToOriginalMethods()
76
            ->getMock();
77
78
        $translator->setDefaultLocale('de');
79
80
81
82
        $this->assertEquals($fallbackMessage, $translator->translate($message, [], $category, 'en'));
83
    }
84
85
    public function testMissingEventTriggered(): void
86
    {
87
        $category = 'test';
88
        $language = 'en';
89
        $message = 'Message';
90
91
        $eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\MockOb...ckBuilder::setMethods() has been deprecated: https://github.com/sebastianbergmann/phpunit/pull/3687 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

91
        $eventDispatcher = /** @scrutinizer ignore-deprecated */ $this->getMockBuilder(EventDispatcherInterface::class)

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
92
            ->setMethods(['dispatch'])
93
            ->getMock();
94
95
        /**
96
         * @var $translator Translator
97
         */
98
        $translator = $this->getMockBuilder(Translator::class)
99
            ->setConstructorArgs(
100
                [
101
                    $eventDispatcher,
102
                    $this->createMock(MessageReaderInterface::class),
103
                ]
104
            )
105
            ->enableProxyingToOriginalMethods()
106
            ->getMock();
107
108
        $translator->setDefaultLocale('de');
109
110
        $eventDispatcher
111
            ->expects($this->at(0))
112
            ->method('dispatch')
113
            ->with(new MissingTranslationEvent($category, $language, $message));
114
115
        $translator->translate($message, [], $category, $language);
116
    }
117
118
    public function getTranslations(): array
119
    {
120
        return [
121
            [null, null, null, [], null],
122
            ['test', 'test', 'test', [], null],
123
            ['test {param}', 'translated {param}', 'translated param-value', ['param' => 'param-value'], null],
124
        ];
125
    }
126
127
    private function formatMessage(string $message, array $parameters): string
128
    {
129
        foreach ($parameters as $key => $value) {
130
            $message = str_replace('{' . $key . '}', $value, $message);
131
        }
132
133
        return $message;
134
    }
135
}
136