Skip to content

Commit

Permalink
Merge branch '5.4' into 6.4
Browse files Browse the repository at this point in the history
* 5.4:
  minor #58472 CS: clean some whitespaces/indentation (keradus)
  Fix newline
  harden test to not depend on the system's configured default timezone
  [Form] Support intl.use_exceptions/error_level in NumberToLocalizedStringTransformer
  [ExpressionLanguage] Add missing test case for `Lexer`
  [FrameworkBundle] Fix passing request_stack to session.listener
  ensure session storages are opened in tests before destroying them
  [HttpKernel] Correctly merge `max-age`/`s-maxage` and `Expires` headers
  [Security][Validator] Check translations for Czech
  [Security] Fix serialized object representation in tests
  [DoctrineBridge] Fix risky test warnings
  • Loading branch information
xabbuh committed Oct 9, 2024
2 parents d4cb9a2 + c1974a7 commit 0fe17f9
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function reverseTransform(mixed $value): int|float|null
$value = str_replace(',', $decSep, $value);
}

//If the value is in exponential notation with a negative exponent, we end up with a float value too
// If the value is in exponential notation with a negative exponent, we end up with a float value too
if (str_contains($value, $decSep) || false !== stripos($value, 'e-')) {
$type = \NumberFormatter::TYPE_DOUBLE;
} else {
Expand All @@ -115,10 +115,14 @@ public function reverseTransform(mixed $value): int|float|null
: \NumberFormatter::TYPE_INT32;
}

$result = $formatter->parse($value, $type, $position);
try {
$result = @$formatter->parse($value, $type, $position);
} catch (\IntlException $e) {
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
}

if (intl_is_failure($formatter->getErrorCode())) {
throw new TransformationFailedException($formatter->getErrorMessage());
throw new TransformationFailedException($formatter->getErrorMessage(), $formatter->getErrorCode());
}

if ($result >= \PHP_INT_MAX || $result <= -\PHP_INT_MAX) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,15 @@ public function reverseTransform(mixed $value): int|float|null
$type = \PHP_INT_SIZE === 8 ? \NumberFormatter::TYPE_INT64 : \NumberFormatter::TYPE_INT32;
}

// replace normal spaces so that the formatter can read them
$result = $formatter->parse(str_replace(' ', "\xc2\xa0", $value), $type, $position);
try {
// replace normal spaces so that the formatter can read them
$result = @$formatter->parse(str_replace(' ', "\xc2\xa0", $value), $type, $position);
} catch (\IntlException $e) {
throw new TransformationFailedException($e->getMessage(), 0, $e);
}

if (intl_is_failure($formatter->getErrorCode())) {
throw new TransformationFailedException($formatter->getErrorMessage());
throw new TransformationFailedException($formatter->getErrorMessage(), $formatter->getErrorCode());
}

if (self::FRACTIONAL == $this->type) {
Expand Down
2 changes: 2 additions & 0 deletions Test/FormPerformanceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ private function doRunTest(): mixed
$this->fail(sprintf('expected running time: <= %s but was: %s', $this->maxRunningTime, $time));
}

$this->expectNotToPerformAssertions();

return $result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function tearDown(): void

if (\extension_loaded('intl')) {
ini_set('intl.use_exceptions', $this->initialTestCaseUseException);
ini_set('intl.error_level', $this->initialTestCaseUseException);
ini_set('intl.error_level', $this->initialTestCaseErrorLevel);
}
}

