diff --git a/app/Models/Discount.php b/app/Models/Discount.php index 5c561d63..2d3bb81d 100644 --- a/app/Models/Discount.php +++ b/app/Models/Discount.php @@ -54,8 +54,12 @@ class Discount extends Model protected function scopePublished(Builder $query): Builder { return $query - ->where('date_start', '<=', Date::now()) - ->where('date_end', '>=', Date::now()); + ->where(function (Builder $q): void { + $q->whereNull('date_start')->orWhere('date_start', '<=', Date::now()); + }) + ->where(function (Builder $q): void { + $q->whereNull('date_end')->orWhere('date_end', '>=', Date::now()); + }); } /** @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\Product,$this> */ diff --git a/tests/Integration/WithApp/App/Models/DiscountTest.php b/tests/Integration/WithApp/App/Models/DiscountTest.php new file mode 100644 index 00000000..e65b5976 --- /dev/null +++ b/tests/Integration/WithApp/App/Models/DiscountTest.php @@ -0,0 +1,86 @@ + published + $permanent = new Discount(); + $permanent->type = DiscountType::PERCENT; + $permanent->value = DiscountValue::fromPercentage(10); + $permanent->is_old_price_displayed = true; + $permanent->date_start = null; + $permanent->date_end = null; + $permanent->save(); + + // 2. Immediate discount with end date in future -> published + $immediate = new Discount(); + $immediate->type = DiscountType::PERCENT; + $immediate->value = DiscountValue::fromPercentage(15); + $immediate->is_old_price_displayed = true; + $immediate->date_start = null; + $immediate->date_end = Carbon::parse('2026-06-20 23:59:59'); + $immediate->save(); + + // 3. Started discount with no end date -> published + $ongoing = new Discount(); + $ongoing->type = DiscountType::PERCENT; + $ongoing->value = DiscountValue::fromPercentage(20); + $ongoing->is_old_price_displayed = true; + $ongoing->date_start = Carbon::parse('2026-06-10 00:00:00'); + $ongoing->date_end = null; + $ongoing->save(); + + // 4. Bounded active discount -> published + $bounded = new Discount(); + $bounded->type = DiscountType::PERCENT; + $bounded->value = DiscountValue::fromPercentage(25); + $bounded->is_old_price_displayed = true; + $bounded->date_start = Carbon::parse('2026-06-10 00:00:00'); + $bounded->date_end = Carbon::parse('2026-06-20 23:59:59'); + $bounded->save(); + + // 5. Future discount -> not published + $future = new Discount(); + $future->type = DiscountType::PERCENT; + $future->value = DiscountValue::fromPercentage(30); + $future->is_old_price_displayed = true; + $future->date_start = Carbon::parse('2026-06-20 00:00:00'); + $future->date_end = Carbon::parse('2026-06-25 23:59:59'); + $future->save(); + + // 6. Expired discount -> not published + $expired = new Discount(); + $expired->type = DiscountType::PERCENT; + $expired->value = DiscountValue::fromPercentage(35); + $expired->is_old_price_displayed = true; + $expired->date_start = Carbon::parse('2026-06-01 00:00:00'); + $expired->date_end = Carbon::parse('2026-06-10 23:59:59'); + $expired->save(); + + // WHEN + $publishedDiscounts = Discount::query()->published()->get(); + + // THEN + $publishedIds = $publishedDiscounts->pluck('id')->all(); + + expect($publishedIds)->toContain($permanent->id) + ->and($publishedIds)->toContain($immediate->id) + ->and($publishedIds)->toContain($ongoing->id) + ->and($publishedIds)->toContain($bounded->id) + ->and($publishedIds)->not->toContain($future->id) + ->and($publishedIds)->not->toContain($expired->id); + + Carbon::setTestNow(); + }); +});