BFI tests

This commit is contained in:
a6a2f5842 2025-11-24 14:32:11 +01:00
parent c1f531e659
commit 7c6b61da45
6 changed files with 32 additions and 26 deletions

View File

@ -31,18 +31,6 @@ class ProductFactory extends Factory
];
}
public function onSale(): static
{
return $this->state(function (array $attributes) {
$regularPrice = $attributes['regular_price'];
return [
'sale_price' => $regularPrice * 0.8,
'sale_start' => now()->subDay(),
'sale_end' => now()->addWeek(),
];
});
}
public function outOfStock(): static
{
return $this->state([

View File

@ -191,9 +191,13 @@ class Product extends Model implements Purchasable, Cartable
public function isOnSale(): bool
{
if (!$this->sale_start) {
return false;
}
$now = now();
if ($this->sale_start && $now->lt($this->sale_start)) {
if ($now->lt($this->sale_start)) {
return false;
}
@ -210,8 +214,7 @@ class Product extends Model implements Purchasable, Cartable
return $this->sale_price;
}
$defaultPrice = $this->defaultPrice()->first();
return $defaultPrice ? $defaultPrice->price : $this->regular_price;
return $this->defaultPrice()->first()?->getCurrentPrice();
}
public function isInStock(): bool

View File

@ -50,4 +50,13 @@ class ProductPrice extends Model implements Cartable
{
return $query->where('active', true);
}
public function getCurrentPrice(): float
{
if ($this->sale_unit_amount && $this->sale_unit_amount < $this->unit_amount) {
return $this->sale_unit_amount;
}
return $this->unit_amount;
}
}

View File

@ -205,7 +205,8 @@ trait HasShoppingCapabilities
public function getCartTotal(?string $cartId = null): float
{
return $this->cartItems()->get()->sum(function ($item) {
return $item->purchasable->getCurrentPrice() * $item->quantity;
dump('getCurrentPrice',get_class($item->purchasable),$item->purchasable->getCurrentPrice());
return ($item->purchasable->getCurrentPrice() ?? 0) * $item->quantity;
});
}

View File

@ -138,6 +138,9 @@ class PurchaseFlowTest extends TestCase
$product1 = Product::factory()->withPrices(unit_amount:40)->create();
$product2 = Product::factory()->withPrices(unit_amount:60)->create();
$this->assertNotNull($product1->getCurrentPrice());
$this->assertNotNull($product2->getCurrentPrice());
$user->addToCart($product1, quantity: 2);
$user->addToCart($product2, quantity: 1);

View File

@ -13,23 +13,25 @@ class ProductPricingTest extends TestCase
/** @test */
public function it_returns_regular_price_when_not_on_sale()
{
$product = Product::factory()->create([
'regular_price' => 100,
'sale_price' => null,
]);
$product = Product::factory()->withPrices(2, 100)->create();
$this->assertEquals(2, $product->prices()->count());
$this->assertFalse($product->isOnSale());
$this->assertNotNull($product->defaultPrice()->first());
$this->assertEquals(100, $product->getCurrentPrice());
}
/** @test */
public function it_returns_sale_price_when_on_sale()
{
$product = Product::factory()->create([
'regular_price' => 100,
'sale_price' => 80,
'sale_start' => now()->subDay(),
'sale_end' => now()->addDay(),
]);
$product = Product::factory()
->withPrices(2, 100)
->onSale(
sale_price: 80,
sale_start: now()->subDay(),
sale_end: now()->addDay(),
)
->create();
$this->assertEquals(80, $product->getCurrentPrice());
}