From 858f2e075f3e68dc49c161be2b703933eafde8cb Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Thu, 23 Apr 2026 20:50:26 +0300 Subject: [PATCH] test for date end being before date start --- app/Plan/UseCases/CreatePlan.php | 7 +++++++ tests/Unit/Plan/UseCases/CreatePlanTest.php | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/app/Plan/UseCases/CreatePlan.php b/app/Plan/UseCases/CreatePlan.php index 7bad774..0be80cb 100644 --- a/app/Plan/UseCases/CreatePlan.php +++ b/app/Plan/UseCases/CreatePlan.php @@ -50,6 +50,13 @@ class CreatePlan throw new BadRequestException('date end is required'); } + $startDate = new DateTimeImmutable($request->dateStart); + $endDate = new DateTimeImmutable($request->dateEnd); + + if ($endDate < $startDate) { + throw new BadRequestException('date end cannot be before date start'); + } + $userId = $request->userId; $user = $this->userRepo->find($userId); if ($user === null) { diff --git a/tests/Unit/Plan/UseCases/CreatePlanTest.php b/tests/Unit/Plan/UseCases/CreatePlanTest.php index 0cdad79..89ba509 100644 --- a/tests/Unit/Plan/UseCases/CreatePlanTest.php +++ b/tests/Unit/Plan/UseCases/CreatePlanTest.php @@ -216,4 +216,18 @@ class CreatePlanTest extends TestCase dateEnd: null, )); } + + public function test_throws_if_date_end_is_before_date_start(): void + { + $this->expectException(BadRequestException::class); + $this->expectExceptionMessage('date end cannot be before date start'); + + $this->useCase->execute(new CreatePlanRequest( + userId: 0, + name: 'test', + textId: 0, + dateStart: '2025-01-02', + dateEnd: '2025-01-01', + )); + } }