fix(product): adjust orderable end condition to include equality

Updated the condition for orderable end to include equality, ensuring products are excluded correctly when the current date matches the orderable end date.
This commit is contained in:
Jérémy Munsch 2026-11-15 19:43:45 +01:00
parent 5278c3e3f9
commit d430fa01d2
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
2 changed files with 82 additions and 1 deletions

View file

@ -52,7 +52,7 @@ final class Product implements Entity
if ($this->orderable_start && $target < $this->orderable_start) {
return false;
}
if ($this->orderable_end && $target > $this->orderable_end) {
if ($this->orderable_end && $target >= $this->orderable_end) {
return false;
}

View file

@ -0,0 +1,81 @@
<?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();
});
});