From accce6af2befef2dcf938bf6a99eab6711506663 Mon Sep 17 00:00:00 2001 From: EwelinaLasowy Date: Wed, 9 Feb 2022 09:39:13 +0100 Subject: [PATCH] #22 - added some tests --- .../VacationCalendarController.php | 4 +- resources/js/Shared/Activity.vue | 2 +- resources/js/Shared/Status.vue | 2 +- tests/Feature/VacationRequestTest.php | 204 ++++++++++++++++++ tests/Unit/VacationRequestStatesTest.php | 98 +++++++++ 5 files changed, 306 insertions(+), 4 deletions(-) create mode 100644 tests/Feature/VacationRequestTest.php create mode 100644 tests/Unit/VacationRequestStatesTest.php diff --git a/app/Infrastructure/Http/Controllers/VacationCalendarController.php b/app/Infrastructure/Http/Controllers/VacationCalendarController.php index 8c5c7f8..1b8ef75 100644 --- a/app/Infrastructure/Http/Controllers/VacationCalendarController.php +++ b/app/Infrastructure/Http/Controllers/VacationCalendarController.php @@ -73,9 +73,9 @@ class VacationCalendarController extends Controller "april" => CarbonInterface::APRIL, "may" => CarbonInterface::MAY, "june" => CarbonInterface::JUNE, - "julu" => CarbonInterface::JULY, + "july" => CarbonInterface::JULY, "august" => CarbonInterface::AUGUST, - "septemter" => CarbonInterface::SEPTEMBER, + "september" => CarbonInterface::SEPTEMBER, "october" => CarbonInterface::OCTOBER, "november" => CarbonInterface::NOVEMBER, "december" => CarbonInterface::DECEMBER, diff --git a/resources/js/Shared/Activity.vue b/resources/js/Shared/Activity.vue index 7bb239d..8b74fc2 100644 --- a/resources/js/Shared/Activity.vue +++ b/resources/js/Shared/Activity.vue @@ -113,4 +113,4 @@ export default { } }, } - \ No newline at end of file + diff --git a/resources/js/Shared/Status.vue b/resources/js/Shared/Status.vue index 297d9be..4d18057 100644 --- a/resources/js/Shared/Status.vue +++ b/resources/js/Shared/Status.vue @@ -82,4 +82,4 @@ export default { } }, } - \ No newline at end of file + diff --git a/tests/Feature/VacationRequestTest.php b/tests/Feature/VacationRequestTest.php new file mode 100644 index 0000000..6731fc7 --- /dev/null +++ b/tests/Feature/VacationRequestTest.php @@ -0,0 +1,204 @@ +createQuietly(); + $currentYearPeriod = YearPeriod::current(); + VacationRequest::factory() + ->count(10) + ->for($user) + ->for($currentYearPeriod) + ->create(); + + $this->actingAs($user) + ->get("/vacation-requests") + ->assertOk() + ->assertInertia( + fn(Assert $page) => $page + ->component("VacationRequest/Index") + ->has("requests.data", 10), + ); + } + public function testUserCanCreateVacationRequest(): void + { + $user = User::factory()->createQuietly(); + $currentYearPeriod = YearPeriod::current(); + $this->actingAs($user) + ->post("/vacation-requests", [ + "type" => VacationType::VACATION->value, + "from" => Carbon::create($currentYearPeriod->year, 2, 7)->toDateString(), + "to" => Carbon::create($currentYearPeriod->year, 2, 11)->toDateString(), + "comment" => "Comment for the vacation request.", + ]) + ->assertSessionHasNoErrors(); + $this->assertDatabaseHas("vacation_requests", [ + "user_id" => $user->id, + "year_period_id" => $currentYearPeriod->id, + "name" => "1/" . $currentYearPeriod->year, + "type" => VacationType::VACATION->value, + "state" => VacationRequestState::WAITING_FOR_TECHNICAL, + "from" => Carbon::create($currentYearPeriod->year, 2, 7)->toDateString(), + "to" => Carbon::create($currentYearPeriod->year, 2, 11)->toDateString(), + "comment" => "Comment for the vacation request.", + ]); + } + public function testTechnicalApproverCanApproveVacationRequest(): void + { + $user = User::factory()->createQuietly(); + $technicalApprover = User::factory()->createQuietly(); + $currentYearPeriod = YearPeriod::current(); + + $vacationRequest = VacationRequest::factory([ + "state" => VacationRequestState::WAITING_FOR_TECHNICAL, + "type" => VacationType::VACATION, + ]) + ->for($user) + ->for($currentYearPeriod) + ->create(); + + $this->actingAs($technicalApprover) + ->post("/vacation-requests/{$vacationRequest->id}/accept-as-technical") + ->assertSessionHasNoErrors(); + $this->assertDatabaseHas("vacation_requests", [ + "state" => VacationRequestState::WAITING_FOR_ADMINISTRATIVE, + ]); + } + public function testAdministrativeApproverCanApproveVacationRequest(): void + { + $user = User::factory()->createQuietly(); + $administrativeApprover = User::factory()->createQuietly(); + + $currentYearPeriod = YearPeriod::current(); + + $vacationRequest = VacationRequest::factory(["state" => VacationRequestState::WAITING_FOR_ADMINISTRATIVE]) + ->for($user) + ->for($currentYearPeriod) + ->create(); + + $this->actingAs($administrativeApprover) + ->post("/vacation-requests/{$vacationRequest->id}/accept-as-administrative") + ->assertSessionHasNoErrors(); + $this->assertDatabaseHas("vacation_requests", [ + "state" => VacationRequestState::APPROVED, + ]); + } + public function testTechnicalApproverCanRejectVacationRequest(): void + { + $user = User::factory()->createQuietly(); + $technicalApprover = User::factory()->createQuietly(); + $currentYearPeriod = YearPeriod::current(); + + $vacationRequest = VacationRequest::factory([ + "state" => VacationRequestState::WAITING_FOR_TECHNICAL, + "type" => VacationType::VACATION, + ]) + ->for($user) + ->for($currentYearPeriod) + ->create(); + + $this->actingAs($technicalApprover) + ->post("/vacation-requests/{$vacationRequest->id}/reject") + ->assertSessionHasNoErrors(); + $this->assertDatabaseHas("vacation_requests", [ + "state" => VacationRequestState::REJECTED, + ]); + } + public function testUserCannotCreateVacationRequestIfHeHasPendingVacationRequestInThisRange(): void + { + $user = User::factory()->createQuietly(); + $currentYearPeriod = YearPeriod::current(); + + VacationRequest::factory([ + "type" => VacationType::VACATION->value, + "state" => VacationRequestState::WAITING_FOR_TECHNICAL, + "from" => Carbon::create($currentYearPeriod->year, 2, 1)->toDateString(), + "to" => Carbon::create($currentYearPeriod->year, 2, 4)->toDateString(), + "comment" => "Comment for the vacation request.", + ]) + ->for($user) + ->for($currentYearPeriod) + ->create(); + + $this->actingAs($user) + ->post("/vacation-requests", [ + "type" => VacationType::VACATION->value, + "from" => Carbon::create($currentYearPeriod->year, 2, 1)->toDateString(), + "to" => Carbon::create($currentYearPeriod->year, 2, 4)->toDateString(), + "comment" => "Another comment for the another vacation request.", + ]) + ->assertSessionHasErrors([ + "vacationRequest" => trans("You have pending vacation request in this range."), + ]); + } + public function testUserCannotCreateVacationRequestIfHeHasApprovedVacationRequestInThisRange(): void + { + $user = User::factory()->createQuietly(); + $currentYearPeriod = YearPeriod::current(); + + VacationRequest::factory([ + "type" => VacationType::VACATION->value, + "state" => VacationRequestState::APPROVED, + "from" => Carbon::create($currentYearPeriod->year, 2, 2)->toDateString(), + "to" => Carbon::create($currentYearPeriod->year, 2, 4)->toDateString(), + "comment" => "Comment for the vacation request.", + ]) + ->for($user) + ->for($currentYearPeriod) + ->create(); + + $this->actingAs($user) + ->post("/vacation-requests", [ + "type" => VacationType::VACATION->value, + "from" => Carbon::create($currentYearPeriod->year, 2, 1)->toDateString(), + "to" => Carbon::create($currentYearPeriod->year, 2, 4)->toDateString(), + "comment" => "Another comment for the another vacation request.", + ]) + ->assertSessionHasErrors([ + "vacationRequest" => trans("You have approved vacation request in this range."), + ]); + } + public function testUserCannotCreateVacationRequestWithEndDatePriorToTheStartDate(): void + { + $user = User::factory()->createQuietly(); + $currentYearPeriod = YearPeriod::current(); + $this->actingAs($user) + ->post("/vacation-requests", [ + "type" => VacationType::VACATION->value, + "from" => Carbon::create($currentYearPeriod->year, 2, 7)->toDateString(), + "to" => Carbon::create($currentYearPeriod->year, 2, 6)->toDateString(), + "comment" => "Comment for the vacation request.", + ]) + ->assertSessionHasErrors([ + "vacationRequest" => trans("Vacation needs minimum one day."), + ]); + } + public function testUserCannotCreateVacationRequestAtTheTurnOfTheYear(): void + { + $user = User::factory()->createQuietly(); + $currentYearPeriod = YearPeriod::current(); + $nextYearPeriod = $this->createYearPeriod(Carbon::now()->year + 1); + $this->actingAs($user) + ->post("/vacation-requests", [ + "type" => VacationType::VACATION->value, + "from" => Carbon::create($currentYearPeriod->year, 12, 27)->toDateString(), + "to" => Carbon::create($nextYearPeriod->year, 1, 2)->toDateString(), + "comment" => "Comment for the vacation request.", + ]) + ->assertSessionHasErrors([ + "vacationRequest" => trans("The vacation request cannot be created at the turn of the year."), + ]); + } +} \ No newline at end of file diff --git a/tests/Unit/VacationRequestStatesTest.php b/tests/Unit/VacationRequestStatesTest.php new file mode 100644 index 0000000..69cec4b --- /dev/null +++ b/tests/Unit/VacationRequestStatesTest.php @@ -0,0 +1,98 @@ +stateManager = $this->app->make(VacationRequestStateManager::class); + + $this->createCurrentYearPeriod(); + } + + public function testAfterCreatingVacationRequestOfTypeVacationItTransitsToProperState(): void + { + $user = User::factory()->createQuietly(); + + $currentYearPeriod = YearPeriod::current(); + + /** @var VacationRequest $vacationRequest */ + $vacationRequest = VacationRequest::factory([ + "type" => VacationType::VACATION->value, + "state" => VacationRequestState::CREATED, + "from" => Carbon::create($currentYearPeriod->year, 2, 1)->toDateString(), + "to" => Carbon::create($currentYearPeriod->year, 2, 4)->toDateString(), + "comment" => "Comment for the vacation request.", + ]) + ->for($user) + ->for($currentYearPeriod) + ->create(); + + $this->stateManager->waitForTechnical($vacationRequest); + + $this->assertEquals(VacationRequestState::WAITING_FOR_TECHNICAL, $vacationRequest->state); + } + + public function testAfterCreatingVacationRequestOfTypeSickVacationItTransitsToProperState(): void + { + $user = User::factory()->createQuietly(); + + $currentYearPeriod = YearPeriod::current(); + + /** @var VacationRequest $vacationRequest */ + $vacationRequest = VacationRequest::factory([ + "type" => VacationType::SICK_VACATION->value, + "state" => VacationRequestState::CREATED, + "from" => Carbon::create($currentYearPeriod->year, 2, 1)->toDateString(), + "to" => Carbon::create($currentYearPeriod->year, 2, 4)->toDateString(), + ]) + ->for($user) + ->for($currentYearPeriod) + ->create(); + + $this->stateManager->approve($vacationRequest); + + $this->assertEquals(VacationRequestState::APPROVED, $vacationRequest->state); + } + + public function testAfterCreatingVacationRequestOfTypeTimeInLieuItTransitsToProperState(): void + { + $user = User::factory()->createQuietly(); + + $currentYearPeriod = YearPeriod::current(); + + /** @var VacationRequest $vacationRequest */ + $vacationRequest = VacationRequest::factory([ + "type" => VacationType::TIME_IN_LIEU->value, + "state" => VacationRequestState::CREATED, + "from" => Carbon::create($currentYearPeriod->year, 2, 2)->toDateString(), + "to" => Carbon::create($currentYearPeriod->year, 2, 2)->toDateString(), + ]) + ->for($user) + ->for($currentYearPeriod) + ->create(); + + $this->stateManager->approve($vacationRequest); + + $this->assertEquals(VacationRequestState::APPROVED, $vacationRequest->state); + } +} \ No newline at end of file