Expand Down Expand Up @@ -339,12 +339,11 @@ public function testReverseTransformFiveDigitYearsWithTimestamp()
$transformer->reverseTransform('20107-03-21 12:34:56');
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
{
if (!\extension_loaded('intl')) {
$this->markTestSkipped('intl extension is not loaded');
}

$errorLevel = ini_set('intl.error_level', \E_WARNING);

try {
Expand All @@ -356,12 +355,11 @@ public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
}
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithExceptions()
{
if (!\extension_loaded('intl')) {
$this->markTestSkipped('intl extension is not loaded');
}

$initialUseExceptions = ini_set('intl.use_exceptions', 1);

try {
Expand All @@ -373,12 +371,11 @@ public function testReverseTransformWrapsIntlErrorsWithExceptions()
}
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
{
if (!\extension_loaded('intl')) {
$this->markTestSkipped('intl extension is not loaded');
}

$initialUseExceptions = ini_set('intl.use_exceptions', 1);
$initialErrorLevel = ini_set('intl.error_level', \E_WARNING);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,29 @@ class NumberToLocalizedStringTransformerTest extends TestCase
{
private string $defaultLocale;

private $initialTestCaseUseException;
private $initialTestCaseErrorLevel;

protected function setUp(): void
{
// Normalize intl. configuration settings.
if (\extension_loaded('intl')) {
$this->initialTestCaseUseException = ini_set('intl.use_exceptions', 0);
$this->initialTestCaseErrorLevel = ini_set('intl.error_level', 0);
}

$this->defaultLocale = \Locale::getDefault();
\Locale::setDefault('en');
}

protected function tearDown(): void
{
\Locale::setDefault($this->defaultLocale);

if (\extension_loaded('intl')) {
ini_set('intl.use_exceptions', $this->initialTestCaseUseException);
ini_set('intl.error_level', $this->initialTestCaseErrorLevel);
}
}

public static function provideTransformations()
Expand Down Expand Up @@ -647,6 +661,56 @@ public function testReverseTransformENotation($output, $input)
$this->assertSame($output, $transformer->reverseTransform($input));
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
{
$errorLevel = ini_set('intl.error_level', \E_WARNING);

try {
$this->expectException(TransformationFailedException::class);
$transformer = new NumberToLocalizedStringTransformer();
$transformer->reverseTransform('invalid_number');
} finally {
ini_set('intl.error_level', $errorLevel);
}
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithExceptions()
{
$initialUseExceptions = ini_set('intl.use_exceptions', 1);

try {
$this->expectException(TransformationFailedException::class);
$transformer = new NumberToLocalizedStringTransformer();
$transformer->reverseTransform('invalid_number');
} finally {
ini_set('intl.use_exceptions', $initialUseExceptions);
}
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
{
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
$initialErrorLevel = ini_set('intl.error_level', \E_WARNING);

try {
$this->expectException(TransformationFailedException::class);
$transformer = new NumberToLocalizedStringTransformer();
$transformer->reverseTransform('invalid_number');
} finally {
ini_set('intl.use_exceptions', $initialUseExceptions);
ini_set('intl.error_level', $initialErrorLevel);
}
}

public static function eNotationProvider(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,29 @@ class PercentToLocalizedStringTransformerTest extends TestCase
{
private string $defaultLocale;

private $initialTestCaseUseException;
private $initialTestCaseErrorLevel;

protected function setUp(): void
{
// Normalize intl. configuration settings.
if (\extension_loaded('intl')) {
$this->initialTestCaseUseException = ini_set('intl.use_exceptions', 0);
$this->initialTestCaseErrorLevel = ini_set('intl.error_level', 0);
}

$this->defaultLocale = \Locale::getDefault();
\Locale::setDefault('en');
}

protected function tearDown(): void
{
\Locale::setDefault($this->defaultLocale);

if (\extension_loaded('intl')) {
ini_set('intl.use_exceptions', $this->initialTestCaseUseException);
ini_set('intl.error_level', $this->initialTestCaseErrorLevel);
}
}

public function testTransform()
Expand Down Expand Up @@ -475,6 +489,56 @@ public function testReverseTransformForHtml5FormatWithScale()

$this->assertEquals(0.1234, $transformer->reverseTransform('12.34'));
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
{
$errorLevel = ini_set('intl.error_level', \E_WARNING);

try {
$this->expectException(TransformationFailedException::class);
$transformer = new PercentToLocalizedStringTransformer(null, null, \NumberFormatter::ROUND_HALFUP);
$transformer->reverseTransform('invalid_number');
} finally {
ini_set('intl.error_level', $errorLevel);
}
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithExceptions()
{
$initialUseExceptions = ini_set('intl.use_exceptions', 1);

try {
$this->expectException(TransformationFailedException::class);
$transformer = new PercentToLocalizedStringTransformer(null, null, \NumberFormatter::ROUND_HALFUP);
$transformer->reverseTransform('invalid_number');
} finally {
ini_set('intl.use_exceptions', $initialUseExceptions);
}
}

/**
* @requires extension intl
*/
public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
{
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
$initialErrorLevel = ini_set('intl.error_level', \E_WARNING);

try {
$this->expectException(TransformationFailedException::class);
$transformer = new PercentToLocalizedStringTransformer(null, null, \NumberFormatter::ROUND_HALFUP);
$transformer->reverseTransform('invalid_number');
} finally {
ini_set('intl.use_exceptions', $initialUseExceptions);
ini_set('intl.error_level', $initialErrorLevel);
}
}
}

class PercentToLocalizedStringTransformerWithoutGrouping extends PercentToLocalizedStringTransformer
Expand Down

0 comments on commit 0fe17f9

Please sign in to comment.