From a4a4ebe95c2cc37ca57b40b64b4d3aee22cdbf97 Mon Sep 17 00:00:00 2001 From: Yurii Myronchuk Date: Tue, 22 Apr 2025 14:32:58 +0000 Subject: [PATCH 1/2] HP-2446: format number as string --- src/price/ProgressivePrice.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/price/ProgressivePrice.php b/src/price/ProgressivePrice.php index baabf712..6c355e19 100644 --- a/src/price/ProgressivePrice.php +++ b/src/price/ProgressivePrice.php @@ -105,8 +105,8 @@ public function calculateSum(QuantityInterface $quantity): ?Money $price = $threshold->price(); $chargedAmount = $price->money() - ->multiply((string)$billedUsage->getQuantity()) - ->divide((string)($price->multiplier())); + ->multiply((string)(sprintf('%.14F', $billedUsage->getQuantity()))) + ->divide((string)(sprintf('%.14F', $price->multiplier()))); $this->calculationTraces[] = new ProgressivePriceCalculationTrace( $threshold, $billedUsage, $chargedAmount From b8a622b0c40225bbd9e57f5cc5f57d0242f758f3 Mon Sep 17 00:00:00 2001 From: Yurii Myronchuk Date: Tue, 6 May 2025 17:12:39 +0000 Subject: [PATCH 2/2] Fix test --- tests/unit/price/ProgressivePriceTest.php | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/unit/price/ProgressivePriceTest.php b/tests/unit/price/ProgressivePriceTest.php index 67e8e625..a7ee9250 100644 --- a/tests/unit/price/ProgressivePriceTest.php +++ b/tests/unit/price/ProgressivePriceTest.php @@ -215,4 +215,41 @@ private function progressivePriceProvider(): Generator ], ]; } + + /** + * @dataProvider progressivePriceProviderSmallUsage + */ + public function testProgressivePriceSmallUsage( + array $inputThresholdsArray, + int $expectedAmount, + string $startPrice, + string $prepaid = '0' + ): void { + $price = $this->createProgressivePrice( + prepaid: $prepaid, + startPrice: $startPrice, + thresholdsArray: $inputThresholdsArray + ); + $this->usage = Quantity::bps(6043); + $usage = $price->calculateUsage($this->usage); + $this->assertSame($this->usage->getQuantity(), $usage->getQuantity()); + + $amount = $price->calculateSum($this->usage); + $this->assertEquals($expectedAmount, $amount->getAmount()); + } + + private function progressivePriceProviderSmallUsage(): Generator + { + yield 'Simple case' => [ + 'thresholds' => [ + ['price' => '10', 'currency' => 'EUR', 'quantity' => '0', 'unit' => 'gbps'], + ], + 'money' => 0, + 'price' => '1', + 'prepaid' => '0', + 'trace' => [ + '6043bps * 0.00000001 = 0.00', + ], + ]; + } }