Skip to content

Commit

Permalink
Merge branch '6.4' into 7.1
Browse files Browse the repository at this point in the history
* 6.4:
  [Form] Remove unnecessary imports
  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
  [Doctrine][Messenger] Use common sequence name to get id from Oracle
  [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
  [Serializer] Fix `ObjectNormalizer` gives warnings on normalizing with public static property
  [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
  [Serializer] Catch `NotNormalizableValueException` for variadic parameters
  • Loading branch information
xabbuh committed Oct 9, 2024
2 parents e88613a + 0fe17f9 commit 7a48dda
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,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 @@ -113,10 +113,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
4 changes: 2 additions & 2 deletions Test/FormPerformanceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Symfony\Component\Form\Test;

use Symfony\Component\Form\Tests\VersionAwareTest;

/**
* Base class for performance tests.
*
Expand All @@ -35,6 +33,8 @@ protected function runTest(): 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
1 change: 0 additions & 1 deletion Tests/Extension/Core/Type/BaseTypeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Form\Tests\VersionAwareTest;

/**
* @author Bernhard Schussek <[email protected]>
Expand Down

0 comments on commit 7a48dda

Please sign in to comment.