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 public function outOfStock(): static
{ {
return $this->state([ return $this->state([

View File

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

View File

@ -50,4 +50,13 @@ class ProductPrice extends Model implements Cartable
{ {
return $query->where('active', true); 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 public function getCartTotal(?string $cartId = null): float
{ {
return $this->cartItems()->get()->sum(function ($item) { 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(); $product1 = Product::factory()->withPrices(unit_amount:40)->create();
$product2 = Product::factory()->withPrices(unit_amount:60)->create(); $product2 = Product::factory()->withPrices(unit_amount:60)->create();
$this->assertNotNull($product1->getCurrentPrice());
$this->assertNotNull($product2->getCurrentPrice());
$user->addToCart($product1, quantity: 2); $user->addToCart($product1, quantity: 2);
$user->addToCart($product2, quantity: 1); $user->addToCart($product2, quantity: 1);

View File

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