1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use JsLocalization\Facades\JsLocalizationHelper; |
4
|
|
|
|
5
|
|
|
class JsLocalizationHelperTest extends TestCase |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
private $tmpFilePath; |
9
|
|
|
|
10
|
|
|
protected $additionalMessageKeys = [ |
11
|
|
|
'additional' => [ |
12
|
|
|
'message1', |
13
|
|
|
'message2' |
14
|
|
|
] |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
protected $additionalMessageKeysFlat = [ |
18
|
|
|
'additional.message1', 'additional.message2' |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
protected $testMessagesFlat = [ |
23
|
|
|
'en' => [ |
24
|
|
|
'test1' => "Text for test1", |
25
|
|
|
'prefix1' => [ |
26
|
|
|
'prefix2' => [ |
27
|
|
|
'test2' => "Text for test2", |
28
|
|
|
'test3' => "Text for test3" |
29
|
|
|
], |
30
|
|
|
'test4' => "Text for test4" |
31
|
|
|
] |
32
|
|
|
] |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
protected $testKeys = [ |
36
|
|
|
'test1', |
37
|
|
|
'prefix1' => [ |
38
|
|
|
'prefix2' => [ |
39
|
|
|
'test2', 'test3' |
40
|
|
|
], |
41
|
|
|
'test4' |
42
|
|
|
] |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
protected $testKeysFlat = [ |
46
|
|
|
'test1', |
47
|
|
|
'prefix1.prefix2.test2', |
48
|
|
|
'prefix1.prefix2.test3', |
49
|
|
|
'prefix1.test4' |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
protected function setUpTestMessagesFile($filePath) |
53
|
|
|
{ |
54
|
|
|
$fileContents = '<?php return ' . var_export($this->testMessagesFlat['en'], true) . ';'; |
55
|
|
|
file_put_contents($filePath, $fileContents); |
56
|
|
|
|
57
|
|
|
$prefix = preg_replace('/\.php$/i', '', basename($filePath)); |
58
|
|
|
|
59
|
|
|
return $prefix; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setUp() |
63
|
|
|
{ |
64
|
|
|
parent::setUp(); |
65
|
|
|
|
66
|
|
|
$this->tmpFilePath = tempnam('/tmp', ''); |
67
|
|
|
unlink($this->tmpFilePath); |
68
|
|
|
|
69
|
|
|
$this->tmpFilePath .= '.php'; |
70
|
|
|
touch($this->tmpFilePath); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function tearDown() |
74
|
|
|
{ |
75
|
|
|
unlink($this->tmpFilePath); |
76
|
|
|
|
77
|
|
|
parent::tearDown(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testResolveMessageKeyArray() |
81
|
|
|
{ |
82
|
|
|
$this->assertEquals($this->testKeysFlat, JsLocalizationHelper::resolveMessageKeyArray($this->testKeys)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testResolveMessageArrayToMessageKeys() |
86
|
|
|
{ |
87
|
|
|
$this->assertEquals($this->testKeysFlat, JsLocalizationHelper::resolveMessageArrayToMessageKeys($this->testMessagesFlat['en'])); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testAddingRetrieving() |
91
|
|
|
{ |
92
|
|
|
JsLocalizationHelper::addMessagesToExport($this->additionalMessageKeys); |
93
|
|
|
|
94
|
|
|
$this->assertEquals( |
95
|
|
|
$this->additionalMessageKeysFlat, |
96
|
|
|
JsLocalizationHelper::getAdditionalMessages() |
97
|
|
|
); |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
$this->addTestMessage('en', 'another', 'Another test text.'); |
101
|
|
|
|
102
|
|
|
JsLocalizationHelper::addMessagesToExport(['another']); |
103
|
|
|
|
104
|
|
|
$this->assertEquals( |
105
|
|
|
array_merge($this->additionalMessageKeysFlat, ['another']), |
106
|
|
|
JsLocalizationHelper::getAdditionalMessages() |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testEventBasedAdding() |
111
|
|
|
{ |
112
|
|
|
$additionalMessageKeys = $this->additionalMessageKeys; |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
Event::listen('JsLocalization.registerMessages', function() |
116
|
|
|
use($additionalMessageKeys) |
117
|
|
|
{ |
118
|
|
|
JsLocalizationHelper::addMessagesToExport($additionalMessageKeys); |
119
|
|
|
}); |
120
|
|
|
|
121
|
|
|
$this->assertEquals([], JsLocalizationHelper::getAdditionalMessages()); |
122
|
|
|
|
123
|
|
|
Event::fire('JsLocalization.registerMessages'); |
124
|
|
|
|
125
|
|
|
$this->assertEquals( |
126
|
|
|
$this->additionalMessageKeysFlat, |
127
|
|
|
JsLocalizationHelper::getAdditionalMessages() |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
$this->addTestMessage('en', 'another', 'Another test text.'); |
132
|
|
|
|
133
|
|
|
Event::listen('JsLocalization.registerMessages', function() |
134
|
|
|
{ |
135
|
|
|
JsLocalizationHelper::addMessagesToExport(['another']); |
136
|
|
|
}); |
137
|
|
|
|
138
|
|
|
Event::fire('JsLocalization.registerMessages'); |
139
|
|
|
|
140
|
|
|
$this->assertEquals( |
141
|
|
|
array_merge($this->additionalMessageKeysFlat, ['another']), |
142
|
|
|
JsLocalizationHelper::getAdditionalMessages() |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testAddMessageFileToExport() |
147
|
|
|
{ |
148
|
|
|
$prefix = 'xyz::' . $this->setUpTestMessagesFile($this->tmpFilePath); |
149
|
|
|
JsLocalizationHelper::addMessageFileToExport($this->tmpFilePath, 'xyz::'); |
150
|
|
|
|
151
|
|
|
// since we just tested the method using a prefix without the trailing '.' |
152
|
|
|
$prefix .= '.'; |
153
|
|
|
|
154
|
|
|
$testKeysFlat = $this->testKeysFlat; |
155
|
|
|
array_walk($testKeysFlat, function(&$key) use($prefix) |
156
|
|
|
{ |
157
|
|
|
$key = $prefix . $key; |
158
|
|
|
}); |
159
|
|
|
|
160
|
|
|
$this->assertEquals($testKeysFlat, JsLocalizationHelper::getAdditionalMessages()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testAddMessageFileToExportExceptionHandling() |
164
|
|
|
{ |
165
|
|
|
$filePath = "/tmp/x/y/z/does-not-exist"; |
166
|
|
|
|
167
|
|
|
$this->setExpectedException( |
168
|
|
|
'JsLocalization\Exceptions\FileNotFoundException', |
169
|
|
|
"File not found: $filePath" |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
JsLocalizationHelper::addMessageFileToExport($filePath, 'xyz::'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
} |
176
|
|
|
|