From 3c43f6814d61dd05d44c4caa429089c4633b486d Mon Sep 17 00:00:00 2001 From: Adrian Hopek Date: Tue, 29 Mar 2022 15:13:10 +0200 Subject: [PATCH] wip --- .../Providers/ObserverServiceProvider.php | 3 - app/Domain/Actions/CreateUserAction.php | 33 ++++++++++ .../Actions/CreateYearPeriodAction.php} | 14 +++- app/Domain/CalendarGenerator.php | 3 +- app/Domain/TimesheetPerUserSheet.php | 3 +- app/Domain/UserVacationStatsRetriever.php | 2 +- app/Eloquent/Models/User.php | 34 ++++++---- app/Eloquent/Models/Vacation.php | 10 +++ app/Eloquent/Observers/UserObserver.php | 12 ---- .../Http/Controllers/DashboardController.php | 6 +- .../Controllers/MonthlyUsageController.php | 6 +- .../Http/Controllers/UserController.php | 5 +- .../Controllers/VacationRequestController.php | 10 +-- app/Infrastructure/Jobs/CheckYearPeriod.php | 24 ++----- database/seeders/DatabaseSeeder.php | 4 -- database/seeders/DemoSeeder.php | 4 -- resources/js/Pages/Calendar.vue | 2 +- resources/js/Pages/MonthlyUsage.vue | 3 +- resources/js/Pages/VacationLimits.vue | 2 +- resources/js/Shared/MainMenu.vue | 6 +- routes/web.php | 6 +- tests/Feature/VacationLimitTest.php | 17 +++-- tests/Feature/VacationRequestTest.php | 66 +++++++++---------- tests/Traits/InteractsWithYearPeriods.php | 2 +- tests/Unit/VacationLimitTest.php | 14 +++- .../Unit/VacationRequestNotificationTest.php | 14 ++-- tests/Unit/VacationRequestStatesTest.php | 6 +- tests/Unit/YearPeriodRetrieverTest.php | 9 ++- 28 files changed, 176 insertions(+), 144 deletions(-) create mode 100644 app/Domain/Actions/CreateUserAction.php rename app/{Eloquent/Observers/YearPeriodObserver.php => Domain/Actions/CreateYearPeriodAction.php} (80%) diff --git a/app/Architecture/Providers/ObserverServiceProvider.php b/app/Architecture/Providers/ObserverServiceProvider.php index 3409e18..0a7dfa6 100644 --- a/app/Architecture/Providers/ObserverServiceProvider.php +++ b/app/Architecture/Providers/ObserverServiceProvider.php @@ -6,15 +6,12 @@ namespace Toby\Architecture\Providers; use Illuminate\Support\ServiceProvider; use Toby\Eloquent\Models\User; -use Toby\Eloquent\Models\YearPeriod; use Toby\Eloquent\Observers\UserObserver; -use Toby\Eloquent\Observers\YearPeriodObserver; class ObserverServiceProvider extends ServiceProvider { public function boot(): void { User::observe(UserObserver::class); - YearPeriod::observe(YearPeriodObserver::class); } } diff --git a/app/Domain/Actions/CreateUserAction.php b/app/Domain/Actions/CreateUserAction.php new file mode 100644 index 0000000..2107a1c --- /dev/null +++ b/app/Domain/Actions/CreateUserAction.php @@ -0,0 +1,33 @@ +save(); + + $this->createVacationLimitsFor($user); + + return $user; + } + + protected function createVacationLimitsFor(User $user): void + { + $yearPeriods = YearPeriod::all(); + + foreach ($yearPeriods as $yearPeriod) { + $user->vacationLimits()->create([ + "year_period_id" => $yearPeriod->id, + ]); + } + } +} diff --git a/app/Eloquent/Observers/YearPeriodObserver.php b/app/Domain/Actions/CreateYearPeriodAction.php similarity index 80% rename from app/Eloquent/Observers/YearPeriodObserver.php rename to app/Domain/Actions/CreateYearPeriodAction.php index b7f370b..8b38bb6 100644 --- a/app/Eloquent/Observers/YearPeriodObserver.php +++ b/app/Domain/Actions/CreateYearPeriodAction.php @@ -2,22 +2,30 @@ declare(strict_types=1); -namespace Toby\Eloquent\Observers; +namespace Toby\Domain\Actions; use Toby\Domain\PolishHolidaysRetriever; use Toby\Eloquent\Models\User; use Toby\Eloquent\Models\YearPeriod; -class YearPeriodObserver +class CreateYearPeriodAction { public function __construct( protected PolishHolidaysRetriever $polishHolidaysRetriever, ) {} - public function created(YearPeriod $yearPeriod): void + public function execute(int $year): YearPeriod { + $yearPeriod = new YearPeriod([ + "year" => $year, + ]); + + $yearPeriod->save(); + $this->createVacationLimitsFor($yearPeriod); $this->createHolidaysFor($yearPeriod); + + return $yearPeriod; } protected function createVacationLimitsFor(YearPeriod $yearPeriod): void diff --git a/app/Domain/CalendarGenerator.php b/app/Domain/CalendarGenerator.php index 61313cb..30d42b7 100644 --- a/app/Domain/CalendarGenerator.php +++ b/app/Domain/CalendarGenerator.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace Toby\Domain; use Carbon\CarbonPeriod; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Toby\Eloquent\Helpers\YearPeriodRetriever; @@ -55,7 +54,7 @@ class CalendarGenerator { return Vacation::query() ->whereBetween("date", [$period->start, $period->end]) - ->whereRelation("vacationRequest", fn(Builder $query) => $query->states(VacationRequestStatesRetriever::successStates())) + ->approved() ->with("vacationRequest") ->get() ->groupBy(fn(Vacation $vacation) => $vacation->date->toDateString()); diff --git a/app/Domain/TimesheetPerUserSheet.php b/app/Domain/TimesheetPerUserSheet.php index 8e0fa6d..e419463 100644 --- a/app/Domain/TimesheetPerUserSheet.php +++ b/app/Domain/TimesheetPerUserSheet.php @@ -25,7 +25,6 @@ use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Style\NumberFormat; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use Toby\Domain\Enums\VacationType; -use Toby\Domain\States\VacationRequest\Approved; use Toby\Eloquent\Models\Holiday; use Toby\Eloquent\Models\User; use Toby\Eloquent\Models\Vacation; @@ -189,7 +188,7 @@ class TimesheetPerUserSheet implements WithTitle, WithHeadings, WithEvents, With return $user->vacations() ->with("vacationRequest") ->whereBetween("date", [$period->start, $period->end]) - ->whereRelation("vacationRequest", "state", Approved::$name) + ->approved() ->get() ->groupBy( [ diff --git a/app/Domain/UserVacationStatsRetriever.php b/app/Domain/UserVacationStatsRetriever.php index 283399c..dcf8c99 100644 --- a/app/Domain/UserVacationStatsRetriever.php +++ b/app/Domain/UserVacationStatsRetriever.php @@ -88,7 +88,7 @@ class UserVacationStatsRetriever $limit = $user->vacationLimits() ->where("year_period_id", $yearPeriod->id) ->first() - ->days; + ?->days; return $limit ?? 0; } diff --git a/app/Eloquent/Models/User.php b/app/Eloquent/Models/User.php index 03e8b3f..0256927 100644 --- a/app/Eloquent/Models/User.php +++ b/app/Eloquent/Models/User.php @@ -71,18 +71,6 @@ class User extends Authenticatable return $this->hasMany(Vacation::class); } - public function scopeSearch(Builder $query, ?string $text): Builder - { - if ($text === null) { - return $query; - } - - return $query - ->where("first_name", "ILIKE", $text) - ->orWhere("last_name", "ILIKE", $text) - ->orWhere("email", "ILIKE", $text); - } - public function getAvatar(): string { return $this->getAvatarGenerator() @@ -108,6 +96,28 @@ class User extends Authenticatable ->exists(); } + public function scopeSearch(Builder $query, ?string $text): Builder + { + if ($text === null) { + return $query; + } + + return $query + ->where("first_name", "ILIKE", $text) + ->orWhere("last_name", "ILIKE", $text) + ->orWhere("email", "ILIKE", $text); + } + + public function scopeWithVacationLimitIn(Builder $query, YearPeriod $yearPeriod): Builder + { + return $query->whereRelation( + "vacationlimits", + fn(Builder $query) => $query + ->where("year_period_id", $yearPeriod->id) + ->whereNotNull("days"), + ); + } + protected function getAvatarName(): string { return mb_substr($this->first_name, 0, 1) . mb_substr($this->last_name, 0, 1); diff --git a/app/Eloquent/Models/Vacation.php b/app/Eloquent/Models/Vacation.php index c22cf76..65ac662 100644 --- a/app/Eloquent/Models/Vacation.php +++ b/app/Eloquent/Models/Vacation.php @@ -4,10 +4,12 @@ declare(strict_types=1); namespace Toby\Eloquent\Models; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Support\Carbon; +use Toby\Domain\VacationRequestStatesRetriever; /** * @property int $id @@ -40,4 +42,12 @@ class Vacation extends Model { return $this->belongsTo(YearPeriod::class); } + + public function scopeApproved(Builder $query): Builder + { + return $query->whereRelation( + "vacationRequest", + fn(Builder $query) => $query->states(VacationRequestStatesRetriever::successStates()), + ); + } } diff --git a/app/Eloquent/Observers/UserObserver.php b/app/Eloquent/Observers/UserObserver.php index b241609..cec30e6 100644 --- a/app/Eloquent/Observers/UserObserver.php +++ b/app/Eloquent/Observers/UserObserver.php @@ -7,7 +7,6 @@ namespace Toby\Eloquent\Observers; use Illuminate\Contracts\Hashing\Hasher; use Illuminate\Support\Str; use Toby\Eloquent\Models\User; -use Toby\Eloquent\Models\YearPeriod; class UserObserver { @@ -23,15 +22,4 @@ class UserObserver */ $user->password = $this->hash->make(Str::random(40)); } - - public function created(User $user): void - { - $yearPeriods = YearPeriod::all(); - - foreach ($yearPeriods as $yearPeriod) { - $user->vacationLimits()->create([ - "year_period_id" => $yearPeriod->id, - ]); - } - } } diff --git a/app/Infrastructure/Http/Controllers/DashboardController.php b/app/Infrastructure/Http/Controllers/DashboardController.php index f215a93..1a94510 100644 --- a/app/Infrastructure/Http/Controllers/DashboardController.php +++ b/app/Infrastructure/Http/Controllers/DashboardController.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Toby\Infrastructure\Http\Controllers; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; use Illuminate\Support\Carbon; use Inertia\Response; @@ -29,10 +28,7 @@ class DashboardController extends Controller $absences = Vacation::query() ->with(["user", "vacationRequest"]) ->whereDate("date", $now) - ->whereRelation( - "vacationRequest", - fn(Builder $query) => $query->states(VacationRequestStatesRetriever::successStates()), - ) + ->approved() ->get(); if ($user->can("listAll", VacationRequest::class)) { diff --git a/app/Infrastructure/Http/Controllers/MonthlyUsageController.php b/app/Infrastructure/Http/Controllers/MonthlyUsageController.php index c2ab9bd..6b7784b 100644 --- a/app/Infrastructure/Http/Controllers/MonthlyUsageController.php +++ b/app/Infrastructure/Http/Controllers/MonthlyUsageController.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Toby\Infrastructure\Http\Controllers; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; use Inertia\Response; use Toby\Domain\Enums\Month; @@ -26,10 +25,7 @@ class MonthlyUsageController extends Controller $currentUser = $request->user(); $users = User::query() - ->whereRelation( - "vacationlimits", - fn(Builder $query) => $query->where("year_period_id", $currentYearPeriod->id)->whereNotNull("days"), - ) + ->withVacationLimitIn($currentYearPeriod) ->where("id", "!=", $currentUser->id) ->orderBy("last_name") ->orderBy("first_name") diff --git a/app/Infrastructure/Http/Controllers/UserController.php b/app/Infrastructure/Http/Controllers/UserController.php index 46557da..39c35b4 100644 --- a/app/Infrastructure/Http/Controllers/UserController.php +++ b/app/Infrastructure/Http/Controllers/UserController.php @@ -8,6 +8,7 @@ use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Inertia\Response; +use Toby\Domain\Actions\CreateUserAction; use Toby\Domain\Enums\EmploymentForm; use Toby\Domain\Enums\Role; use Toby\Eloquent\Models\User; @@ -54,11 +55,11 @@ class UserController extends Controller /** * @throws AuthorizationException */ - public function store(UserRequest $request): RedirectResponse + public function store(UserRequest $request, CreateUserAction $createUserAction): RedirectResponse { $this->authorize("manageUsers"); - User::query()->create($request->data()); + $createUserAction->execute($request->data()); return redirect() ->route("users.index") diff --git a/app/Infrastructure/Http/Controllers/VacationRequestController.php b/app/Infrastructure/Http/Controllers/VacationRequestController.php index a11c1de..1ea2e0f 100644 --- a/app/Infrastructure/Http/Controllers/VacationRequestController.php +++ b/app/Infrastructure/Http/Controllers/VacationRequestController.php @@ -103,10 +103,7 @@ class VacationRequestController extends Controller ->paginate(); $users = User::query() - ->whereRelation( - "vacationlimits", - fn(Builder $query) => $query->where("year_period_id", $yearPeriod->id)->whereNotNull("days"), - ) + ->withVacationLimitIn($yearPeriod) ->orderBy("last_name") ->orderBy("first_name") ->get(); @@ -164,10 +161,7 @@ class VacationRequestController extends Controller $yearPeriod = $yearPeriodRetriever->selected(); $users = User::query() - ->whereRelation( - "vacationlimits", - fn(Builder $query) => $query->where("year_period_id", $yearPeriod->id)->whereNotNull("days"), - ) + ->withVacationLimitIn($yearPeriod) ->orderBy("last_name") ->orderBy("first_name") ->get(); diff --git a/app/Infrastructure/Jobs/CheckYearPeriod.php b/app/Infrastructure/Jobs/CheckYearPeriod.php index 9922e01..62bf937 100644 --- a/app/Infrastructure/Jobs/CheckYearPeriod.php +++ b/app/Infrastructure/Jobs/CheckYearPeriod.php @@ -8,6 +8,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Support\Carbon; +use Toby\Domain\Actions\CreateYearPeriodAction; use Toby\Eloquent\Models\YearPeriod; class CheckYearPeriod implements ShouldQueue @@ -15,30 +16,17 @@ class CheckYearPeriod implements ShouldQueue use Dispatchable; use Queueable; - public function handle(): void + public function handle(CreateYearPeriodAction $createYearPeriodAction): void { $currentYearPeriod = YearPeriod::current(); + $now = Carbon::now(); if ($currentYearPeriod === null) { - $this->createCurrentYearPeriod(); + $createYearPeriodAction->execute($now->year); } - if (YearPeriod::query()->max("year") === Carbon::now()->year) { - $this->createNextYearPeriod(); + if (YearPeriod::query()->max("year") === $now->year) { + $createYearPeriodAction->execute($now->year + 1); } } - - protected function createCurrentYearPeriod(): void - { - YearPeriod::query()->create([ - "year" => Carbon::now()->year, - ]); - } - - protected function createNextYearPeriod(): void - { - YearPeriod::query()->create([ - "year" => Carbon::now()->year + 1, - ]); - } } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 8d58909..581ecfd 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -17,10 +17,6 @@ class DatabaseSeeder extends Seeder { public function run(): void { - User::unsetEventDispatcher(); - YearPeriod::unsetEventDispatcher(); - VacationRequest::unsetEventDispatcher(); - User::factory(9)->create(); User::factory([ "email" => env("LOCAL_EMAIL_FOR_LOGIN_VIA_GOOGLE"), diff --git a/database/seeders/DemoSeeder.php b/database/seeders/DemoSeeder.php index 7c1bb3c..4d50271 100644 --- a/database/seeders/DemoSeeder.php +++ b/database/seeders/DemoSeeder.php @@ -29,10 +29,6 @@ class DemoSeeder extends Seeder { public function run(): void { - User::unsetEventDispatcher(); - YearPeriod::unsetEventDispatcher(); - VacationRequest::unsetEventDispatcher(); - $user = User::factory([ "first_name" => "Jan", "last_name" => "Kowalski", diff --git a/resources/js/Pages/Calendar.vue b/resources/js/Pages/Calendar.vue index 6014f57..954399f 100644 --- a/resources/js/Pages/Calendar.vue +++ b/resources/js/Pages/Calendar.vue @@ -46,7 +46,7 @@
Pobierz plik Excel diff --git a/resources/js/Pages/MonthlyUsage.vue b/resources/js/Pages/MonthlyUsage.vue index 26a8a1a..6c2cff3 100644 --- a/resources/js/Pages/MonthlyUsage.vue +++ b/resources/js/Pages/MonthlyUsage.vue @@ -81,6 +81,7 @@ import { useMonthInfo } from '@/Composables/monthInfo' import VacationBar from '@/Shared/VacationBar' const props = defineProps({ + years: Object, monthlyUsage: Object, currentMonth: String, }) @@ -89,6 +90,6 @@ const { getMonths } = useMonthInfo() const months = getMonths() function isCurrentMonth(month) { - return props.currentMonth === month.value + return (props.years.selected.year === props.years.current.year && props.currentMonth === month.value) } diff --git a/resources/js/Pages/VacationLimits.vue b/resources/js/Pages/VacationLimits.vue index 139843a..6a33dcf 100644 --- a/resources/js/Pages/VacationLimits.vue +++ b/resources/js/Pages/VacationLimits.vue @@ -14,7 +14,7 @@
- +
Strona główna -
+
- Kliknij, aby wrócić do obecnego roku + Wróc do obecnego roku
diff --git a/routes/web.php b/routes/web.php index fad12be..7eedd7a 100644 --- a/routes/web.php +++ b/routes/web.php @@ -28,7 +28,7 @@ Route::middleware("auth")->group(function (): void { Route::post("year-periods/{yearPeriod}/select", SelectYearPeriodController::class) ->name("year-periods.select"); - Route::prefix("/vacation")->group(function () { + Route::prefix("/vacation")->group(function (): void { Route::get("/limits", [VacationLimitController::class, "edit"]) ->name("vacation.limits"); Route::get("/calendar/{month?}", [VacationCalendarController::class, "index"]) @@ -56,9 +56,9 @@ Route::middleware("auth")->group(function (): void { ->name("vacation.requests.reject"); Route::post("/requests/{vacationRequest}/cancel", [VacationRequestController::class, "cancel"]) ->name("vacation.requests.cancel"); - Route::post("/requests/{vacationRequest}/accept-as-technical", [VacationRequestController::class, "acceptAsTechnical"],) + Route::post("/requests/{vacationRequest}/accept-as-technical", [VacationRequestController::class, "acceptAsTechnical"], ) ->name("vacation.requests.accept-as-technical"); - Route::post("/requests/{vacationRequest}/accept-as-administrative", [VacationRequestController::class, "acceptAsAdministrative"],) + Route::post("/requests/{vacationRequest}/accept-as-administrative", [VacationRequestController::class, "acceptAsAdministrative"], ) ->name("vacation.requests.accept-as-administrative"); Route::get("/monthly-usage", MonthlyUsageController::class) diff --git a/tests/Feature/VacationLimitTest.php b/tests/Feature/VacationLimitTest.php index fe88b59..e8de05e 100644 --- a/tests/Feature/VacationLimitTest.php +++ b/tests/Feature/VacationLimitTest.php @@ -9,6 +9,7 @@ use Inertia\Testing\AssertableInertia as Assert; use Tests\FeatureTestCase; use Toby\Eloquent\Models\User; use Toby\Eloquent\Models\VacationLimit; +use Toby\Eloquent\Models\YearPeriod; class VacationLimitTest extends FeatureTestCase { @@ -16,12 +17,14 @@ class VacationLimitTest extends FeatureTestCase public function testAdminCanSeeVacationLimits(): void { - $admin = User::factory()->admin()->createQuietly(); + $admin = User::factory()->admin()->create(); - User::factory(10)->create(); + VacationLimit::factory(10) + ->for(YearPeriod::current()) + ->create(); $this->actingAs($admin) - ->get("/vacation-limits") + ->get("/vacation/limits") ->assertOk() ->assertInertia( fn(Assert $page) => $page @@ -32,9 +35,11 @@ class VacationLimitTest extends FeatureTestCase public function testAdminCanUpdateVacationLimits(): void { - $admin = User::factory()->admin()->createQuietly(); + $admin = User::factory()->admin()->create(); - User::factory(3)->create(); + VacationLimit::factory(3) + ->for(YearPeriod::current()) + ->create(); [$limit1, $limit2, $limit3] = VacationLimit::all(); @@ -54,7 +59,7 @@ class VacationLimitTest extends FeatureTestCase ]; $this->actingAs($admin) - ->put("/vacation-limits", [ + ->put("/vacation/limits", [ "items" => $data, ]) ->assertRedirect(); diff --git a/tests/Feature/VacationRequestTest.php b/tests/Feature/VacationRequestTest.php index 07bf8a5..60cab63 100644 --- a/tests/Feature/VacationRequestTest.php +++ b/tests/Feature/VacationRequestTest.php @@ -39,7 +39,7 @@ class VacationRequestTest extends FeatureTestCase public function testUserCanSeeVacationRequestsList(): void { - $user = User::factory()->createQuietly(); + $user = User::factory()->create(); $currentYearPeriod = YearPeriod::current(); VacationRequest::factory() @@ -49,7 +49,7 @@ class VacationRequestTest extends FeatureTestCase ->create(); $this->actingAs($user) - ->get("/vacation-requests/me") + ->get("/vacation/requests/me") ->assertOk() ->assertInertia( fn(Assert $page) => $page @@ -60,7 +60,7 @@ class VacationRequestTest extends FeatureTestCase public function testUserCanCreateVacationRequest(): void { - $user = User::factory()->createQuietly(); + $user = User::factory()->create(); $currentYearPeriod = YearPeriod::current(); @@ -72,7 +72,7 @@ class VacationRequestTest extends FeatureTestCase ->create(); $this->actingAs($user) - ->post("/vacation-requests", [ + ->post("/vacation/requests", [ "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 2, 7)->toDateString(), @@ -94,8 +94,8 @@ class VacationRequestTest extends FeatureTestCase public function testUserCanCreateVacationRequestOnEmployeeBehalf(): void { - $creator = User::factory()->admin()->createQuietly(); - $user = User::factory()->createQuietly(); + $creator = User::factory()->admin()->create(); + $user = User::factory()->create(); $currentYearPeriod = YearPeriod::current(); @@ -107,7 +107,7 @@ class VacationRequestTest extends FeatureTestCase ->create(); $this->actingAs($creator) - ->post("/vacation-requests", [ + ->post("/vacation/requests", [ "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 2, 7)->toDateString(), @@ -130,8 +130,8 @@ class VacationRequestTest extends FeatureTestCase public function testUserCanCreateVacationRequestOnEmployeeBehalfAndSkipAcceptanceFlow(): void { - $creator = User::factory()->admin()->createQuietly(); - $user = User::factory()->createQuietly(); + $creator = User::factory()->admin()->create(); + $user = User::factory()->create(); $currentYearPeriod = YearPeriod::current(); @@ -143,7 +143,7 @@ class VacationRequestTest extends FeatureTestCase ->create(); $this->actingAs($creator) - ->post("/vacation-requests", [ + ->post("/vacation/requests", [ "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 2, 7)->toDateString(), @@ -167,8 +167,8 @@ class VacationRequestTest extends FeatureTestCase public function testTechnicalApproverCanApproveVacationRequest(): void { - $user = User::factory()->createQuietly(); - $technicalApprover = User::factory()->technicalApprover()->createQuietly(); + $user = User::factory()->create(); + $technicalApprover = User::factory()->technicalApprover()->create(); $currentYearPeriod = YearPeriod::current(); $vacationRequest = VacationRequest::factory([ @@ -180,7 +180,7 @@ class VacationRequestTest extends FeatureTestCase ->create(); $this->actingAs($technicalApprover) - ->post("/vacation-requests/{$vacationRequest->id}/accept-as-technical") + ->post("/vacation/requests/{$vacationRequest->id}/accept-as-technical") ->assertSessionHasNoErrors(); $vacationRequest->refresh(); @@ -190,8 +190,8 @@ class VacationRequestTest extends FeatureTestCase public function testAdministrativeApproverCanApproveVacationRequest(): void { - $user = User::factory()->createQuietly(); - $administrativeApprover = User::factory()->administrativeApprover()->createQuietly(); + $user = User::factory()->create(); + $administrativeApprover = User::factory()->administrativeApprover()->create(); $currentYearPeriod = YearPeriod::current(); @@ -203,7 +203,7 @@ class VacationRequestTest extends FeatureTestCase ->create(); $this->actingAs($administrativeApprover) - ->post("/vacation-requests/{$vacationRequest->id}/accept-as-administrative") + ->post("/vacation/requests/{$vacationRequest->id}/accept-as-administrative") ->assertSessionHasNoErrors(); $vacationRequest->refresh(); @@ -213,8 +213,8 @@ class VacationRequestTest extends FeatureTestCase public function testTechnicalApproverCanRejectVacationRequest(): void { - $user = User::factory()->createQuietly(); - $technicalApprover = User::factory()->technicalApprover()->createQuietly(); + $user = User::factory()->create(); + $technicalApprover = User::factory()->technicalApprover()->create(); $currentYearPeriod = YearPeriod::current(); VacationLimit::factory([ @@ -234,7 +234,7 @@ class VacationRequestTest extends FeatureTestCase ->create(); $this->actingAs($technicalApprover) - ->post("/vacation-requests/{$vacationRequest->id}/reject") + ->post("/vacation/requests/{$vacationRequest->id}/reject") ->assertSessionHasNoErrors(); $vacationRequest->refresh(); @@ -244,7 +244,7 @@ class VacationRequestTest extends FeatureTestCase public function testUserCannotCreateVacationRequestIfHeExceedsHisVacationLimit(): void { - $user = User::factory()->createQuietly(); + $user = User::factory()->create(); $currentYearPeriod = YearPeriod::current(); VacationLimit::factory([ @@ -255,7 +255,7 @@ class VacationRequestTest extends FeatureTestCase ->create(); $this->actingAs($user) - ->post("/vacation-requests", [ + ->post("/vacation/requests", [ "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 2, 7)->toDateString(), @@ -269,7 +269,7 @@ class VacationRequestTest extends FeatureTestCase public function testUserCannotCreateVacationRequestAtWeekend(): void { - $user = User::factory()->createQuietly(); + $user = User::factory()->create(); $currentYearPeriod = YearPeriod::current(); VacationLimit::factory([ @@ -280,7 +280,7 @@ class VacationRequestTest extends FeatureTestCase ->create(); $this->actingAs($user) - ->post("/vacation-requests", [ + ->post("/vacation/requests", [ "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 2, 5)->toDateString(), @@ -294,7 +294,7 @@ class VacationRequestTest extends FeatureTestCase public function testUserCannotCreateVacationRequestAtHoliday(): void { - $user = User::factory()->createQuietly(); + $user = User::factory()->create(); $currentYearPeriod = YearPeriod::current(); VacationLimit::factory([ @@ -312,7 +312,7 @@ class VacationRequestTest extends FeatureTestCase } $this->actingAs($user) - ->post("/vacation-requests", [ + ->post("/vacation/requests", [ "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 4, 18)->toDateString(), @@ -326,7 +326,7 @@ class VacationRequestTest extends FeatureTestCase public function testUserCannotCreateVacationRequestIfHeHasPendingVacationRequestInThisRange(): void { - $user = User::factory()->createQuietly(); + $user = User::factory()->create(); $currentYearPeriod = YearPeriod::current(); VacationLimit::factory([ @@ -348,7 +348,7 @@ class VacationRequestTest extends FeatureTestCase ->create(); $this->actingAs($user) - ->post("/vacation-requests", [ + ->post("/vacation/requests", [ "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 2, 1)->toDateString(), @@ -362,7 +362,7 @@ class VacationRequestTest extends FeatureTestCase public function testUserCannotCreateVacationRequestIfHeHasApprovedVacationRequestInThisRange(): void { - $user = User::factory()->createQuietly(); + $user = User::factory()->create(); $currentYearPeriod = YearPeriod::current(); VacationLimit::factory([ @@ -384,7 +384,7 @@ class VacationRequestTest extends FeatureTestCase ->create(); $this->actingAs($user) - ->post("/vacation-requests", [ + ->post("/vacation/requests", [ "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 2, 1)->toDateString(), @@ -398,10 +398,10 @@ class VacationRequestTest extends FeatureTestCase public function testUserCannotCreateVacationRequestWithEndDatePriorToTheStartDate(): void { - $user = User::factory()->createQuietly(); + $user = User::factory()->create(); $currentYearPeriod = YearPeriod::current(); $this->actingAs($user) - ->post("/vacation-requests", [ + ->post("/vacation/requests", [ "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 2, 7)->toDateString(), @@ -415,11 +415,11 @@ class VacationRequestTest extends FeatureTestCase public function testUserCannotCreateVacationRequestAtTheTurnOfTheYear(): void { - $user = User::factory()->createQuietly(); + $user = User::factory()->create(); $currentYearPeriod = YearPeriod::current(); $nextYearPeriod = $this->createYearPeriod(Carbon::now()->year + 1); $this->actingAs($user) - ->post("/vacation-requests", [ + ->post("/vacation/requests", [ "user" => $user->id, "type" => VacationType::Vacation->value, "from" => Carbon::create($currentYearPeriod->year, 12, 27)->toDateString(), diff --git a/tests/Traits/InteractsWithYearPeriods.php b/tests/Traits/InteractsWithYearPeriods.php index 6c84de2..982cc7f 100644 --- a/tests/Traits/InteractsWithYearPeriods.php +++ b/tests/Traits/InteractsWithYearPeriods.php @@ -16,7 +16,7 @@ trait InteractsWithYearPeriods public function createYearPeriod(int $year): YearPeriod { /** @var YearPeriod $yearPeriod */ - $yearPeriod = YearPeriod::factory()->createQuietly([ + $yearPeriod = YearPeriod::factory()->create([ "year" => $year, ]); diff --git a/tests/Unit/VacationLimitTest.php b/tests/Unit/VacationLimitTest.php index 726e248..63d4f72 100644 --- a/tests/Unit/VacationLimitTest.php +++ b/tests/Unit/VacationLimitTest.php @@ -7,6 +7,8 @@ namespace Tests\Unit; use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\TestCase; use Tests\Traits\InteractsWithYearPeriods; +use Toby\Domain\Actions\CreateUserAction; +use Toby\Domain\Actions\CreateYearPeriodAction; use Toby\Eloquent\Models\User; use Toby\Eloquent\Models\YearPeriod; @@ -27,7 +29,11 @@ class VacationLimitTest extends TestCase $this->assertDatabaseCount("vacation_limits", 0); $currentYearPeriod = YearPeriod::current(); - $user = User::factory()->create(); + $createUserAction = $this->app->make(CreateUserAction::class); + + $dumpData = User::factory()->raw(); + + $user = $createUserAction->execute($dumpData); $this->assertDatabaseCount("vacation_limits", 1); @@ -40,10 +46,12 @@ class VacationLimitTest extends TestCase public function testWhenYearPeriodIsCreatedThenVacationLimitsForThisYearPeriodAreCreated(): void { $this->assertDatabaseCount("vacation_limits", 0); + $createYearPeriodAction = $this->app->make(CreateYearPeriodAction::class); + $lastYear = YearPeriod::query()->max("year") + 1; - User::factory(10)->createQuietly(); + User::factory(10)->create(); - YearPeriod::factory()->create(); + $createYearPeriodAction->execute($lastYear); $this->assertDatabaseCount("vacation_limits", 10); } diff --git a/tests/Unit/VacationRequestNotificationTest.php b/tests/Unit/VacationRequestNotificationTest.php index 18c4035..f84933f 100644 --- a/tests/Unit/VacationRequestNotificationTest.php +++ b/tests/Unit/VacationRequestNotificationTest.php @@ -39,16 +39,16 @@ class VacationRequestNotificationTest extends TestCase $user = User::factory([ "role" => Role::Employee, - ])->createQuietly(); + ])->create(); $technicalApprover = User::factory([ "role" => Role::TechnicalApprover, - ])->createQuietly(); + ])->create(); $administrativeApprover = User::factory([ "role" => Role::AdministrativeApprover, - ])->createQuietly(); + ])->create(); $admin = User::factory([ "role" => Role::Administrator, - ])->createQuietly(); + ])->create(); $currentYearPeriod = YearPeriod::current(); @@ -78,13 +78,13 @@ class VacationRequestNotificationTest extends TestCase $technicalApprover = User::factory([ "role" => Role::TechnicalApprover, - ])->createQuietly(); + ])->create(); $administrativeApprover = User::factory([ "role" => Role::AdministrativeApprover, - ])->createQuietly(); + ])->create(); $admin = User::factory([ "role" => Role::Administrator, - ])->createQuietly(); + ])->create(); $currentYearPeriod = YearPeriod::current(); diff --git a/tests/Unit/VacationRequestStatesTest.php b/tests/Unit/VacationRequestStatesTest.php index 140b5ca..23c383a 100644 --- a/tests/Unit/VacationRequestStatesTest.php +++ b/tests/Unit/VacationRequestStatesTest.php @@ -40,7 +40,7 @@ class VacationRequestStatesTest extends TestCase public function testAfterCreatingVacationRequestOfTypeVacationItTransitsToProperState(): void { - $user = User::factory()->createQuietly(); + $user = User::factory()->create(); $currentYearPeriod = YearPeriod::current(); @@ -63,7 +63,7 @@ class VacationRequestStatesTest extends TestCase public function testAfterCreatingVacationRequestOfTypeSickVacationItTransitsToProperState(): void { - $user = User::factory()->createQuietly(); + $user = User::factory()->create(); $currentYearPeriod = YearPeriod::current(); @@ -85,7 +85,7 @@ class VacationRequestStatesTest extends TestCase public function testAfterCreatingVacationRequestOfTypeTimeInLieuItTransitsToProperState(): void { - $user = User::factory()->createQuietly(); + $user = User::factory()->create(); $currentYearPeriod = YearPeriod::current(); diff --git a/tests/Unit/YearPeriodRetrieverTest.php b/tests/Unit/YearPeriodRetrieverTest.php index 7b18aa9..9fd95d1 100644 --- a/tests/Unit/YearPeriodRetrieverTest.php +++ b/tests/Unit/YearPeriodRetrieverTest.php @@ -60,7 +60,14 @@ class YearPeriodRetrieverTest extends TestCase public function testLinks(): void { $expected = [ - "current" => $this->current->year, + "current" => [ + "year" => $this->currentYearPeriod->year, + "link" => route("year-periods.select", $this->currentYearPeriod), + ], + "selected" => [ + "year" => $this->currentYearPeriod->year, + "link" => route("year-periods.select", $this->currentYearPeriod), + ], "navigation" => [ [ "year" => $this->previousYearPeriod->year,