8000 [Form] Support intl.use_exceptions/error_level in NumberToLocalizedSt… · symfony/symfony@72549c5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 72549c5

Browse files
bram123fabpot
authored andcommitted
[Form] Support intl.use_exceptions/error_level in NumberToLocalizedStringTransformer
1 parent 908a91f commit 72549c5

File tree

5 files changed

+151
-18
lines changed

5 files changed

+151
-18
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,14 @@ public function reverseTransform($value)
152152
: \NumberFormatter::TYPE_INT32;
153153
}
154154

155-
$result = $formatter->parse($value, $type, $position);
155+
try {
156+
$result = @$formatter->parse($value, $type, $position);
157+
} catch (\IntlException $e) {
158+
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
159+
}
156160

157161
if (intl_is_failure($formatter->getErrorCode())) {
158-
throw new TransformationFailedException($formatter->getErrorMessage());
162+
throw new TransformationFailedException($formatter->getErrorMessage(), $formatter->getErrorCode());
159163
}
160164

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

src/Symfony/Component/Form/Extension/Core/DataTransformer/PercentToLocalizedStringTransformer.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,15 @@ public function reverseTransform($value)
139139
$type = \PHP_INT_SIZE === 8 ? \NumberFormatter::TYPE_INT64 : \NumberFormatter::TYPE_INT32;
140140
}
141141

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

145149
if (intl_is_failure($formatter->getErrorCode())) {
146-
throw new TransformationFailedException($formatter->getErrorMessage());
150+
throw new TransformationFailedException($formatter->getErrorMessage(), $formatter->getErrorCode());
147151
}
148152

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

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToLocalizedStringTransformerTest.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ protected function tearDown(): void
5656

5757
if (\extension_loaded('intl')) {
5858
ini_set('intl.use_exceptions', $this->initialTestCaseUseException);
59-
ini_set('intl.error_level', $this->initialTestCaseUseException);
59+
ini_set('intl.error_level', $this->initialTestCaseErrorLevel);
6060
}
6161
}
6262

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

344+
/**
345+
* @requires extension intl
346+
*/
344347
public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
345348
{
346-
if (!\extension_loaded('intl')) {
347-
$this->markTestSkipped('intl extension is not loaded');
348-
}
349-
350349
$errorLevel = ini_set('intl.error_level', \E_WARNING);
351350

352351
try {
@@ -358,12 +357,11 @@ public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
358357
}
359358
}
360359

360+
/**
361+
* @requires extension intl
362+
*/
361363
public function testReverseTransformWrapsIntlErrorsWithExceptions()
362364
{
363-
if (!\extension_loaded('intl')) {
364-
$this->markTestSkipped('intl extension is not loaded');
365-
}
366-
367365
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
368366

369367
try {
@@ -375,12 +373,11 @@ public function testReverseTransformWrapsIntlErrorsWithExceptions()
375373
}
376374
}
377375

376+
/**
377+
* @requires extension intl
378+
*/
378379
public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
379380
{
380-
if (!\extension_loaded('intl')) {
381-
$this->markTestSkipped('intl extension is not loaded');
382-
}
383-
384381
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
385382
$initialErrorLevel = ini_set('intl.error_level', \E_WARNING);
386383

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/NumberToLocalizedStringTransformerTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,29 @@ class NumberToLocalizedStringTransformerTest extends TestCase
2020
{
2121
private $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 [

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/PercentToLocalizedStringTransformerTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,29 @@ class PercentToLocalizedStringTransformerTest extends TestCase
2323

2424
private $defaultLocale;
2525

26+
private $initialTestCaseUseException;
27+
private $initialTestCaseErrorLevel;
28+
2629
protected function setUp(): void
2730
{
31+
// Normalize intl. configuration settings.
32+
if (\extension_loaded('intl')) {
33+
$this->initialTestCaseUseException = ini_set('intl.use_exceptions', 0);
34+
$this->initialTestCaseErrorLevel = ini_set('intl.error_level', 0);
35+
}
36+
2837
$this->defaultLocale = \Locale::getDefault();
2938
\Locale::setDefault('en');
3039
}
3140

3241
protected function tearDown(): void
3342
{
3443
\Locale::setDefault($this->defaultLocale);
44+
45+
if (\extension_loaded('intl')) {
46+
ini_set('intl.use_exceptions', $this->initialTestCaseUseException);
47+
ini_set('intl.error_level', $this->initialTestCaseErrorLevel);
48+
}
3549
}
3650

3751
public function testTransform()
@@ -483,6 +497,56 @@ public function testReverseTransformForHtml5FormatWithScale()
483497

484498
$this->assertEquals(0.1234, $transformer->reverseTransform('12.34'));
485499
}
500+
501+
/**
502+
* @requires extension intl
503+
*/
504+
public function testReverseTransformWrapsIntlErrorsWithErrorLevel()
505+
{
506+
$errorLevel = ini_set('intl.error_level', \E_WARNING);
507+
508+
try {
509+
$this->expectException(TransformationFailedException::class);
510+
$transformer = new PercentToLocalizedStringTransformer(null, null, \NumberFormatter::ROUND_HALFUP);
511+
$transformer->reverseTransform('invalid_number');
512+
} finally {
513+
ini_set('intl.error_level', $errorLevel);
514+
}
515+
}
516+
517+
/**
518+
* @requires extension intl
519+
*/
520+
public function testReverseTransformWrapsIntlErrorsWithExceptions()
521+
{
522+
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
523+
524+
try {
525+
$this->expectException(TransformationFailedException::class);
526+
$transformer = new PercentToLocalizedStringTransformer(null, null, \NumberFormatter::ROUND_HALFUP);
527+
$transformer->reverseTransform('invalid_number');
528+
} finally {
529+
ini_set('intl.use_exceptions', $initialUseExceptions);
530+
}
531+
}
532+
533+
/**
534+
* @requires extension intl
535+
*/
536+
public function testReverseTransformWrapsIntlErrorsWithExceptionsAndErrorLevel()
537+
{
538+
$initialUseExceptions = ini_set('intl.use_exceptions', 1);
539+
$initialErrorLevel = ini_set('intl.error_level', \E_WARNING);
540+
541+
try {
542+
$this->expectException(TransformationFailedException::class);
543+
$transformer = new PercentToLocalizedStringTransformer(null, null, \NumberFormatter::ROUND_HALFUP);
544+
$transformer->reverseTransform('invalid_number');
545+
} finally {
546+
ini_set('intl.use_exceptions', $initialUseExceptions);
547+
ini_set('intl.error_level', $initialErrorLevel);
548+
}
549+
}
486550
}
487551

488552
class PercentToLocalizedStringTransformerWithoutGrouping extends PercentToLocalizedStringTransformer

0 commit comments

Comments
 (0)
0