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

View File

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

View File

@ -51,9 +51,9 @@ class ProductPrice extends Model implements Cartable
return $query->where('active', true); 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; return $this->sale_unit_amount;
} }

View File

@ -210,11 +210,11 @@ class StockManagementTest extends TestCase
/** @test */ /** @test */
public function product_tracks_low_stock_threshold() public function product_tracks_low_stock_threshold()
{ {
$product = Product::factory()->create([ $product = Product::factory()
'manage_stock' => true, ->withStocks(12)
'stock_quantity' => 15, ->create([
'low_stock_threshold' => 10, 'low_stock_threshold' => 10,
]); ]);
$this->assertFalse($product->isLowStock()); $this->assertFalse($product->isLowStock());
@ -226,14 +226,12 @@ class StockManagementTest extends TestCase
/** @test */ /** @test */
public function it_updates_in_stock_status_automatically() public function it_updates_in_stock_status_automatically()
{ {
$product = Product::factory()->create([ $product = Product::factory()
'manage_stock' => true, ->withStocks(10)
'stock_quantity' => 5, ->create();
'in_stock' => true,
]);
$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() public function it_returns_sale_price_when_on_sale()
{ {
$product = Product::factory() $product = Product::factory()
->withPrices(2, 100) ->withPrices(1, 100)
->onSale( ->create([
sale_price: 80, 'sale_start' => now()->subDay(),
sale_start: now()->subDay(), 'sale_end' => now()->addDay(),
sale_end: now()->addDay(), ]);
)
->create(); $price = $product->prices()->first();
$price->sale_unit_amount = 80;
$price->save();
$this->assertEquals(80, $product->getCurrentPrice()); $this->assertEquals(80, $product->getCurrentPrice());
} }
@ -39,11 +42,19 @@ class ProductPricingTest extends TestCase
/** @test */ /** @test */
public function it_returns_regular_price_when_sale_has_ended() public function it_returns_regular_price_when_sale_has_ended()
{ {
$product = Product::factory()->create([ $product = Product::factory()->withPrices(1, 100)->create([
'regular_price' => 100, 'sale_start' => now()->subWeek(),
'sale_price' => 80, 'sale_end' => now()->addHour(),
'sale_start' => now()->subDays(7), ]);
'sale_end' => now()->subDay(),
$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()); $this->assertEquals(100, $product->getCurrentPrice());
@ -52,26 +63,21 @@ class ProductPricingTest extends TestCase
/** @test */ /** @test */
public function it_returns_regular_price_when_sale_hasnt_started() public function it_returns_regular_price_when_sale_hasnt_started()
{ {
$product = Product::factory()->create([ $product = Product::factory()->withPrices(1, 100)->create([
'regular_price' => 100,
'sale_price' => 80,
'sale_start' => now()->addDay(), 'sale_start' => now()->addDay(),
'sale_end' => now()->addWeek(), 'sale_end' => now()->addWeek(),
]); ]);
$this->assertEquals(100, $product->getCurrentPrice()); $price = $product->prices()->first();
} $price->sale_unit_amount = 80;
$price->save();
/** @test */ $this->assertEquals(100, $product->getCurrentPrice());
public function it_calculates_discount_percentage()
{ $product->update([
$product = Product::factory()->create([ 'sale_start' => now()->subHour(),
'regular_price' => 100,
'sale_price' => 75,
]); ]);
$discount = (($product->regular_price - $product->sale_price) / $product->regular_price) * 100; $this->assertEquals(80, $product->getCurrentPrice());
$this->assertEquals(25, $discount);
} }
} }

View File

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