|
16 | 16 | */ |
17 | 17 | class IconvTransliterationTest extends TestCase |
18 | 18 | { |
| 19 | + /** |
| 20 | + * Ensure iconv is available and locale supports transliteration. |
| 21 | + */ |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + if (!function_exists('iconv')) { |
| 25 | + $this->markTestSkipped('iconv() is not available.'); |
| 26 | + } |
| 27 | + |
| 28 | + $currentLocale = setlocale(LC_CTYPE, 0); |
| 29 | + if (in_array($currentLocale, ['C', 'POSIX'], true)) { |
| 30 | + // Attempt to change the locale to something compatible |
| 31 | + $fallbackLocales = ['en_US.UTF-8', 'C.UTF-8', 'de_DE.UTF-8']; |
| 32 | + foreach ($fallbackLocales as $locale) { |
| 33 | + if (setlocale(LC_CTYPE, $locale) !== false) { |
| 34 | + return; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Still stuck in C/POSIX? Skip |
| 39 | + $this->markTestSkipped("Unsupported locale for iconv transliteration: $currentLocale"); |
| 40 | + } |
| 41 | + } |
19 | 42 |
|
20 | 43 | /** |
21 | 44 | * Excerpt from http://php.net/manual/en/function.iconv.php#74101 |
@@ -47,13 +70,16 @@ public static function iconvTransliterationSlugProvider() |
47 | 70 | /** |
48 | 71 | * @dataProvider iconvTransliterationSlugProvider |
49 | 72 | */ |
50 | | - public function testIconvTransliteration($in, $out) |
| 73 | + public function testIconvTransliteration(string $input, string $expected): void |
51 | 74 | { |
52 | | - if (!function_exists('iconv')) { |
53 | | - $this->markTestSkipped(); |
| 75 | + $translit = iconv('utf-8', 'us-ascii//TRANSLIT', $input); |
| 76 | + |
| 77 | + // If transliteration results in all question marks, skip (locale likely broken) |
| 78 | + if (preg_match('/^\?+$/', $translit) && $input !== str_repeat('?', strlen($input))) { |
| 79 | + $this->markTestSkipped('iconv() returned only question marks — possibly due to locale issues.'); |
54 | 80 | } |
55 | | - $translit = iconv('utf-8', 'us-ascii//TRANSLIT', $in); |
56 | | - $this->assertEquals($out, $translit, 'iconv transliteration behaves as expected'); |
| 81 | + |
| 82 | + $this->assertEquals($expected, $translit, 'iconv transliteration behaves as expected'); |
57 | 83 | } |
58 | 84 |
|
59 | 85 | } |
0 commit comments