Skip to content

Commit 7a48dda

Browse files
committed
Merge branch '6.4' into 7.1
* 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
2 parents e88613a + 0fe17f9 commit 7a48dda

7 files changed

+154
-22
lines changed

Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function reverseTransform(mixed $value): int|float|null
104104
$value = str_replace(',', $decSep, $value);
105105
}
106106

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

116-
$result = $formatter->parse($value, $type, $position);
116+
try {
117+
$result = @$formatter->parse($value, $type, $position);
118+
} catch (\IntlException $e) {
119+
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
120+
}
117121

118122
if (intl_is_failure($formatter->getErrorCode())) {
119-
throw new TransformationFailedException($formatter->getErrorMessage());
123+
throw new TransformationFailedException($formatter->getErrorMessage(), $formatter->getErrorCode());
120124
}
121125

122126
if ($result >= \PHP_INT_MAX || $result <= -\PHP_INT_MAX) {

Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,15 @@ public function reverseTransform(mixed $value): int|float|null
131131
$type = \PHP_INT_SIZE === 8 ? \NumberFormatter::TYPE_INT64 : \NumberFormatter::TYPE_INT32;
132132
}
133133

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

137141
if (intl_is_failure($formatter->getErrorCode())) {
138-
throw new TransformationFailedException($formatter->getErrorMessage());
142+
throw new TransformationFailedException($formatter->getErrorMessage(), $formatter->getErrorCode());
139143
}
140144

