b2in/tests/Feature/Models/ProductTest.php
2026-02-20 17:57:50 +01:00

172 lines
5.3 KiB
PHP

<?php
use App\Enums\PriceType;
use App\Enums\ProductStatus;
use App\Enums\ProductType;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Collection;
use App\Models\Hub;
use App\Models\Media;
use App\Models\Partner;
use App\Models\Product;
use App\Models\Tag;
test('product can be created with factory', function () {
$product = Product::factory()->create();
expect($product)->toBeInstanceOf(Product::class);
expect($product->id)->toBeInt();
expect($product->name)->toBeString();
expect($product->slug)->toBeString();
});
test('product casts product_type to enum', function () {
$product = Product::factory()->create(['product_type' => 'local_stock']);
expect($product->product_type)->toBe(ProductType::LocalStock);
});
test('product casts status to enum', function () {
$product = Product::factory()->create(['status' => 'draft']);
expect($product->status)->toBe(ProductStatus::Draft);
});
test('product casts price_type to enum', function () {
$product = Product::factory()->create(['price_type' => 'from_price']);
expect($product->price_type)->toBe(PriceType::FromPrice);
});
test('product belongs to partner', function () {
$partner = Partner::factory()->create();
$product = Product::factory()->create(['partner_id' => $partner->id]);
expect($product->partner->id)->toBe($partner->id);
});
test('product belongs to hub', function () {
$hub = Hub::factory()->create();
$product = Product::factory()->create(['hub_id' => $hub->id]);
expect($product->hub->id)->toBe($hub->id);
});
test('product belongs to brand', function () {
$partner = Partner::factory()->manufacturer()->create();
$brand = Brand::factory()->create(['partner_id' => $partner->id]);
$product = Product::factory()->create(['brand_id' => $brand->id]);
expect($product->brand->id)->toBe($brand->id);
});
test('product belongs to collection', function () {
$collection = Collection::create(['name' => 'Test Kollektion', 'slug' => 'test-kollektion']);
$product = Product::factory()->create(['collection_id' => $collection->id]);
expect($product->collection->id)->toBe($collection->id);
});
test('product can have categories', function () {
$product = Product::factory()->create();
$category = Category::create(['name' => 'Sofas', 'slug' => 'sofas']);
$product->categories()->attach($category);
expect($product->categories)->toHaveCount(1);
expect($product->categories->first()->name)->toBe('Sofas');
});
test('product can have tags', function () {
$product = Product::factory()->create();
$tag = Tag::create(['name' => 'Neu', 'slug' => 'neu']);
$product->tags()->attach($tag);
expect($product->tags)->toHaveCount(1);
expect($product->tags->first()->name)->toBe('Neu');
});
test('product can have media', function () {
$product = Product::factory()->create();
$media = Media::factory()->create([
'model_type' => Product::class,
'model_id' => $product->id,
]);
expect($product->media)->toHaveCount(1);
expect($product->media->first()->file_path)->toBe($media->file_path);
});
test('product scope active filters correctly', function () {
Product::factory()->create(['status' => ProductStatus::Active]);
Product::factory()->create(['status' => ProductStatus::Draft]);
Product::factory()->create(['status' => ProductStatus::Archived]);
expect(Product::active()->count())->toBe(1);
});
test('product scope curated filters correctly', function () {
Product::factory()->create(['is_curated' => true]);
Product::factory()->create(['is_curated' => false]);
expect(Product::curated()->count())->toBe(1);
});
test('product scope localStock filters correctly', function () {
Product::factory()->localStock()->create();
Product::factory()->smartOrder()->create();
expect(Product::localStock()->count())->toBe(1);
expect(Product::smartOrder()->count())->toBe(1);
});
test('product scope inHub filters correctly', function () {
$hub = Hub::factory()->create();
$otherHub = Hub::factory()->create();
Product::factory()->create(['hub_id' => $hub->id]);
Product::factory()->create(['hub_id' => $otherHub->id]);
expect(Product::inHub($hub->id)->count())->toBe(1);
});
test('product scope available combines active curated and available', function () {
Product::factory()->create([
'status' => ProductStatus::Active,
'is_curated' => true,
'is_available' => true,
]);
Product::factory()->create([
'status' => ProductStatus::Active,
'is_curated' => false,
'is_available' => true,
]);
Product::factory()->create([
'status' => ProductStatus::Draft,
'is_curated' => true,
'is_available' => true,
]);
expect(Product::available()->count())->toBe(1);
});
test('product factory has localStock state', function () {
$product = Product::factory()->localStock()->create();
expect($product->product_type)->toBe(ProductType::LocalStock);
});
test('product factory has smartOrder state', function () {
$product = Product::factory()->smartOrder()->create();
expect($product->product_type)->toBe(ProductType::SmartOrder);
});
test('product factory has active state', function () {
$product = Product::factory()->active()->create();
expect($product->status)->toBe(ProductStatus::Active);
expect($product->is_curated)->toBeTrue();
});