From 92e1f531edb70a1530920ad932a8fb4b680204b5 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Mon, 30 Sep 2024 21:28:44 +0200 Subject: [PATCH 1/4] [DoctrineBridge] Fix risky test warnings --- Test/FormPerformanceTestCase.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Test/FormPerformanceTestCase.php b/Test/FormPerformanceTestCase.php index 8c0284ebf..e4329150a 100644 --- a/Test/FormPerformanceTestCase.php +++ b/Test/FormPerformanceTestCase.php @@ -45,6 +45,8 @@ private function doRunTest() $this->fail(sprintf('expected running time: <= %s but was: %s', $this->maxRunningTime, $time)); } + $this->expectNotToPerformAssertions(); + return $result; } From 5771ce9d999dcb43627b68e7344d51a5e20bf48f Mon Sep 17 00:00:00 2001 From: Bram Leeda Date: Thu, 3 Oct 2024 20:27:26 +0200 Subject: [PATCH 2/4] [Form] Support intl.use_exceptions/error_level in NumberToLocalizedStringTransformer --- .../NumberToLocalizedStringTransformer.php | 8 ++- .../PercentToLocalizedStringTransformer.php | 10 ++- ...teTimeToLocalizedStringTransformerTest.php | 23 +++---- ...NumberToLocalizedStringTransformerTest.php | 64 +++++++++++++++++++ ...ercentToLocalizedStringTransformerTest.php | 64 +++++++++++++++++++ 5 files changed, 151 insertions(+), 18 deletions(-) diff --git a/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php b/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php index d407e8858..bd9f796e1 100644 --- a/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php +++ b/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php @@ -152,10 +152,14 @@ public function reverseTransform($value) : \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) { diff --git a/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php b/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php index fdeed2231..b716e436e 100644 --- a/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php +++ b/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php @@ -139,11 +139,15 @@ public function reverseTransform($value) $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) { diff --git a/Tests/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php b/Tests/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php index 8a37707e7..21f5c5079 100644 --- a/Tests/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php +++ b/Tests/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php @@ -56,7 +56,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); } } @@ -341,12 +341,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 { @@ -358,12 +357,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 { @@ -375,12 +373,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); diff --git a/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php b/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php index f5246e222..f40d447f4 100644 --- a/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php +++ b/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php @@ -20,8 +20,17 @@ class NumberToLocalizedStringTransformerTest extends TestCase { private $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'); } @@ -29,6 +38,11 @@ protected function setUp(): void 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() @@ -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 [ diff --git a/Tests/Extension/Core/DataTransformer/PercentToLocalizedStringTransformerTest.php b/Tests/Extension/Core/DataTransformer/PercentToLocalizedStringTransformerTest.php index 6afa4c35d..161aa81ca 100644 --- a/Tests/Extension/Core/DataTransformer/PercentToLocalizedStringTransformerTest.php +++ b/Tests/Extension/Core/DataTransformer/PercentToLocalizedStringTransformerTest.php @@ -23,8 +23,17 @@ class PercentToLocalizedStringTransformerTest extends TestCase private $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'); } @@ -32,6 +41,11 @@ protected function setUp(): void 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() @@ -483,6 +497,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 From c1974a723cdee8a273cb49ce13fada5c1667706a Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 8 Oct 2024 09:23:06 +0200 Subject: [PATCH 3/4] minor #58472 CS: clean some whitespaces/indentation (keradus) This PR was squashed before being merged into the 7.2 branch. Discussion ---------- CS: clean some whitespaces/indentation | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | Fix CS | License | MIT Commits ------- d0f63b8afa1 CS: clean some whitespaces/indentation --- .../Core/DataTransformer/NumberToLocalizedStringTransformer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php b/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php index bd9f796e1..653f1c445 100644 --- a/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php +++ b/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php @@ -143,7 +143,7 @@ public function reverseTransform($value) $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 { From d4cb9a2447b46f16256bb2b327434d80a35b10b6 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Tue, 8 Oct 2024 14:54:29 +0200 Subject: [PATCH 4/4] [Form] Remove unnecessary imports --- Test/FormPerformanceTestCase.php | 1 - Tests/Extension/Core/Type/BaseTypeTestCase.php | 1 - 2 files changed, 2 deletions(-) diff --git a/Test/FormPerformanceTestCase.php b/Test/FormPerformanceTestCase.php index 702f37b7a..21e6ed7da 100644 --- a/Test/FormPerformanceTestCase.php +++ b/Test/FormPerformanceTestCase.php @@ -12,7 +12,6 @@ namespace Symfony\Component\Form\Test; use Symfony\Component\Form\Test\Traits\RunTestTrait; -use Symfony\Component\Form\Tests\VersionAwareTest; /** * Base class for performance tests. diff --git a/Tests/Extension/Core/Type/BaseTypeTestCase.php b/Tests/Extension/Core/Type/BaseTypeTestCase.php index 32ab61116..8293ec31b 100644 --- a/Tests/Extension/Core/Type/BaseTypeTestCase.php +++ b/Tests/Extension/Core/Type/BaseTypeTestCase.php @@ -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