141145
if (self::FRACTIONAL == $this->type) {

Test/FormPerformanceTestCase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace Symfony\Component\Form\Test;
1313

14-
use Symfony\Component\Form\Tests\VersionAwareTest;
15-
1614
/**
1715
* Base class for performance tests.
1816
*
@@ -35,6 +33,8 @@ protected function runTest(): mixed
3533
$this->fail(sprintf('expected running time: <= %s but was: %s', $this->maxRunningTime, $time));
3634
}
3735

36+
$this->expectNotToPerformAssertions();
37+
3838
return $result;
3939
}
4040

Tests/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php

+10-13
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function tearDown(): void
5454

5555
if (\extension_loaded('intl')) {
5656
ini_set('intl.use_exceptions', $this->initialTestCaseUseException);
57-
ini_set('intl.error_level', $this->initialTestCaseUseException);
57+
ini_set('intl.error_level', $this->initialTestCaseErrorLevel);
5858
}
5959
}
6060

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

342+
/**
343+
* @requires extension intl
344+
*/
342345
public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
343346
{
344-
if (!\extension_loaded('intl')) {
345-
$this->markTestSkipped('intl extension is not loaded');
346-
}
347-
348347
$errorLevel = ini_set('intl.error_level', \E_WARNING);
349348

350349
try {
@@ -356,12 +355,11 @@ public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
356355
}
357356
}
358357

358+
/**
359+
* @requires extension intl
360+
*/
359361
public function testReverseTransformWrapsIntlErrorsWithExceptions()
360362
{
361-
if (!\extension_loaded('intl')) {
362-
$this->markTestSkipped('intl extension is not loaded');
363-
}
364-
365363
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
366364

367365
try {
@@ -373,12 +371,11 @@ public function testReverseTransformWrapsIntlErrorsWithExceptions()
373371
}
374372
}
375373

374+
/**
375+
* @requires extension intl
376+
*/
376377
public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
377378
{
378-
if (!\extension_loaded('intl')) {
379-
$this->markTestSkipped('intl extension is not loaded');
380-
}
381-
382379
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
383380
$initialErrorLevel = ini_set('intl.error_level', \E_WARNING);
384381

Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php

+64
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,29 @@ class NumberToLocalizedStringTransformerTest extends TestCase
2020
{
2121
private string $defaultLocale;
2222

23+
private $initialTestCaseUseException;
24+
private $initialTestCaseErrorLevel;
25+
2326
protected function setUp(): void
2427
{
28+
// Normalize intl. configuration settings.
29+
if (\extension_loaded('intl')) {
30+
$this->initialTestCaseUseException = ini_set('intl.use_exceptions', 0);
31+
$this->initialTestCaseErrorLevel = ini_set('intl.error_level', 0);
32+
}
33+
2534
$this->defaultLocale = \Locale::getDefault();
2635
\Locale::setDefault('en');
2736
}
2837

2938
protected function tearDown(): void
3039
{
3140
\Locale::setDefault($this->defaultLocale);
41+
42+
if (\extension_loaded('intl')) {
43+
ini_set('intl.use_exceptions', $this->initialTestCaseUseException);
44+
ini_set('intl.error_level', $this->initialTestCaseErrorLevel);
45+
}
3246
}
3347

3448
public static function provideTransformations()
@@ -647,6 +661,56 @@ public function testReverseTransformENotation($output, $input)
647661
$this->assertSame($output, $transformer->reverseTransform($input));
648662
}
649663

664+
/**
665+
* @requires extension intl
666+
*/
667+
public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
668+
{
669+
$errorLevel = ini_set('intl.error_level', \E_WARNING);
670+
671+
try {
672+
$this->expectException(TransformationFailedException::class);
673+
$transformer = new NumberToLocalizedStringTransformer();
674+
$transformer->reverseTransform('invalid_number');
675+
} finally {
676+
ini_set('intl.error_level', $errorLevel);
677+
}
678+
}
679+
680+
/**
681+
* @requires extension intl
682+
*/
683+
public function testReverseTransformWrapsIntlErrorsWithExceptions()
684+
{
685+
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
686+
687+
try {
688+
$this->expectException(TransformationFailedException::class);
689+
$transformer = new NumberToLocalizedStringTransformer();
690+
$transformer->reverseTransform('invalid_number');
691+
} finally {
692+
ini_set('intl.use_exceptions', $initialUseExceptions);
693+
}
694+
}
695+
696+
/**
697+
* @requires extension intl
698+
*/
699+
public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
700+
{
701+
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
702+
$initialErrorLevel = ini_set('intl.error_level', \E_WARNING);
703+
704+
try {
705+
$this->expectException(TransformationFailedException::class);
706+
$transformer = new NumberToLocalizedStringTransformer();
707+
$transformer->reverseTransform('invalid_number');
708+
} finally {
709+
ini_set('intl.use_exceptions', $initialUseExceptions);
710+
ini_set('intl.error_level', $initialErrorLevel);
711+
}
712+
}
713+
650714
public static function eNotationProvider(): array
651715
{
652716
return [

Tests/Extension/Core/DataTransformer/PercentToLocalizedStringTransformerTest.php

+64
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,29 @@ class PercentToLocalizedStringTransformerTest extends TestCase
2020
{
2121
private string $defaultLocale;
2222

23+
private $initialTestCaseUseException;
24+
private $initialTestCaseErrorLevel;
25+
2326
protected function setUp(): void
2427
{
28+
// Normalize intl. configuration settings.
29+
if (\extension_loaded('intl')) {
30+
$this->initialTestCaseUseException = ini_set('intl.use_exceptions', 0);
31+
$this->initialTestCaseErrorLevel = ini_set('intl.error_level', 0);
32+
}
33+
2534
$this->defaultLocale = \Locale::getDefault();
2635
\Locale::setDefault('en');
2736
}
2837

2938
protected function tearDown(): void
3039
{
3140
\Locale::setDefault($this->defaultLocale);
41+
42+
if (\extension_loaded('intl')) {
43+
ini_set('intl.use_exceptions', $this->initialTestCaseUseException);
44+
ini_set('intl.error_level', $this->initialTestCaseErrorLevel);
45+
}
3246
}
3347

3448
public function testTransform()
@@ -475,6 +489,56 @@ public function testReverseTransformForHtml5FormatWithScale()
475489

476490
$this->assertEquals(0.1234, $transformer->reverseTransform('12.34'));
477491
}
492+
493+
/**
494+
* @requires extension intl
495+
*/
496+
public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
497+
{
498+
$errorLevel = ini_set('intl.error_level', \E_WARNING);
499+
500+
try {
501+
$this->expectException(TransformationFailedException::class);
502+
$transformer = new PercentToLocalizedStringTransformer(null, null, \NumberFormatter::ROUND_HALFUP);
503+
$transformer->reverseTransform('invalid_number');
504+
} finally {
505+
ini_set('intl.error_level', $errorLevel);
506+
}
507+
}
508+
509+
/**
510+
* @requires extension intl
511+
*/
512+
public function testReverseTransformWrapsIntlErrorsWithExceptions()
513+
{
514+
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
515+
516+
try {
517+
$this->expectException(TransformationFailedException::class);
518+
$transformer = new PercentToLocalizedStringTransformer(null, null, \NumberFormatter::ROUND_HALFUP);
519+
$transformer->reverseTransform('invalid_number');
520+
} finally {
521+
ini_set('intl.use_exceptions', $initialUseExceptions);
522+
}
523+
}
524+
525+
/**
526+
* @requires extension intl
527+
*/
528+
public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
529+
{
530+
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
531+
$initialErrorLevel = ini_set('intl.error_level', \E_WARNING);
532+
533+
try {
534+
$this->expectException(TransformationFailedException::class);
535+
$transformer = new PercentToLocalizedStringTransformer(null, null, \NumberFormatter::ROUND_HALFUP);
536+
$transformer->reverseTransform('invalid_number');
537+
} finally {
538+
ini_set('intl.use_exceptions', $initialUseExceptions);
539+
ini_set('intl.error_level', $initialErrorLevel);
540+
}
541+
}
478542
}
479543

480544
class PercentToLocalizedStringTransformerWithoutGrouping extends PercentToLocalizedStringTransformer

Tests/Extension/Core/Type/BaseTypeTestCase.php

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Form\Tests\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\Test\TypeTestCase;
15-
use Symfony\Component\Form\Tests\VersionAwareTest;
1615

1716
/**
1817
* @author Bernhard Schussek <[email protected]>

0 commit comments

Comments
 (0)