Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions app/Models/PluginBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}

Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/PluginBundleWithoutPriceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Tests\Feature;

use App\Models\PluginBundle;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PluginBundleWithoutPriceTest extends TestCase
{
use RefreshDatabase;

public function test_a_bundle_without_any_price_has_no_discount_or_savings(): void
{
$bundle = PluginBundle::factory()->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()));
}
}