1111
1212namespace Symfony \Component \Intl \Tests \NumberFormatter ;
1313
14- use PHPUnit \Framework \Error \Warning ;
1514use PHPUnit \Framework \TestCase ;
1615use Symfony \Component \Intl \Globals \IntlGlobals ;
1716use Symfony \Component \Intl \NumberFormatter \NumberFormatter ;
@@ -328,13 +327,17 @@ public function testFormatTypeCurrency($formatter, $value)
328327 {
329328 if (\PHP_VERSION_ID >= 80000 ) {
330329 $ this ->expectException (\ValueError::class);
331- } elseif (method_exists ($ this , 'expectWarning ' )) {
332- $ this ->expectWarning ();
333330 } else {
334- $ this ->expectException (Warning ::class);
331+ $ this ->expectException (\ErrorException ::class);
335332 }
336333
337- $ formatter ->format ($ value , NumberFormatter::TYPE_CURRENCY );
334+ set_error_handler ([self ::class, 'throwOnWarning ' ]);
335+
336+ try {
337+ $ formatter ->format ($ value , NumberFormatter::TYPE_CURRENCY );
338+ } finally {
339+ restore_error_handler ();
340+ }
338341 }
339342
340343 /**
@@ -705,16 +708,21 @@ public static function parseProvider()
705708
706709 public function testParseTypeDefault ()
707710 {
711+ $ formatter = static ::getNumberFormatter ('en ' , NumberFormatter::DECIMAL );
712+
708713 if (\PHP_VERSION_ID >= 80000 ) {
709714 $ this ->expectException (\ValueError::class);
710- } elseif (method_exists ($ this , 'expectWarning ' )) {
711- $ this ->expectWarning ();
712715 } else {
713- $ this ->expectException (Warning ::class);
716+ $ this ->expectException (\ErrorException ::class);
714717 }
715718
716- $ formatter = static ::getNumberFormatter ('en ' , NumberFormatter::DECIMAL );
717- $ formatter ->parse ('1 ' , NumberFormatter::TYPE_DEFAULT );
719+ set_error_handler ([self ::class, 'throwOnWarning ' ]);
720+
721+ try {
722+ $ formatter ->parse ('1 ' , NumberFormatter::TYPE_DEFAULT );
723+ } finally {
724+ restore_error_handler ();
725+ }
718726 }
719727
720728 /**
@@ -831,16 +839,21 @@ public static function parseTypeDoubleProvider()
831839
832840 public function testParseTypeCurrency ()
833841 {
842+ $ formatter = static ::getNumberFormatter ('en ' , NumberFormatter::DECIMAL );
843+
834844 if (\PHP_VERSION_ID >= 80000 ) {
835845 $ this ->expectException (\ValueError::class);
836- } elseif (method_exists ($ this , 'expectWarning ' )) {
837- $ this ->expectWarning ();
838846 } else {
839- $ this ->expectException (Warning ::class);
847+ $ this ->expectException (\ErrorException ::class);
840848 }
841849
842- $ formatter = static ::getNumberFormatter ('en ' , NumberFormatter::DECIMAL );
843- $ formatter ->parse ('1 ' , NumberFormatter::TYPE_CURRENCY );
850+ set_error_handler ([self ::class, 'throwOnWarning ' ]);
851+
852+ try {
853+ $ formatter ->parse ('1 ' , NumberFormatter::TYPE_CURRENCY );
854+ } finally {
855+ restore_error_handler ();
856+ }
844857 }
845858
846859 public function testParseWithNotNullPositionValue ()
@@ -864,4 +877,13 @@ abstract protected function getIntlErrorCode(): int;
864877 * @param int $errorCode
865878 */
866879 abstract protected function isIntlFailure ($ errorCode ): bool ;
880+
881+ public static function throwOnWarning (int $ errno , string $ errstr , string $ errfile = null , int $ errline = null ): bool
882+ {
883+ if ($ errno & (\E_WARNING | \E_USER_WARNING )) {
884+ throw new \ErrorException ($ errstr , 0 , $ errno , $ errfile ?? __FILE__ , $ errline ?? __LINE__ );
885+ }
886+
887+ return false ;
888+ }
867889}
0 commit comments