|
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
|
|
|
|