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