Completed
Pull Request — master (#18)
by
unknown
11:20
created

TranslatorTest::getTranslations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 2
b 0
f 0
cc 1
nc 1
nop 0
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\Translator\TranslationsLoaderInterface;
9
use Yiisoft\I18n\Translator\Translator;
10
11
class TranslatorTest extends TestCase
12
{
13
    /**
14
     * @dataProvider getTranslations
15
     * @param $message
16
     * @param $translate
17
     */
18
    public function testTranslation($message, $translate)
19
    {
20
        $translationsLoader = $this->getMockBuilder(TranslationsLoaderInterface::class)
21
            ->setMethods(['load'])
22
            ->getMock();
23
24
        $translator = $this->getMockBuilder(Translator::class)
25
            ->setConstructorArgs([
26
                $this->createMock(EventDispatcherInterface::class),
27
                $translationsLoader,
28
            ])
29
            ->enableProxyingToOriginalMethods()
30
            ->getMock();
31
32
        $translationsLoader->expects($this->once())
33
            ->method('load')
34
            ->willReturn([$message => $translate]);
35
36
        $this->assertEquals($translate, $translator->translate($message));
37
    }
38
39
    public function testMissingEventTriggered()
40
    {
41
        $category = 'test';
42
        $language = 'en';
43
        $message = 'Message';
44
45
        $eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)
46
            ->setMethods(['dispatch'])
47
            ->getMock();
48
49
        $translator = $this->getMockBuilder(Translator::class)
50
            ->setConstructorArgs([
51
                $eventDispatcher,
52
                $this->createMock(TranslationsLoaderInterface::class),
53
            ])
54
            ->enableProxyingToOriginalMethods()
55
            ->getMock();
56
57
        $eventDispatcher
58
            ->expects($this->once())
59
            ->method('dispatch')
60
            ->with(new MissingTranslationEvent($category, $language, $message));
61
62
        $translator->translate($message, $category, $language);
63
    }
64
65
    public function getTranslations(): array
66
    {
67
        return [
68
            [null, null],
69
            [1, 1],
70
            ['test', 'test'],
71
        ];
72
    }
73
}
74