From 10a07398a4ff2ad943473f5f081ac6d3759b53a6 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 2 Jun 2024 18:05:40 +0900 Subject: [PATCH 1/2] test: add test cases --- tests/system/Helpers/NumberHelperTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/system/Helpers/NumberHelperTest.php b/tests/system/Helpers/NumberHelperTest.php index 9b3afa61992f..7e5fe2a358c4 100644 --- a/tests/system/Helpers/NumberHelperTest.php +++ b/tests/system/Helpers/NumberHelperTest.php @@ -38,6 +38,11 @@ public function testRomanNumber(): void $this->assertSame('X', number_to_roman(10)); } + public function testRomanNumberString(): void + { + $this->assertSame('XCVI', number_to_roman('96')); + } + public function testRomanNumberRange(): void { $this->assertNull(number_to_roman(-1)); @@ -70,6 +75,11 @@ public function testNumberToSize(): void $this->assertSame('456 Bytes', number_to_size(456, 1, 'en_US')); } + public function testNumberToSizeString(): void + { + $this->assertSame('456 Bytes', number_to_size('456', 1, 'en_US')); + } + public function testKbFormat(): void { $this->assertSame('4.5 KB', number_to_size(4567, 1, 'en_US')); @@ -109,6 +119,11 @@ public function testThousands(): void $this->assertSame('1,000 thousand', number_to_amount('999999', 0, 'en_US')); } + public function testThousandsInt(): void + { + $this->assertSame('123 thousand', number_to_amount(123000, 0, 'en_US')); + } + public function testMillions(): void { $this->assertSame('123.4 million', number_to_amount('123,400,000', 1, 'en_US')); From 3d6436ed4666732f0008da27cf3ffd943720150d Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 2 Jun 2024 18:06:41 +0900 Subject: [PATCH 2/2] fix: TypeError in number_to_amount() TypeError: str_replace(): Argument #3 ($subject) must be of type array|string, int given --- system/Helpers/number_helper.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/system/Helpers/number_helper.php b/system/Helpers/number_helper.php index 96468ddcdea3..0999d200f99b 100644 --- a/system/Helpers/number_helper.php +++ b/system/Helpers/number_helper.php @@ -82,8 +82,9 @@ function number_to_amount($num, int $precision = 0, ?string $locale = null) // Strip any formatting & ensure numeric input try { // @phpstan-ignore-next-line - $num = 0 + str_replace(',', '', $num); + $num = 0 + str_replace(',', '', (string) $num); } catch (ErrorException) { + // Catch "Warning: A non-numeric value encountered" return false; }