feat(variants): add minimum and maximum use dates to variants and update related logic
This commit is contained in:
@@ -32,11 +32,14 @@ class CatalogItemControllerTest extends TestCase
|
||||
'slug' => 'shirt',
|
||||
'nombre' => 'Shirt',
|
||||
'precio' => 100,
|
||||
'minimum_use_date' => '2026-08-01 09:00:00',
|
||||
'maximum_use_date' => '2026-08-31 18:00:00',
|
||||
'attribute_codes' => [$attribute->codigo],
|
||||
'images' => [$image, $image],
|
||||
'variants' => [
|
||||
[
|
||||
'real_stock' => 5,
|
||||
'maximum_use_date' => '2026-08-15 18:00:00',
|
||||
'values' => ['size' => 'M'],
|
||||
'images' => [$image],
|
||||
],
|
||||
@@ -48,7 +51,20 @@ class CatalogItemControllerTest extends TestCase
|
||||
->assertJsonPath('data.nombre', 'Shirt')
|
||||
->assertJsonCount(2, 'data.images')
|
||||
->assertJsonCount(1, 'data.variants')
|
||||
->assertJsonCount(1, 'data.variants.0.images');
|
||||
->assertJsonCount(1, 'data.variants.0.images')
|
||||
->assertJsonPath('data.variants.0.minimum_use_date', null)
|
||||
->assertJsonPath(
|
||||
'data.variants.0.maximum_use_date',
|
||||
fn (string $value): bool => str_starts_with($value, '2026-08-15T18:00:00'),
|
||||
)
|
||||
->assertJsonPath(
|
||||
'data.variants.0.effective_minimum_use_date',
|
||||
fn (string $value): bool => str_starts_with($value, '2026-08-01T09:00:00'),
|
||||
)
|
||||
->assertJsonPath(
|
||||
'data.variants.0.effective_maximum_use_date',
|
||||
fn (string $value): bool => str_starts_with($value, '2026-08-15T18:00:00'),
|
||||
);
|
||||
|
||||
$item = CatalogItem::query()->where('slug', 'shirt')->firstOrFail();
|
||||
$variant = $item->variants()->firstOrFail();
|
||||
|
||||
@@ -174,4 +174,12 @@ class CatalogSchemaTest extends TestCase
|
||||
'orden' => 3,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_variants_can_override_catalog_item_use_dates(): void
|
||||
{
|
||||
$this->assertTrue(Schema::hasColumns('variantes', [
|
||||
'minimum_use_date',
|
||||
'maximum_use_date',
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ use App\Domains\Catalog\Services\CatalogService;
|
||||
use App\Domains\Shared\Enums\FieldType;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -93,6 +94,73 @@ class CatalogServiceTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function test_variant_use_dates_override_or_inherit_catalog_item_dates(): void
|
||||
{
|
||||
$attribute = $this->createAttribute('day');
|
||||
$itemMinimum = Carbon::parse('2026-08-01 09:00:00');
|
||||
$itemMaximum = Carbon::parse('2026-08-31 18:00:00');
|
||||
$variantMaximum = Carbon::parse('2026-08-15 18:00:00');
|
||||
|
||||
$item = $this->service->create([
|
||||
'tenant_code' => $this->tenant->codigo,
|
||||
'slug' => 'dated-variants',
|
||||
'nombre' => 'Dated variants',
|
||||
'precio' => 100,
|
||||
'minimum_use_date' => $itemMinimum,
|
||||
'maximum_use_date' => $itemMaximum,
|
||||
'attribute_codes' => [$attribute->codigo],
|
||||
'variants' => [
|
||||
[
|
||||
'real_stock' => 5,
|
||||
'maximum_use_date' => $variantMaximum,
|
||||
'values' => [$attribute->codigo => 'Saturday'],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$variant = $item->variants->firstOrFail();
|
||||
|
||||
$this->assertNull($variant->minimum_use_date);
|
||||
$this->assertTrue($variant->maximum_use_date->equalTo($variantMaximum));
|
||||
$this->assertTrue($variant->getMinimumUseDate()->equalTo($itemMinimum));
|
||||
$this->assertTrue($variant->getMaximumUseDate()->equalTo($variantMaximum));
|
||||
}
|
||||
|
||||
public function test_it_rejects_an_invalid_effective_variant_use_date_range(): void
|
||||
{
|
||||
$attribute = $this->createAttribute('day');
|
||||
|
||||
try {
|
||||
$this->service->create([
|
||||
'tenant_code' => $this->tenant->codigo,
|
||||
'slug' => 'invalid-dated-variant',
|
||||
'nombre' => 'Invalid dated variant',
|
||||
'precio' => 100,
|
||||
'minimum_use_date' => '2026-08-10 09:00:00',
|
||||
'maximum_use_date' => '2026-08-31 18:00:00',
|
||||
'attribute_codes' => [$attribute->codigo],
|
||||
'variants' => [
|
||||
[
|
||||
'real_stock' => 5,
|
||||
'maximum_use_date' => '2026-08-09 18:00:00',
|
||||
'values' => [$attribute->codigo => 'Saturday'],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->fail('A validation exception was not thrown.');
|
||||
} catch (ValidationException $exception) {
|
||||
$this->assertArrayHasKey(
|
||||
'variants.0.maximum_use_date',
|
||||
$exception->errors(),
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertDatabaseMissing('catalog_items', [
|
||||
'slug' => 'invalid-dated-variant',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_rejects_direct_inventory_together_with_variants(): void
|
||||
{
|
||||
$attribute = $this->createAttribute('size');
|
||||
|
||||
Reference in New Issue
Block a user