mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-02 05:26:43 +00:00
Throw when list formatting fails
This commit is contained in:
@@ -451,7 +451,12 @@ final class IntlExtension extends AbstractExtension
|
||||
throw new RuntimeError('The "format_list" filter requires the "IntlListFormatter" class, which is available since PHP 8.5.');
|
||||
}
|
||||
|
||||
return $this->createListFormatter($locale, $type, $width)->format($strings);
|
||||
$formatter = $this->createListFormatter($locale, $type, $width);
|
||||
if (false === $ret = $formatter->format($strings)) {
|
||||
throw new RuntimeError(\sprintf('Unable to format the given list: %s', $formatter->getErrorMessage()));
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
private function createDateFormatter(?string $locale, ?string $dateFormat, ?string $timeFormat, string $pattern, ?\DateTimeZone $timezone, ?string $calendar): \IntlDateFormatter
|
||||
|
||||
@@ -13,6 +13,7 @@ namespace Twig\Extra\Intl\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Twig\Environment;
|
||||
use Twig\Error\RuntimeError;
|
||||
use Twig\Extension\CoreExtension;
|
||||
use Twig\Extra\Intl\IntlExtension;
|
||||
use Twig\Loader\ArrayLoader;
|
||||
@@ -223,4 +224,22 @@ class IntlExtensionTest extends TestCase
|
||||
$this->assertLessThanOrEqual(100, \count($cache));
|
||||
$this->assertGreaterThan(1, \count($cache));
|
||||
}
|
||||
|
||||
public function testFormatListThrowsOnFailure(): void
|
||||
{
|
||||
if (!class_exists('IntlListFormatter')) {
|
||||
$this->markTestSkipped('IntlListFormatter is not available.');
|
||||
}
|
||||
|
||||
$strings = ['Alice', "\xB1\x31"];
|
||||
$formatter = new \IntlListFormatter('en', \IntlListFormatter::TYPE_AND, \IntlListFormatter::WIDTH_WIDE);
|
||||
if (false !== $formatter->format($strings)) {
|
||||
$this->markTestSkipped('IntlListFormatter accepts the malformed UTF-8 input.');
|
||||
}
|
||||
|
||||
$this->expectException(RuntimeError::class);
|
||||
$this->expectExceptionMessage('Unable to format the given list: '.$formatter->getErrorMessage());
|
||||
|
||||
(new IntlExtension())->formatList($strings, locale: 'en');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user