1313
1414namespace CodeIgniter \Commands \Translation ;
1515
16+ use CodeIgniter \Exceptions \LogicException ;
1617use CodeIgniter \Test \CIUnitTestCase ;
1718use CodeIgniter \Test \StreamFilterTrait ;
1819use Config \App ;
@@ -34,15 +35,17 @@ final class LocalizationSyncTest extends CIUnitTestCase
3435 * @var array<string, array<string,mixed>|string|null>
3536 */
3637 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 ' ,
4649 ],
4750 ],
4851 ];
@@ -51,10 +54,10 @@ protected function setUp(): void
5154 {
5255 parent ::setUp ();
5356
54- config (App::class)->supportedLocales = ['en ' , 'ru ' , 'test ' ];
57+ config (App::class)->supportedLocales = ['en ' , 'ru ' , 'de ' ];
5558
5659 self ::$ locale = Locale::getDefault ();
57- self ::$ languageTestPath = SUPPORTPATH . 'Language ' . DIRECTORY_SEPARATOR ;
60+ self ::$ languageTestPath = SUPPORTPATH . 'Language/ ' ;
5861 $ this ->makeLanguageFiles ();
5962 }
6063
@@ -67,9 +70,9 @@ protected function tearDown(): void
6770
6871 public function testSyncDefaultLocale (): void
6972 {
70- command ('lang:sync --target test ' );
73+ command ('lang:sync --target de ' );
7174
72- $ langFile = self ::$ languageTestPath . 'test /Sync.php ' ;
75+ $ langFile = self ::$ languageTestPath . 'de /Sync.php ' ;
7376
7477 $ this ->assertFileExists ($ langFile );
7578
@@ -81,9 +84,9 @@ public function testSyncDefaultLocale(): void
8184
8285 public function testSyncWithLocaleOption (): void
8386 {
84- command ('lang:sync --locale ru --target test ' );
87+ command ('lang:sync --locale ru --target de ' );
8588
86- $ langFile = self ::$ languageTestPath . 'test /Sync.php ' ;
89+ $ langFile = self ::$ languageTestPath . 'de /Sync.php ' ;
8790
8891 $ this ->assertFileExists ($ langFile );
8992
@@ -95,29 +98,21 @@ public function testSyncWithLocaleOption(): void
9598
9699 public function testSyncWithExistTranslation (): void
97100 {
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) ' ,
121116 ],
122117 ],
123118 ];
@@ -126,40 +121,83 @@ public function testSyncWithExistTranslation(): void
126121 <?php
127122
128123 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) ',
138133 ],
139134 ],
135+ 'empty_array' => [],
140136 ];
141137 TEXT_WRAP;
142138
143- file_put_contents ( self ::$ languageTestPath . 'test /Sync.php ' , $ lang ) ;
139+ $ langFile = self ::$ languageTestPath . 'de /Sync.php ' ;
144140
145- command ('lang:sync --target test ' );
141+ mkdir (self ::$ languageTestPath . 'de ' , 0755 );
142+ file_put_contents ($ langFile , $ lang );
146143
147- $ langFile = self :: $ languageTestPath . ' test/Sync.php ' ;
144+ command ( ' lang:sync --target de ' ) ;
148145
149146 $ this ->assertFileExists ($ langFile );
150147
151148 $ langKeys = include $ langFile ;
149+
152150 $ this ->assertIsArray ($ langKeys );
153- $ this ->assertSame ($ oldLangKeys , $ langKeys );
151+ $ this ->assertSame ($ expectedLangKeys , $ langKeys );
154152 }
155153
156154 public function testSyncWithIncorrectLocaleOption (): void
157155 {
158- command ('lang:sync --locale test_locale_incorrect --target test ' );
156+ command ('lang:sync --locale test_locale_incorrect --target de ' );
159157
160158 $ this ->assertStringContainsString ('is not supported ' , $ this ->getStreamFilterBuffer ());
161159 }
162160
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+
163201 public function testSyncWithIncorrectTargetOption (): void
164202 {
165203 command ('lang:sync --locale en --target test_locale_incorrect ' );
@@ -173,15 +211,17 @@ private function makeLanguageFiles(): void
173211 <?php
174212
175213 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...',
185225 ],
186226 ],
187227 ];
@@ -193,22 +233,21 @@ private function makeLanguageFiles(): void
193233
194234 private function clearGeneratedFiles (): void
195235 {
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+ ];
206241
207- foreach ($ files as $ file ) {
242+ foreach ($ files as $ file ) {
243+ if (is_file ($ file )) {
208244 unlink ($ file );
209245 }
246+ }
210247
211- rmdir (self ::$ languageTestPath . 'test ' );
248+ if (is_dir (self ::$ languageTestPath . 'de ' )) {
249+ delete_files (self ::$ languageTestPath . 'de ' );
250+ rmdir (self ::$ languageTestPath . 'de ' );
212251 }
213252 }
214253}
0 commit comments