131 lines
4.9 KiB
PHP
131 lines
4.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Integration\WithApp\App\Models;
|
|
|
|
use App\Models\Product;
|
|
use App\Models\ProductInco;
|
|
use App\Models\Range;
|
|
use Domain\Enums\ProductBrand;
|
|
use Domain\Enums\SellUnit;
|
|
use Domain\Enums\TaxRate;
|
|
use Domain\Enums\Units\InvoicingUnit;
|
|
use Domain\Enums\Units\OrderUnit;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Money\Money;
|
|
use Shared\Helpers\UuidHelper;
|
|
|
|
function createOrderableTestProduct(
|
|
Range $range,
|
|
?\DateTimeInterface $orderableStart = null,
|
|
?\DateTimeInterface $orderableEnd = null,
|
|
): Product {
|
|
$product = new Product([
|
|
'range_id' => $range->id,
|
|
'name' => 'Produit ' . uniqid(),
|
|
'brand' => ProductBrand::loue,
|
|
'invoicing_unit' => InvoicingUnit::PCE,
|
|
'sell_unit' => SellUnit::PCE,
|
|
'order_unit' => OrderUnit::PCE,
|
|
'tax_rate' => TaxRate::TVA_20,
|
|
'description' => 'Description test',
|
|
'status' => true,
|
|
'orderable_start' => $orderableStart,
|
|
'orderable_end' => $orderableEnd,
|
|
]);
|
|
$product->id = UuidHelper::v7()->toString();
|
|
$product->order = random_int(1, 999999);
|
|
$product->price = Money::EUR(1000);
|
|
$product->short_description = 'Description courte';
|
|
$product->picture_alt = 'alt';
|
|
$product->picture_title = 'title';
|
|
$product->picture_large_alt = 'alt';
|
|
$product->picture_large_title = 'title';
|
|
$product->saveOrFail();
|
|
|
|
$inco = new ProductInco();
|
|
$inco->no_art = random_int(10000, 99999);
|
|
$inco->product()->associate($product);
|
|
$inco->saveOrFail();
|
|
|
|
return $product;
|
|
}
|
|
|
|
describe('Product::scopeOrderable', function (): void {
|
|
it('includes product when current date equals orderable_start and excludes when current date equals orderable_end', function (): void {
|
|
// GIVEN
|
|
$now = Date::parse('2026-11-15 12:00:00');
|
|
Date::setTestNow($now);
|
|
|
|
$range = new Range();
|
|
$range->name = 'Gamme ' . uniqid();
|
|
$range->status = true;
|
|
$range->order = 1;
|
|
$range->picture_alt = 'alt';
|
|
$range->picture_title = 'title';
|
|
$range->saveOrFail();
|
|
|
|
$startsNow = createOrderableTestProduct($range, orderableStart: $now, orderableEnd: $now->copy()->addHour());
|
|
$endsNow = createOrderableTestProduct($range, orderableStart: $now->copy()->subHour(), orderableEnd: $now);
|
|
$inRange = createOrderableTestProduct($range, orderableStart: $now->copy()->subHour(), orderableEnd: $now->copy()->addHour());
|
|
$future = createOrderableTestProduct($range, orderableStart: $now->copy()->addSecond(), orderableEnd: $now->copy()->addHour());
|
|
|
|
// WHEN
|
|
$orderableIds = Product::query()->orderable()->pluck('id')->all();
|
|
|
|
// THEN
|
|
expect($orderableIds)->toContain($startsNow->id, $inRange->id)
|
|
->and($orderableIds)->not->toContain($endsNow->id, $future->id);
|
|
|
|
Date::setTestNow();
|
|
});
|
|
});
|
|
|
|
describe('Product order auto-increment with SoftDeletes', function (): void {
|
|
it('computes max order including soft-deleted products when saving a new product without explicit order', function (): void {
|
|
// GIVEN
|
|
$range = new Range();
|
|
$range->name = 'Gamme Order ' . uniqid();
|
|
$range->status = true;
|
|
$range->order = 1;
|
|
$range->picture_alt = 'alt';
|
|
$range->picture_title = 'title';
|
|
$range->saveOrFail();
|
|
|
|
$makeProduct = static function (string $name) use ($range): Product {
|
|
$product = new Product([
|
|
'range_id' => $range->id,
|
|
'name' => $name,
|
|
'brand' => ProductBrand::loue,
|
|
'invoicing_unit' => InvoicingUnit::PCE,
|
|
'sell_unit' => SellUnit::PCE,
|
|
'order_unit' => OrderUnit::PCE,
|
|
'tax_rate' => TaxRate::TVA_20,
|
|
'description' => 'Description',
|
|
'status' => true,
|
|
]);
|
|
$product->price = Money::EUR(1000);
|
|
$product->short_description = 'Court';
|
|
$product->picture_alt = 'alt';
|
|
$product->picture_title = 'title';
|
|
$product->picture_large_alt = 'alt';
|
|
$product->picture_large_title = 'title';
|
|
$product->saveOrFail();
|
|
|
|
return $product;
|
|
};
|
|
|
|
$first = $makeProduct('Produit 1 ' . uniqid());
|
|
$second = $makeProduct('Produit 2 ' . uniqid());
|
|
$second->delete();
|
|
|
|
// WHEN
|
|
$third = $makeProduct('Produit 3 ' . uniqid());
|
|
|
|
// THEN
|
|
expect($first->order)->toBe(1)
|
|
->and($second->order)->toBe(2)
|
|
->and($third->order)->toBe(3);
|
|
});
|
|
});
|