|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\I18n; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
|
|
9
|
|
|
class TranslatorTest extends TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var Translator - System Under Test */ |
|
12
|
|
|
protected Translator $sut; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array[] |
|
16
|
|
|
*/ |
|
17
|
|
|
protected array $translationData = [ |
|
18
|
|
|
'foo' => [ |
|
19
|
|
|
'bar' => 'baz', |
|
20
|
|
|
'replacable' => 'baz %2$s %1$s', |
|
21
|
|
|
], |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
public function setUp(): void |
|
25
|
|
|
{ |
|
26
|
|
|
parent::setUp(); |
|
27
|
|
|
|
|
28
|
|
|
$this->sut = new Translator($this->translationData); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return array[] |
|
33
|
|
|
*/ |
|
34
|
|
|
public function translateProvider(): array |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
|
|
['foo:bar', [], 'baz'], |
|
38
|
|
|
['foo:replacable', ['second', 'first'], 'baz first second'], |
|
39
|
|
|
['foo:replacable', ['foo:bar', 'first'], 'baz first baz'], |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @dataProvider translateProvider |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $expression |
|
47
|
|
|
* @param array $args |
|
48
|
|
|
* @param string $expectedResult |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testTranslate(string $expression, array $args, string $expectedResult): void |
|
51
|
|
|
{ |
|
52
|
|
|
$actualResult = $this->sut->translate($expression, ...$args); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return array[] |
|
60
|
|
|
*/ |
|
61
|
|
|
public function canTranslateProvider(): array |
|
62
|
|
|
{ |
|
63
|
|
|
return [ |
|
64
|
|
|
['foo:bar', [], true], |
|
65
|
|
|
['foo', [], false], |
|
66
|
|
|
['foo:bar:baz', [], false], |
|
67
|
|
|
['foo:replacable', ['second', 'first'], true], |
|
68
|
|
|
['foo:replacable', ['foo:bar', 'first'], true], |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @dataProvider canTranslateProvider |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $expression |
|
76
|
|
|
* @param array $args |
|
77
|
|
|
* @param bool $expectedResult |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testCanTranslate(string $expression, array $args, bool $expectedResult): void |
|
80
|
|
|
{ |
|
81
|
|
|
$actualResult = $this->sut->canTranslate($expression, ...$args); |
|
82
|
|
|
|
|
83
|
|
|
$this->assertEquals($expectedResult, $actualResult); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|