Skip to content

Commit 03a81b6

Browse files
committed
test: Add test for lang:find command
1 parent 32c5307 commit 03a81b6

5 files changed

Lines changed: 359 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\Support\Services\Translation;
13+
14+
class TranslationFour
15+
{
16+
public function list()
17+
{
18+
$translationOne1 = lang('TranslationOne.title');
19+
$translationOne5 = lang('TranslationOne.last_operation_success');
20+
21+
$translationThree1 = lang('TranslationThree.alerts.created');
22+
$translationThree2 = lang('TranslationThree.alerts.failed_insert');
23+
24+
$translationThree5 = lang('TranslationThree.formFields.new.name');
25+
$translationThree7 = lang('TranslationThree.formFields.new.short_tag');
26+
27+
$translationFour1 = lang('Translation-Four.dashed.key-with-dash');
28+
$translationFour2 = lang('Translation-Four.dashed.key-with-dash-two');
29+
}
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\Support\Services\Translation;
13+
14+
class TranslationOne
15+
{
16+
public function list()
17+
{
18+
$translationOne1 = lang('TranslationOne.title');
19+
$translationOne2 = lang('TranslationOne.DESCRIPTION');
20+
$translationOne3 = lang('TranslationOne.metaTags');
21+
$translationOne4 = lang('TranslationOne.Copyright');
22+
$translationOne5 = lang('TranslationOne.last_operation_success');
23+
24+
$translationThree1 = lang('TranslationThree.alerts.created');
25+
$translationThree2 = lang('TranslationThree.alerts.failed_insert');
26+
$translationThree3 = lang('TranslationThree.alerts.Updated');
27+
$translationThree4 = lang('TranslationThree.alerts.DELETED');
28+
29+
$translationThree5 = lang('TranslationThree.formFields.new.name');
30+
$translationThree6 = lang('TranslationThree.formFields.new.TEXT');
31+
$translationThree7 = lang('TranslationThree.formFields.new.short_tag');
32+
$translationThree8 = lang('TranslationThree.formFields.edit.name');
33+
$translationThree9 = lang('TranslationThree.formFields.edit.TEXT');
34+
$translationThree10 = lang('TranslationThree.formFields.edit.short_tag');
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\Support\Services\Translation;
13+
14+
class TranslationThree
15+
{
16+
public function list()
17+
{
18+
$translationOne1 = lang('TranslationOne.title');
19+
$translationOne2 = lang('TranslationOne.DESCRIPTION');
20+
$translationOne6 = lang('TranslationOne.subTitle');
21+
$translationOne7 = lang('TranslationOne.overflow_style');
22+
23+
$translationThree1 = lang('TranslationThree.alerts.created');
24+
$translationThree2 = lang('TranslationThree.alerts.failed_insert');
25+
26+
$translationThree5 = lang('TranslationThree.formFields.new.name');
27+
$translationThree6 = lang('TranslationThree.formFields.new.TEXT');
28+
$translationThree7 = lang('TranslationThree.formFields.new.short_tag');
29+
30+
$translationThree11 = lang('TranslationThree.alerts.CANCELED');
31+
$translationThree12 = lang('TranslationThree.alerts.missing_keys');
32+
33+
$translationThree13 = lang('TranslationThree.formErrors.edit.empty_name');
34+
$translationThree14 = lang('TranslationThree.formErrors.edit.INVALID_TEXT');
35+
$translationThree15 = lang('TranslationThree.formErrors.edit.missing_short_tag');
36+
}
37+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\Support\Services\Translation;
13+
14+
class TranslationTwo
15+
{
16+
public function list()
17+
{
18+
$langKey = 'TranslationTwo.error_key';
19+
20+
/**
21+
* Error language keys
22+
*/
23+
$translationError1 = lang('TranslationTwo');
24+
$translationError2 = lang(' ');
25+
$translationError3 = lang('');
26+
$translationError4 = lang('.invalid_key');
27+
$translationError5 = lang('TranslationTwo.');
28+
$translationError6 = lang('TranslationTwo...');
29+
$translationError7 = lang('..invalid_nested_key..');
30+
31+
/**
32+
* Empty in comments lang('') lang(' ')
33+
*/
34+
}
35+
}
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Commands\Translation;
13+
14+
use CodeIgniter\Test\CIUnitTestCase;
15+
use CodeIgniter\Test\StreamFilterTrait;
16+
use Config\App;
17+
use Config\Services;
18+
use Locale;
19+
20+
/**
21+
* @group Others
22+
*
23+
* @internal
24+
*/
25+
final class LocalizationFinderTest extends CIUnitTestCase
26+
{
27+
use StreamFilterTrait;
28+
29+
private static string $locale;
30+
private static string $languageTestPath;
31+
32+
protected function setUp(): void
33+
{
34+
parent::setUp();
35+
self::$locale = Locale::getDefault();
36+
self::$languageTestPath = SUPPORTPATH . 'Language/';
37+
$this->clearGeneratedFiles();
38+
}
39+
40+
public function testUpdateDefaultLocale(): void
41+
{
42+
@mkdir(self::$languageTestPath . self::$locale, 0777, true);
43+
Services::commands()->run('lang:find', [
44+
'dir' => 'Services/Translation',
45+
]);
46+
$this->realizeAssertion();
47+
$this->clearGeneratedFiles();
48+
}
49+
50+
public function testUpdateWithLocaleOption(): void
51+
{
52+
self::$locale = config(App::class)->supportedLocales[0];
53+
@mkdir(self::$languageTestPath . self::$locale, 0777, true);
54+
Services::commands()->run('lang:find', [
55+
'dir' => 'Services/Translation',
56+
'locale' => self::$locale,
57+
]);
58+
$this->realizeAssertion();
59+
$this->clearGeneratedFiles();
60+
}
61+
62+
public function testUpdateWithIncorrectLocaleOption(): void
63+
{
64+
self::$locale = 'test_locale_incorrect';
65+
@mkdir(self::$languageTestPath . self::$locale, 0777, true);
66+
$status = Services::commands()->run('lang:find', [
67+
'dir' => 'Services/Translation',
68+
'locale' => self::$locale,
69+
]);
70+
$this->assertSame($status, -1);
71+
$this->clearGeneratedFiles();
72+
}
73+
74+
public function testUpdateWithEmptyDirOption(): void
75+
{
76+
@mkdir(self::$languageTestPath . self::$locale, 0777, true);
77+
Services::commands()->run('lang:find', []);
78+
$this->realizeAssertion();
79+
$this->clearGeneratedFiles();
80+
}
81+
82+
public function testUpdateWithIncorrectDirOption(): void
83+
{
84+
@mkdir(self::$languageTestPath . self::$locale, 0777, true);
85+
$status = Services::commands()->run('lang:find', [
86+
'dir' => 'Services/Translation/NotExistFolder',
87+
]);
88+
$this->assertSame($status, -1);
89+
$this->clearGeneratedFiles();
90+
}
91+
92+
public function testShowNewTranslation(): void
93+
{
94+
@mkdir(self::$languageTestPath . self::$locale, 0777, true);
95+
Services::commands()->run('lang:find', [
96+
'dir' => 'Services/Translation',
97+
'show-new' => null,
98+
]);
99+
$this->assertStringContainsString($this->getActualTableWithNewKeys(), $this->getStreamFilterBuffer());
100+
$this->clearGeneratedFiles();
101+
}
102+
103+
private function getActualTranslationOneKeys(): array
104+
{
105+
return [
106+
'title' => 'TranslationOne.title',
107+
'DESCRIPTION' => 'TranslationOne.DESCRIPTION',
108+
'subTitle' => 'TranslationOne.subTitle',
109+
'overflow_style' => 'TranslationOne.overflow_style',
110+
'metaTags' => 'TranslationOne.metaTags',
111+
'Copyright' => 'TranslationOne.Copyright',
112+
'last_operation_success' => 'TranslationOne.last_operation_success',
113+
];
114+
}
115+
116+
private function getActualTranslationThreeKeys(): array
117+
{
118+
return [
119+
'alerts' => [
120+
'created' => 'TranslationThree.alerts.created',
121+
'failed_insert' => 'TranslationThree.alerts.failed_insert',
122+
'CANCELED' => 'TranslationThree.alerts.CANCELED',
123+
'missing_keys' => 'TranslationThree.alerts.missing_keys',
124+
'Updated' => 'TranslationThree.alerts.Updated',
125+
'DELETED' => 'TranslationThree.alerts.DELETED',
126+
],
127+
'formFields' => [
128+
'new' => [
129+
'name' => 'TranslationThree.formFields.new.name',
130+
'TEXT' => 'TranslationThree.formFields.new.TEXT',
131+
'short_tag' => 'TranslationThree.formFields.new.short_tag',
132+
],
133+
'edit' => [
134+
'name' => 'TranslationThree.formFields.edit.name',
135+
'TEXT' => 'TranslationThree.formFields.edit.TEXT',
136+
'short_tag' => 'TranslationThree.formFields.edit.short_tag',
137+
],
138+
],
139+
'formErrors' => [
140+
'edit' => [
141+
'empty_name' => 'TranslationThree.formErrors.edit.empty_name',
142+
'INVALID_TEXT' => 'TranslationThree.formErrors.edit.INVALID_TEXT',
143+
'missing_short_tag' => 'TranslationThree.formErrors.edit.missing_short_tag',
144+
],
145+
],
146+
];
147+
}
148+
149+
private function getActualTranslationFourKeys(): array
150+
{
151+
return [
152+
'dashed' => [
153+
'key-with-dash' => 'Translation-Four.dashed.key-with-dash',
154+
'key-with-dash-two' => 'Translation-Four.dashed.key-with-dash-two',
155+
],
156+
];
157+
}
158+
159+
private function getActualTableWithNewKeys(): string
160+
{
161+
return <<<'TEXT_WRAP'
162+
+------------------+----------------------------------------------------+
163+
| File | Key |
164+
+------------------+----------------------------------------------------+
165+
| Translation-Four | Translation-Four.dashed.key-with-dash |
166+
| Translation-Four | Translation-Four.dashed.key-with-dash-two |
167+
| TranslationOne | TranslationOne.Copyright |
168+
| TranslationOne | TranslationOne.DESCRIPTION |
169+
| TranslationOne | TranslationOne.last_operation_success |
170+
| TranslationOne | TranslationOne.metaTags |
171+
| TranslationOne | TranslationOne.overflow_style |
172+
| TranslationOne | TranslationOne.subTitle |
173+
| TranslationOne | TranslationOne.title |
174+
| TranslationThree | TranslationThree.alerts.CANCELED |
175+
| TranslationThree | TranslationThree.alerts.DELETED |
176+
| TranslationThree | TranslationThree.alerts.Updated |
177+
| TranslationThree | TranslationThree.alerts.created |
178+
| TranslationThree | TranslationThree.alerts.failed_insert |
179+
| TranslationThree | TranslationThree.alerts.missing_keys |
180+
| TranslationThree | TranslationThree.formErrors.edit.INVALID_TEXT |
181+
| TranslationThree | TranslationThree.formErrors.edit.empty_name |
182+
| TranslationThree | TranslationThree.formErrors.edit.missing_short_tag |
183+
| TranslationThree | TranslationThree.formFields.edit.TEXT |
184+
| TranslationThree | TranslationThree.formFields.edit.name |
185+
| TranslationThree | TranslationThree.formFields.edit.short_tag |
186+
| TranslationThree | TranslationThree.formFields.new.TEXT |
187+
| TranslationThree | TranslationThree.formFields.new.name |
188+
| TranslationThree | TranslationThree.formFields.new.short_tag |
189+
+------------------+----------------------------------------------------+
190+
TEXT_WRAP;
191+
}
192+
193+
private function realizeAssertion(): void
194+
{
195+
$this->assertFileExists(self::$languageTestPath . self::$locale . '/TranslationOne.php');
196+
$this->assertFileExists(self::$languageTestPath . self::$locale . '/TranslationThree.php');
197+
198+
$translationOneKeys = require self::$languageTestPath . self::$locale . '/TranslationOne.php';
199+
$translationThreeKeys = require self::$languageTestPath . self::$locale . '/TranslationThree.php';
200+
$translationFourKeys = require self::$languageTestPath . self::$locale . '/Translation-Four.php';
201+
202+
$this->assertSame($translationOneKeys, $this->getActualTranslationOneKeys());
203+
$this->assertSame($translationThreeKeys, $this->getActualTranslationThreeKeys());
204+
$this->assertSame($translationFourKeys, $this->getActualTranslationFourKeys());
205+
}
206+
207+
private function clearGeneratedFiles(): void
208+
{
209+
if (is_file(self::$languageTestPath . self::$locale . '/TranslationOne.php')) {
210+
unlink(self::$languageTestPath . self::$locale . '/TranslationOne.php');
211+
}
212+
213+
if (is_file(self::$languageTestPath . self::$locale . '/TranslationThree.php')) {
214+
unlink(self::$languageTestPath . self::$locale . '/TranslationThree.php');
215+
}
216+
217+
if (is_file(self::$languageTestPath . self::$locale . '/Translation-Four.php')) {
218+
unlink(self::$languageTestPath . self::$locale . '/Translation-Four.php');
219+
}
220+
}
221+
}

0 commit comments

Comments
 (0)