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

TranslatorTest.php$0 ➔ one()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
1
<?php
2
3
namespace Yii\I18n\Tests;
4
5
use Phan\Plugin\PrintfCheckerPlugin\PrimitiveValue;
0 ignored issues
show
Bug introduced by
The type Phan\Plugin\PrintfCheckerPlugin\PrimitiveValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use PHPUnit\Framework\TestCase;
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Yiisoft\I18n\Event\MissingTranslationEvent;
9
use Yiisoft\I18n\MessageFormatterInterface;
10
use Yiisoft\I18n\MessageReaderInterface;
11
use Yiisoft\I18n\Translator\Translator;
12
13
final class TranslatorTest extends TestCase
14
{
15
    /**
16
     * @dataProvider getTranslations
17
     * @param string|null $message
18
     * @param string|null $translation
19
     * @param string|null $expected
20
     * @param array $parameters
21
     * @param string|null $category
22
     */
23
    public function testTranslation(
24
        ?string $message,
25
        ?string $translation,
26
        ?string $expected,
27
        array $parameters,
28
        ?string $category
29
    ): void {
30
        $messageReader = $this->createMessageReader([$message => $translation]);
31
32
        $messageFormatter = null;
33
        if ([] !== $parameters) {
34
            $messageFormatter = $this->getMockBuilder(MessageFormatterInterface::class)->getMock();
35
            $messageFormatter
36
                ->method('format')
37
                ->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

37
                ->willReturn($this->formatMessage(/** @scrutinizer ignore-type */ $translation, $parameters));
Loading history...
38
        }
39
40
        /**
41
         * @var $translator Translator
42
         */
43
        $translator = $this->getMockBuilder(Translator::class)
44
            ->setConstructorArgs(
45
                [
46
                    $this->createMock(EventDispatcherInterface::class),
47
                    $messageReader,
48
                    $messageFormatter
49
                ]
50
            )
51
            ->enableProxyingToOriginalMethods()
52
            ->getMock();
53
54
        $this->assertEquals($expected, $translator->translate($message, $parameters, $category));
55
    }
56
57
    public function testFallbackLocale(): void
58
    {
59
        $category = 'test';
60
        $message = 'test';
61
        $fallbackMessage = 'test de locale';
62
63
        $messageReader = $this->createMessageReader(['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
        $this->assertEquals($fallbackMessage, $translator->translate($message, [], $category, 'en'));
82
    }
83
84
    public function testMissingEventTriggered(): void
85
    {
86
        $category = 'test';
87
        $language = 'en';
88
        $message = 'Message';
89
90
        $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

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