D basic tests

This commit is contained in:
a6a2f5842 2025-11-25 17:02:39 +01:00
parent 01c21506b6
commit 5fb31d0c4f
6 changed files with 47 additions and 52 deletions

View File

@ -134,7 +134,6 @@ class ShopAddExampleProducts extends Command
'is_visible' => true,
'featured' => $this->faker->boolean(20), // 20% featured
'price' => $onSale ? $regularPrice * 0.8 : $regularPrice,
'regular_price' => $regularPrice,
'sale_price' => $onSale ? $regularPrice * $this->faker->randomFloat(2, 0.6, 0.9) : null,
'sale_start' => $onSale ? now()->subDays($this->faker->numberBetween(1, 30)) : null,
'sale_end' => $onSale ? now()->addDays($this->faker->numberBetween(7, 60)) : null,
@ -361,7 +360,6 @@ class ShopAddExampleProducts extends Command
'status' => 'published',
'is_visible' => false, // Variations are not directly visible
'price' => $product->price + ($index * 5), // Slight price increase per size
'regular_price' => $product->regular_price + ($index * 5),
'manage_stock' => true,
'stock_quantity' => $this->faker->numberBetween(5, 50),
'in_stock' => true,

View File

@ -216,11 +216,7 @@ class Product extends Model implements Purchasable, Cartable
public function getCurrentPrice(): ?float
{
if ($this->isOnSale()) {
return $this->sale_price;
}
return $this->defaultPrice()->first()?->getCurrentPrice();
return $this->defaultPrice()->first()?->getCurrentPrice($this->isOnSale());
}
public function isInStock(): bool
@ -425,7 +421,7 @@ class Product extends Model implements Purchasable, Cartable
return false;
}
return $this->stock_quantity <= $this->low_stock_threshold;
return $this->getAvailableStock() <= $this->low_stock_threshold;
}
public function isVisible(): bool
@ -452,7 +448,6 @@ class Product extends Model implements Purchasable, Cartable
'short_description' => $this->getLocalized('short_description'),
'type' => $this->type,
'price' => $this->getCurrentPrice(),
'regular_price' => $this->regular_price,
'sale_price' => $this->sale_price,
'is_on_sale' => $this->isOnSale(),
'low_stock' => $this->isLowStock(),

View File

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

View File

@ -210,9 +210,9 @@ class StockManagementTest extends TestCase
/** @test */
public function product_tracks_low_stock_threshold()
{
$product = Product::factory()->create([
'manage_stock' => true,
'stock_quantity' => 15,
$product = Product::factory()
->withStocks(12)
->create([
'low_stock_threshold' => 10,
]);
@ -226,14 +226,12 @@ class StockManagementTest extends TestCase
/** @test */
public function it_updates_in_stock_status_automatically()
{
$product = Product::factory()->create([
'manage_stock' => true,
'stock_quantity' => 5,
'in_stock' => true,
]);
$product = Product::factory()
->withStocks(10)
->create();
$product->decreaseStock(5);
$product->decreaseStock(10);
$this->assertFalse($product->fresh()->in_stock);
$this->assertFalse($product->fresh()->isInStock());
}
}

View File

@ -25,13 +25,16 @@ class ProductPricingTest extends TestCase
public function it_returns_sale_price_when_on_sale()
{
$product = Product::factory()
->withPrices(2, 100)
->onSale(
sale_price: 80,
sale_start: now()->subDay(),
sale_end: now()->addDay(),
)
->create();
->withPrices(1, 100)
->create([
'sale_start' => now()->subDay(),
'sale_end' => now()->addDay(),
]);
$price = $product->prices()->first();
$price->sale_unit_amount = 80;
$price->save();
$this->assertEquals(80, $product->getCurrentPrice());
}
@ -39,11 +42,19 @@ class ProductPricingTest extends TestCase
/** @test */
public function it_returns_regular_price_when_sale_has_ended()
{
$product = Product::factory()->create([
'regular_price' => 100,
'sale_price' => 80,
'sale_start' => now()->subDays(7),
'sale_end' => now()->subDay(),
$product = Product::factory()->withPrices(1, 100)->create([
'sale_start' => now()->subWeek(),
'sale_end' => now()->addHour(),
]);
$price = $product->prices()->first();
$price->sale_unit_amount = 80;
$price->save();
$this->assertEquals(80, $product->getCurrentPrice());
$product->update([
'sale_end' => now()->subHour(),
]);
$this->assertEquals(100, $product->getCurrentPrice());
@ -52,26 +63,21 @@ class ProductPricingTest extends TestCase
/** @test */
public function it_returns_regular_price_when_sale_hasnt_started()
{
$product = Product::factory()->create([
'regular_price' => 100,
'sale_price' => 80,
$product = Product::factory()->withPrices(1, 100)->create([
'sale_start' => now()->addDay(),
'sale_end' => now()->addWeek(),
]);
$this->assertEquals(100, $product->getCurrentPrice());
}
$price = $product->prices()->first();
$price->sale_unit_amount = 80;
$price->save();
/** @test */
public function it_calculates_discount_percentage()
{
$product = Product::factory()->create([
'regular_price' => 100,
'sale_price' => 75,
$this->assertEquals(100, $product->getCurrentPrice());
$product->update([
'sale_start' => now()->subHour(),
]);
$discount = (($product->regular_price - $product->sale_price) / $product->regular_price) * 100;
$this->assertEquals(25, $discount);
$this->assertEquals(80, $product->getCurrentPrice());
}
}

View File

@ -25,9 +25,7 @@ class StockManagementTest extends TestCase
/** @test */
public function it_detects_sufficient_stock()
{
$product = Product::factory()->create([
'manage_stock' => true,
'stock_quantity' => 50,
$product = Product::factory()->withStocks(50)->create([
'low_stock_threshold' => 10,
]);