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\MessageReaderInterface; |
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
|
|
|
$messageReader = $this->getMockBuilder(MessageReaderInterface::class) |
21
|
|
|
->getMock(); |
22
|
|
|
|
23
|
|
|
$translator = $this->getMockBuilder(Translator::class) |
24
|
|
|
->setConstructorArgs([ |
25
|
|
|
$this->createMock(EventDispatcherInterface::class), |
26
|
|
|
$messageReader, |
27
|
|
|
]) |
28
|
|
|
->enableProxyingToOriginalMethods() |
29
|
|
|
->getMock(); |
30
|
|
|
|
31
|
|
|
$messageReader->expects($this->once()) |
32
|
|
|
->method('all') |
33
|
|
|
->willReturn([$message => $translate]); |
34
|
|
|
|
35
|
|
|
$this->assertEquals($translate, $translator->translate($message)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testMissingEventTriggered() |
39
|
|
|
{ |
40
|
|
|
$category = 'test'; |
41
|
|
|
$language = 'en'; |
42
|
|
|
$message = 'Message'; |
43
|
|
|
|
44
|
|
|
$eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class) |
45
|
|
|
->setMethods(['dispatch']) |
46
|
|
|
->getMock(); |
47
|
|
|
|
48
|
|
|
$translator = $this->getMockBuilder(Translator::class) |
49
|
|
|
->setConstructorArgs([ |
50
|
|
|
$eventDispatcher, |
51
|
|
|
$this->createMock(MessageReaderInterface::class), |
52
|
|
|
]) |
53
|
|
|
->enableProxyingToOriginalMethods() |
54
|
|
|
->getMock(); |
55
|
|
|
|
56
|
|
|
$eventDispatcher |
57
|
|
|
->expects($this->once()) |
58
|
|
|
->method('dispatch') |
59
|
|
|
->with(new MissingTranslationEvent($category, $language, $message)); |
60
|
|
|
|
61
|
|
|
$translator->translate($message, $category, $language); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getTranslations(): array |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
[null, null], |
68
|
|
|
[1, 1], |
69
|
|
|
['test', 'test'], |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|