Updated the condition for orderable end to include equality, ensuring products are excluded correctly when the current date matches the orderable end date.
81 lines
3 KiB
PHP
81 lines
3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
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 Ramsey\Uuid\Uuid;
|
|
|
|
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 = Uuid::uuid7()->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();
|
|
});
|
|
});
|