toBe('') ->and(StringEncoder::toUtf8($withBom, 'UTF-8'))->toBe('Bonjour') ->and(StringEncoder::toUtf8($withBom, 'UTF-8', stripBom: false))->toBe($withBom); }); it('throws ValueError for invalid UTF-8 or unknown encoding in toUtf8', function (): void { // GIVEN $invalidUtf8 = "\xFF\xFE"; // WHEN / THEN expect(static fn (): string => StringEncoder::toUtf8($invalidUtf8, 'UTF-8')) ->toThrow(\ValueError::class) ->and(static fn (): string => StringEncoder::toUtf8('abc', 'UNKNOWN-ENC')) ->toThrow(\ValueError::class); }); it('converts Windows-1252 special bytes via aliases', function (): void { // GIVEN $bytes = "\x80\x92\x9C\xE9"; // WHEN / THEN expect(StringEncoder::toUtf8($bytes, 'win1252'))->toBe("€\u{2019}œé") ->and(StringEncoder::toUtf8($bytes, 'cp1252'))->toBe("€\u{2019}œé") ->and(StringEncoder::toUtf8($bytes, 'latin-1'))->toBe("€\u{2019}œé") ->and(StringEncoder::toUtf8($bytes, ' ISO-8859-1 '))->toBe("€\u{2019}œé"); }); it('converts ISO-8859-15 override bytes to UTF-8', function (): void { // GIVEN $bytes = "\xA4\xA6\xA8\xB4\xB8\xBC\xBD\xBE"; // WHEN $result = StringEncoder::toUtf8($bytes, 'ISO-8859-15'); // THEN expect($result)->toBe('€ŠšŽžŒœŸ'); }); it('detects and converts to UTF-8 with fallback encoding and BOM handling', function (): void { // GIVEN $validUtf8WithBom = "\xEF\xBB\xBFCafé"; $rawWin1252 = "\x80\xE9"; $rawLatin9 = "\xA4\xBC"; // WHEN / THEN expect(StringEncoder::detectAndConvertToUtf8(''))->toBe('') ->and(StringEncoder::detectAndConvertToUtf8($validUtf8WithBom))->toBe('Café') ->and(StringEncoder::detectAndConvertToUtf8($rawWin1252))->toBe('€é') ->and(StringEncoder::detectAndConvertToUtf8($rawLatin9, 'ISO-8859-15'))->toBe('€Œ') ->and(static fn (): string => StringEncoder::detectAndConvertToUtf8('abc', 'UTF-8')) ->toThrow(\ValueError::class) ->and(static fn (): string => StringEncoder::detectAndConvertToUtf8('abc', 'UNKNOWN')) ->toThrow(\ValueError::class); }); it('converts arrays of strings and lists supported encodings', function (): void { // GIVEN $items = ["\x80", "\xE9"]; // WHEN $converted = StringEncoder::arrayToUtf8($items, 'Windows-1252'); $supported = StringEncoder::supportedEncodings(); // THEN expect($converted)->toBe(['€', 'é']) ->and($supported)->toContain('UTF-8', 'Windows-1252', 'ISO-8859-15', 'win1252', 'latin-9'); });