Passed
Push — master ( 1fc7ea...714bef )
by Alexander
01:16
created

TranslatorTest.php$0 ➔ one()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
1
<?php
2
3
namespace Yii\I18n\Tests;
4
5
use PHPUnit\Framework\TestCase;
6
use Psr\EventDispatcher\EventDispatcherInterface;
7
use Yiisoft\I18n\Event\MissingTranslationEvent;
8
use Yiisoft\I18n\MessageFormatterInterface;
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 $id
17
     * @param string|null $translation
18
     * @param string|null $expected
19
     * @param array $parameters
20
     * @param string|null $category
21
     */
22
    public function testTranslation(
23
        ?string $id,
24
        ?string $translation,
25
        ?string $expected,
26
        array $parameters,
27
        ?string $category
28
    ): void {
29
        $messageReader = $this->createMessageReader([$id => $translation]);
30
31
        $messageFormatter = null;
32
        if ([] !== $parameters) {
33
            $messageFormatter = $this->getMockBuilder(MessageFormatterInterface::class)->getMock();
34
            $messageFormatter
35
                ->method('format')
36
                ->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

36
                ->willReturn($this->formatMessage(/** @scrutinizer ignore-type */ $translation, $parameters));
Loading history...
37
        }
38
39
        /**
40
         * @var $translator Translator
41
         */
42
        $translator = $this->getMockBuilder(Translator::class)
43
            ->setConstructorArgs(
44
                [
45
                    $this->createMock(EventDispatcherInterface::class),
46
                    $messageReader,
47
                    $messageFormatter
48
                ]
49
            )
50
            ->enableProxyingToOriginalMethods()
51
            ->getMock();
52
53
        $this->assertEquals($expected, $translator->translate($id, $parameters, $category));
0 ignored issues
show
Bug introduced by
It seems like $id can also be of type null; however, parameter $id of Yiisoft\I18n\Translator\Translator::translate() 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

53
        $this->assertEquals($expected, $translator->translate(/** @scrutinizer ignore-type */ $id, $parameters, $category));
Loading history...
54
    }
55
56
    public function testFallbackLocale(): void
57
    {
58
        $category = 'test';
59
        $message = 'test';
60
        $fallbackMessage = 'test de locale';
61
62
        $messageReader = $this->createMessageReader(['test' => $fallbackMessage]);
63
64
        /**
65
         * @var $translator Translator
66
         */
67
        $translator = $this->getMockBuilder(Translator::class)
68
            ->setConstructorArgs(
69
                [
70
                    $this->createMock(EventDispatcherInterface::class),
71
                    $messageReader,
72
                ]
73
            )
74
            ->enableProxyingToOriginalMethods()
75
            ->getMock();
76
77
        $translator->setDefaultLocale('de');
78
79
80
        $this->assertEquals($fallbackMessage, $translator->translate($message, [], $category, 'en'));
81
    }
82
83
    public function testMissingEventTriggered(): void
84
    {
85
        $category = 'test';
86
        $language = 'en';
87
        $message = 'Message';
88
89
        $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

89
        $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...
90
            ->setMethods(['dispatch'])
91
            ->getMock();
92
93
        /**
94
         * @var $translator Translator
95
         */
96
        $translator = $this->getMockBuilder(Translator::class)
97
            ->setConstructorArgs(
98
                [
99
                    $eventDispatcher,
100
                    $this->createMessageReader([]),
101
                ]
102
            )
103
            ->enableProxyingToOriginalMethods()
104
            ->getMock();
105
106
        $translator->setDefaultLocale('de');
107
108
        $eventDispatcher
109
            ->expects($this->at(0))
110
            ->method('dispatch')
111
            ->with(new MissingTranslationEvent($category, $language, $message));
112
113
        $translator->translate($message, [], $category, $language);
114
    }
115
116
    public function getTranslations(): array
117
    {
118
        return [
119
            ['test', 'test', 'test', [], null],
120
            ['test {param}', 'translated {param}', 'translated param-value', ['param' => 'param-value'], null],
121
        ];
122
    }
123
124
    private function formatMessage(string $message, array $parameters): string
125
    {
126
        foreach ($parameters as $key => $value) {
127
            $message = str_replace('{' . $key . '}', $value, $message);
128
        }
129
130
        return $message;
131
    }
132
133
    private function createMessageReader(array $messages): MessageReaderInterface
134
    {
135
        return new class($messages) implements MessageReaderInterface {
136
            private array $messages;
137
138
            public function __construct(array $messages)
139
            {
140
                $this->messages = $messages;
141
            }
142
143
            public function all($context = null): array
144
            {
145
                return $this->messages;
146
            }
147
148
            public function one(string $id, $context = null): ?string
149
            {
150
                return $this->messages[$id] ?? null;
151
            }
152
153
            public function plural(string $id, int $count, $context = null): ?string
154
            {
155
                return $this->messages[$id] ?? null;
156
            }
157
        };
158
    }
159
}
160