13
13
14
14
namespace CodeIgniter \Commands \Translation ;
15
15
16
+ use CodeIgniter \Exceptions \LogicException ;
16
17
use CodeIgniter \Test \CIUnitTestCase ;
17
18
use CodeIgniter \Test \StreamFilterTrait ;
18
19
use Config \App ;
@@ -34,15 +35,17 @@ final class LocalizationSyncTest extends CIUnitTestCase
34
35
* @var array<string, array<string,mixed>|string|null>
35
36
*/
36
37
private array $ expectedKeys = [
37
- 'a ' => 'Sync.a ' ,
38
- 'b ' => 'Sync.b ' ,
39
- 'c ' => 'Sync.c ' ,
40
- 'd ' => [],
41
- 'e ' => 'Sync.e ' ,
42
- 'f ' => [
43
- 'g ' => 'Sync.f.g ' ,
44
- 'h ' => [
45
- 'i ' => 'Sync.f.h.i ' ,
38
+ 'title ' => 'Sync.title ' ,
39
+ 'status ' => [
40
+ 'error ' => 'Sync.status.error ' ,
41
+ 'done ' => 'Sync.status.done ' ,
42
+ 'critical ' => 'Sync.status.critical ' ,
43
+ ],
44
+ 'description ' => 'Sync.description ' ,
45
+ 'empty_array ' => [],
46
+ 'more ' => [
47
+ 'nested ' => [
48
+ 'key ' => 'Sync.more.nested.key ' ,
46
49
],
47
50
],
48
51
];
@@ -51,10 +54,10 @@ protected function setUp(): void
51
54
{
52
55
parent ::setUp ();
53
56
54
- config (App::class)->supportedLocales = ['en ' , 'ru ' , 'test ' ];
57
+ config (App::class)->supportedLocales = ['en ' , 'ru ' , 'de ' ];
55
58
56
59
self ::$ locale = Locale::getDefault ();
57
- self ::$ languageTestPath = SUPPORTPATH . 'Language ' . DIRECTORY_SEPARATOR ;
60
+ self ::$ languageTestPath = SUPPORTPATH . 'Language/ ' ;
58
61
$ this ->makeLanguageFiles ();
59
62
}
60
63
@@ -67,9 +70,9 @@ protected function tearDown(): void
67
70
68
71
public function testSyncDefaultLocale (): void
69
72
{
70
- command ('lang:sync --target test ' );
73
+ command ('lang:sync --target de ' );
71
74
72
- $ langFile = self ::$ languageTestPath . 'test /Sync.php ' ;
75
+ $ langFile = self ::$ languageTestPath . 'de /Sync.php ' ;
73
76
74
77
$ this ->assertFileExists ($ langFile );
75
78
@@ -81,9 +84,9 @@ public function testSyncDefaultLocale(): void
81
84
82
85
public function testSyncWithLocaleOption (): void
83
86
{
84
- command ('lang:sync --locale ru --target test ' );
87
+ command ('lang:sync --locale ru --target de ' );
85
88
86
- $ langFile = self ::$ languageTestPath . 'test /Sync.php ' ;
89
+ $ langFile = self ::$ languageTestPath . 'de /Sync.php ' ;
87
90
88
91
$ this ->assertFileExists ($ langFile );
89
92
@@ -95,29 +98,21 @@ public function testSyncWithLocaleOption(): void
95
98
96
99
public function testSyncWithExistTranslation (): void
97
100
{
98
- // First run, add new keys
99
- command ('lang:sync --target test ' );
100
-
101
- $ langFile = self ::$ languageTestPath . 'test/Sync.php ' ;
102
-
103
- $ this ->assertFileExists ($ langFile );
104
-
105
- $ langKeys = include $ langFile ;
106
-
107
- $ this ->assertIsArray ($ langKeys );
108
- $ this ->assertSame ($ this ->expectedKeys , $ langKeys );
109
-
110
- // Second run, save old keys
111
- $ oldLangKeys = [
112
- 'a ' => 'old value 1 ' ,
113
- 'b ' => 2000 ,
114
- 'c ' => null ,
115
- 'd ' => [],
116
- 'e ' => '' ,
117
- 'f ' => [
118
- 'g ' => 'old value 2 ' ,
119
- 'h ' => [
120
- 'i ' => 'old value 3 ' ,
101
+ // Save old values and add new keys from "en/Sync.php"
102
+ // Add value from the old file "de/Sync.php" to new
103
+ // Right sort as in "en/Sync.php"
104
+ $ expectedLangKeys = [
105
+ 'title ' => 'Default title (old) ' ,
106
+ 'status ' => [
107
+ 'error ' => 'Error! (old) ' ,
108
+ 'done ' => 'Sync.status.done ' ,
109
+ 'critical ' => 'Critical! (old) ' ,
110
+ ],
111
+ 'description ' => 'Sync.description ' ,
112
+ 'empty_array ' => [],
113
+ 'more ' => [
114
+ 'nested ' => [
115
+ 'key ' => 'More nested key... (old) ' ,
121
116
],
122
117
],
123
118
];
@@ -126,40 +121,83 @@ public function testSyncWithExistTranslation(): void
126
121
<?php
127
122
128
123
return [
129
- 'a ' => 'old value 1',
130
- 'b ' => 2000 ,
131
- 'c' => null ,
132
- 'd' => [ ],
133
- 'e' => '',
134
- 'f' => [
135
- 'g' => 'old value 2',
136
- 'h ' => [
137
- 'i ' => 'old value 3 ',
124
+ 'status ' => [
125
+ 'critical ' => 'Critical! (old)' ,
126
+ 'error' => 'Error! (old)' ,
127
+ ],
128
+ 'skip' => 'skip this value ',
129
+ 'title' => 'Default title (old)',
130
+ 'more' => [
131
+ 'nested ' => [
132
+ 'key ' => 'More nested key... (old) ',
138
133
],
139
134
],
135
+ 'empty_array' => [],
140
136
];
141
137
TEXT_WRAP;
142
138
143
- file_put_contents ( self ::$ languageTestPath . 'test /Sync.php ' , $ lang ) ;
139
+ $ langFile = self ::$ languageTestPath . 'de /Sync.php ' ;
144
140
145
- command ('lang:sync --target test ' );
141
+ mkdir (self ::$ languageTestPath . 'de ' , 0755 );
142
+ file_put_contents ($ langFile , $ lang );
146
143
147
- $ langFile = self :: $ languageTestPath . ' test/Sync.php ' ;
144
+ command ( ' lang:sync --target de ' ) ;
148
145
149
146
$ this ->assertFileExists ($ langFile );
150
147
151
148
$ langKeys = include $ langFile ;
149
+
152
150
$ this ->assertIsArray ($ langKeys );
153
- $ this ->assertSame ($ oldLangKeys , $ langKeys );
151
+ $ this ->assertSame ($ expectedLangKeys , $ langKeys );
154
152
}
155
153
156
154
public function testSyncWithIncorrectLocaleOption (): void
157
155
{
158
- command ('lang:sync --locale test_locale_incorrect --target test ' );
156
+ command ('lang:sync --locale test_locale_incorrect --target de ' );
159
157
160
158
$ this ->assertStringContainsString ('is not supported ' , $ this ->getStreamFilterBuffer ());
161
159
}
162
160
161
+ public function testSyncWithNullableOriginalLangValue (): void
162
+ {
163
+ $ langWithNullValue = <<<'TEXT_WRAP'
164
+ <?php
165
+
166
+ return [
167
+ 'nullable' => null,
168
+ ];
169
+ TEXT_WRAP;
170
+
171
+ file_put_contents (self ::$ languageTestPath . self ::$ locale . '/SyncInvalid.php ' , $ langWithNullValue );
172
+ ob_get_flush ();
173
+
174
+ $ this ->expectException (LogicException::class);
175
+ $ this ->expectExceptionMessageMatches ('/Only "array" or "string" is allowed/ ' );
176
+
177
+ command ('lang:sync --target de ' );
178
+ }
179
+
180
+ public function testSyncWithIntegerOriginalLangValue (): void
181
+ {
182
+ $ this ->resetStreamFilterBuffer ();
183
+
184
+ $ langWithIntegerValue = <<<'TEXT_WRAP'
185
+ <?php
186
+
187
+ return [
188
+ 'integer' => 1000,
189
+ ];
190
+ TEXT_WRAP;
191
+
192
+ file_put_contents (self ::$ languageTestPath . self ::$ locale . '/SyncInvalid.php ' , $ langWithIntegerValue );
193
+ ob_get_flush ();
194
+
195
+ $ this ->expectException (LogicException::class);
196
+ $ this ->expectExceptionMessageMatches ('/Only "array" or "string" is allowed/ ' );
197
+
198
+ command ('lang:sync --target de ' );
199
+ }
200
+
163
201
public function testSyncWithIncorrectTargetOption (): void
164
202
{
165
203
command ('lang:sync --locale en --target test_locale_incorrect ' );
@@ -173,15 +211,17 @@ private function makeLanguageFiles(): void
173
211
<?php
174
212
175
213
return [
176
- 'a' => 'value 1',
177
- 'b' => 2,
178
- 'c' => null,
179
- 'd' => [],
180
- 'e' => '',
181
- 'f' => [
182
- 'g' => 'value 2',
183
- 'h' => [
184
- 'i' => 'value 3',
214
+ 'title' => 'Default title',
215
+ 'status' => [
216
+ 'error' => 'Error!',
217
+ 'done' => 'Done!',
218
+ 'critical' => 'Critical!',
219
+ ],
220
+ 'description' => '',
221
+ 'empty_array' => [],
222
+ 'more' => [
223
+ 'nested' => [
224
+ 'key' => 'More nested key...',
185
225
],
186
226
],
187
227
];
@@ -193,22 +233,21 @@ private function makeLanguageFiles(): void
193
233
194
234
private function clearGeneratedFiles (): void
195
235
{
196
- if (is_file (self ::$ languageTestPath . self ::$ locale . '/Sync.php ' )) {
197
- unlink (self ::$ languageTestPath . self ::$ locale . '/Sync.php ' );
198
- }
199
-
200
- if (is_file (self ::$ languageTestPath . 'ru/Sync.php ' )) {
201
- unlink (self ::$ languageTestPath . 'ru/Sync.php ' );
202
- }
203
-
204
- if (is_dir (self ::$ languageTestPath . 'test ' )) {
205
- $ files = glob (self ::$ languageTestPath . 'test/* ' , GLOB_MARK );
236
+ $ files = [
237
+ self ::$ languageTestPath . self ::$ locale . '/Sync.php ' ,
238
+ self ::$ languageTestPath . self ::$ locale . '/SyncInvalid.php ' ,
239
+ self ::$ languageTestPath . 'ru/Sync.php ' ,
240
+ ];
206
241
207
- foreach ($ files as $ file ) {
242
+ foreach ($ files as $ file ) {
243
+ if (is_file ($ file )) {
208
244
unlink ($ file );
209
245
}
246
+ }
210
247
211
- rmdir (self ::$ languageTestPath . 'test ' );
248
+ if (is_dir (self ::$ languageTestPath . 'de ' )) {
249
+ delete_files (self ::$ languageTestPath . 'de ' );
250
+ rmdir (self ::$ languageTestPath . 'de ' );
212
251
}
213
252
}
214
253
}
0 commit comments