Passed
Pull Request — master (#19)
by
unknown
11:20
created

TranslatorTest::testFallbackLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 29
rs 9.7333
cc 1
nc 1
nop 0
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
class TranslatorTest extends TestCase
13
{
14
    /**
15
     * @dataProvider getTranslations
16
     * @param $message
17
     * @param $translate
18
     * @param $expected
19
     * @param $parameters
20
     * @param $category
21
     */
22
    public function testTranslation($message, $translate, $expected, $parameters, $category)
23
    {
24
        $messageReader = $this->getMockBuilder(MessageReaderInterface::class)
25
            ->getMock();
26
27
        $messageFormatter = null;
28
        if ([] !== $parameters) {
29
            $messageFormatter = $this->getMockBuilder(MessageFormatterInterface::class)
30
                ->getMock();
31
        }
32
33
        /**
34
         * @var $translator Translator
35
         */
36
        $translator = $this->getMockBuilder(Translator::class)
37
            ->setConstructorArgs(
38
                [
39
                    $this->createMock(EventDispatcherInterface::class),
40
                    $messageReader,
41
                    $messageFormatter
42
                ]
43
            )
44
            ->enableProxyingToOriginalMethods()
45
            ->getMock();
46
47
        $messageReader->expects($this->once())
48
            ->method('all')
49
            ->willReturn([$message => $translate]);
50
51
        if ($messageFormatter instanceof MessageFormatterInterface) {
52
            $messageFormatter->expects($this->once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Yiisoft\I18n\MessageFormatterInterface. ( Ignorable by Annotation )

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

52
            $messageFormatter->/** @scrutinizer ignore-call */ 
53
                               expects($this->once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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

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