|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Commands\Translation; |
| 15 | + |
| 16 | +use CodeIgniter\Test\CIUnitTestCase; |
| 17 | +use CodeIgniter\Test\StreamFilterTrait; |
| 18 | +use Config\App; |
| 19 | +use Locale; |
| 20 | +use PHPUnit\Framework\Attributes\Group; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +#[Group('Others')] |
| 26 | +final class LocalizationSyncTest extends CIUnitTestCase |
| 27 | +{ |
| 28 | + use StreamFilterTrait; |
| 29 | + |
| 30 | + private static string $locale; |
| 31 | + private static string $languageTestPath; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var array<string, array<string,mixed>|string|null> |
| 35 | + */ |
| 36 | + 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', |
| 46 | + ], |
| 47 | + ], |
| 48 | + ]; |
| 49 | + |
| 50 | + protected function setUp(): void |
| 51 | + { |
| 52 | + parent::setUp(); |
| 53 | + |
| 54 | + config(App::class)->supportedLocales = ['en', 'ru', 'test']; |
| 55 | + |
| 56 | + self::$locale = Locale::getDefault(); |
| 57 | + self::$languageTestPath = SUPPORTPATH . 'Language' . DIRECTORY_SEPARATOR; |
| 58 | + $this->makeLanguageFiles(); |
| 59 | + } |
| 60 | + |
| 61 | + protected function tearDown(): void |
| 62 | + { |
| 63 | + parent::tearDown(); |
| 64 | + |
| 65 | + $this->clearGeneratedFiles(); |
| 66 | + } |
| 67 | + |
| 68 | + public function testSyncDefaultLocale(): void |
| 69 | + { |
| 70 | + command('lang:sync --target test'); |
| 71 | + |
| 72 | + $langFile = self::$languageTestPath . 'test/Sync.php'; |
| 73 | + |
| 74 | + $this->assertFileExists($langFile); |
| 75 | + |
| 76 | + $langKeys = include $langFile; |
| 77 | + |
| 78 | + $this->assertIsArray($langKeys); |
| 79 | + $this->assertSame($this->expectedKeys, $langKeys); |
| 80 | + } |
| 81 | + |
| 82 | + public function testSyncWithLocaleOption(): void |
| 83 | + { |
| 84 | + command('lang:sync --locale ru --target test'); |
| 85 | + |
| 86 | + $langFile = self::$languageTestPath . 'test/Sync.php'; |
| 87 | + |
| 88 | + $this->assertFileExists($langFile); |
| 89 | + |
| 90 | + $langKeys = include $langFile; |
| 91 | + |
| 92 | + $this->assertIsArray($langKeys); |
| 93 | + $this->assertSame($this->expectedKeys, $langKeys); |
| 94 | + } |
| 95 | + |
| 96 | + public function testSyncWithExistTranslation(): void |
| 97 | + { |
| 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', |
| 121 | + ], |
| 122 | + ], |
| 123 | + ]; |
| 124 | + |
| 125 | + $lang = <<<'TEXT_WRAP' |
| 126 | + <?php |
| 127 | +
|
| 128 | + 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', |
| 138 | + ], |
| 139 | + ], |
| 140 | + ]; |
| 141 | + TEXT_WRAP; |
| 142 | + |
| 143 | + file_put_contents(self::$languageTestPath . 'test/Sync.php', $lang); |
| 144 | + |
| 145 | + command('lang:sync --target test'); |
| 146 | + |
| 147 | + $langFile = self::$languageTestPath . 'test/Sync.php'; |
| 148 | + |
| 149 | + $this->assertFileExists($langFile); |
| 150 | + |
| 151 | + $langKeys = include $langFile; |
| 152 | + $this->assertIsArray($langKeys); |
| 153 | + $this->assertSame($oldLangKeys, $langKeys); |
| 154 | + } |
| 155 | + |
| 156 | + public function testSyncWithIncorrectLocaleOption(): void |
| 157 | + { |
| 158 | + command('lang:sync --locale test_locale_incorrect --target test'); |
| 159 | + |
| 160 | + $this->assertStringContainsString('is not supported', $this->getStreamFilterBuffer()); |
| 161 | + } |
| 162 | + |
| 163 | + public function testSyncWithIncorrectTargetOption(): void |
| 164 | + { |
| 165 | + command('lang:sync --locale en --target test_locale_incorrect'); |
| 166 | + |
| 167 | + $this->assertStringContainsString('is not supported', $this->getStreamFilterBuffer()); |
| 168 | + } |
| 169 | + |
| 170 | + private function makeLanguageFiles(): void |
| 171 | + { |
| 172 | + $lang = <<<'TEXT_WRAP' |
| 173 | + <?php |
| 174 | +
|
| 175 | + 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', |
| 185 | + ], |
| 186 | + ], |
| 187 | + ]; |
| 188 | + TEXT_WRAP; |
| 189 | + |
| 190 | + file_put_contents(self::$languageTestPath . self::$locale . '/Sync.php', $lang); |
| 191 | + file_put_contents(self::$languageTestPath . 'ru/Sync.php', $lang); |
| 192 | + } |
| 193 | + |
| 194 | + private function clearGeneratedFiles(): void |
| 195 | + { |
| 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); |
| 206 | + |
| 207 | + foreach ($files as $file) { |
| 208 | + unlink($file); |
| 209 | + } |
| 210 | + |
| 211 | + rmdir(self::$languageTestPath . 'test'); |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments