laravel-shop/tests/Unit/StockManagementTest.php

60 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2025-11-21 10:49:41 +00:00
<?php
namespace Blax\Shop\Tests\Unit;
use Blax\Shop\Models\Product;
use Blax\Shop\Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
2025-12-24 18:40:10 +00:00
use PHPUnit\Framework\Attributes\Test;
2025-11-21 10:49:41 +00:00
class StockManagementTest extends TestCase
{
use RefreshDatabase;
2025-12-24 18:40:10 +00:00
#[Test]
2025-11-21 10:49:41 +00:00
public function it_detects_low_stock()
{
2026-05-18 11:05:38 +00:00
$product = Product::factory()->withStocks(5)->create([
2025-11-21 10:49:41 +00:00
'manage_stock' => true,
'low_stock_threshold' => 10,
]);
$this->assertTrue($product->isLowStock());
}
2025-12-24 18:40:10 +00:00
#[Test]
2025-11-21 10:49:41 +00:00
public function it_detects_sufficient_stock()
{
2025-11-25 16:02:39 +00:00
$product = Product::factory()->withStocks(50)->create([
2025-11-21 10:49:41 +00:00
'low_stock_threshold' => 10,
]);
$this->assertFalse($product->isLowStock());
}
2025-12-24 18:40:10 +00:00
#[Test]
2025-11-21 10:49:41 +00:00
public function it_marks_product_as_out_of_stock()
{
2026-05-18 11:05:38 +00:00
// manage_stock + no ledger entries IS the out-of-stock state under
// the ledger-only model — there are no `in_stock` / `stock_status`
// columns to fall back on anymore.
2025-11-21 10:49:41 +00:00
$product = Product::factory()->create([
'manage_stock' => true,
]);
2026-05-18 11:05:38 +00:00
$this->assertFalse($product->isInStock());
$this->assertSame(0, $product->getAvailableStock());
2025-11-21 10:49:41 +00:00
}
2025-12-24 18:40:10 +00:00
#[Test]
2025-11-21 10:49:41 +00:00
public function products_without_stock_management_are_always_in_stock()
{
$product = Product::factory()->create([
'manage_stock' => false,
]);
// When stock management is disabled, product should be considered in stock
$this->assertFalse($product->manage_stock);
}
}