From 0c712175c832d06a5aec444a378d56a8102d9930 Mon Sep 17 00:00:00 2001 From: Shane Rosenthal Date: Wed, 25 Nov 2026 12:37:25 -0500 Subject: [PATCH] Handle bundles with no price in discount and savings Co-Authored-By: Claude Opus 5.5 --- app/Models/PluginBundle.php | 10 +++++--- .../Feature/PluginBundleWithoutPriceTest.php | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 tests/Feature/PluginBundleWithoutPriceTest.php diff --git a/app/Models/PluginBundle.php b/app/Models/PluginBundle.php index 1426f872d..90724e98a 100644 --- a/app/Models/PluginBundle.php +++ b/app/Models/PluginBundle.php @@ -188,11 +188,11 @@ protected function formattedPrice(): Attribute /** * Calculate the discount percentage for a given price amount. */ - public function getDiscountPercentFor(int $priceAmount): int + public function getDiscountPercentFor(?int $priceAmount): int { $retailValue = $this->retail_value; - if ($retailValue <= 0) { + if ($priceAmount === null || $retailValue <= 0) { return 0; } @@ -226,8 +226,12 @@ protected function discountPercent(): Attribute /** * Get the savings amount in cents for a given price amount. */ - public function getSavingsFor(int $priceAmount): int + public function getSavingsFor(?int $priceAmount): int { + if ($priceAmount === null) { + return 0; + } + return max(0, $this->retail_value - $priceAmount); } diff --git a/tests/Feature/PluginBundleWithoutPriceTest.php b/tests/Feature/PluginBundleWithoutPriceTest.php new file mode 100644 index 000000000..6e6fc3eb1 --- /dev/null +++ b/tests/Feature/PluginBundleWithoutPriceTest.php @@ -0,0 +1,23 @@ +create(['price' => null]); + + $this->assertSame(0, $bundle->discount_percent); + $this->assertSame(0, $bundle->savings); + $this->assertSame(0, $bundle->getDiscountPercentForUser(null)); + $this->assertSame(0, $bundle->getSavingsForUser(User::factory()->create())); + } +}