From 026bfe485f36c902c5514f160d0358ef733cde07 Mon Sep 17 00:00:00 2001 From: Adrian Hopek Date: Tue, 25 Jan 2022 09:02:48 +0100 Subject: [PATCH] #28 - holidays management (#30) * #28 - holidays management * #28 - fix * #28 - fix * #28 - fix * #28 - fix * #28 - fix * #28 - fix * #28 - cr fix --- app/Helpers/PolishHolidaysRetriever.php | 34 ++++ app/Http/Controllers/HolidayController.php | 65 +++++++ app/Http/Requests/HolidayRequest.php | 37 ++++ app/Http/Requests/UserRequest.php | 2 +- .../Resources/HolidayFormDataResource.php | 21 +++ app/Http/Resources/HolidayResource.php | 22 +++ app/Models/Holiday.php | 32 ++++ app/Models/User.php | 2 +- app/Models/YearPeriod.php | 19 +- app/Observers/YearPeriodObserver.php | 20 +++ app/Providers/AppServiceProvider.php | 6 +- app/Rules/YearPeriodExists.php | 24 +++ composer.json | 1 + composer.lock | 75 +++++++- database/factories/HolidayFactory.php | 20 +++ database/factories/UserFactory.php | 3 +- database/factories/YearPeriodFactory.php | 2 +- .../2014_10_12_000000_create_users_table.php | 2 +- ...022_01_20_140544_create_holidays_table.php | 26 +++ database/seeders/DatabaseSeeder.php | 11 ++ resources/js/Pages/Holidays/Create.vue | 106 +++++++++++ resources/js/Pages/Holidays/Edit.vue | 113 ++++++++++++ resources/js/Pages/Holidays/Index.vue | 167 ++++++++++++++++++ resources/js/Pages/Users/Create.vue | 2 +- resources/js/Pages/Users/Edit.vue | 2 +- resources/js/Shared/MainMenu.vue | 3 +- routes/web.php | 5 +- tests/Feature/HolidayTest.php | 127 +++++++++++++ tests/Feature/UserTest.php | 10 +- tests/Traits/InteractsWithYearPeriods.php | 2 +- 30 files changed, 938 insertions(+), 23 deletions(-) create mode 100644 app/Helpers/PolishHolidaysRetriever.php create mode 100644 app/Http/Controllers/HolidayController.php create mode 100644 app/Http/Requests/HolidayRequest.php create mode 100644 app/Http/Resources/HolidayFormDataResource.php create mode 100644 app/Http/Resources/HolidayResource.php create mode 100644 app/Models/Holiday.php create mode 100644 app/Rules/YearPeriodExists.php create mode 100644 database/factories/HolidayFactory.php create mode 100644 database/migrations/2022_01_20_140544_create_holidays_table.php create mode 100644 resources/js/Pages/Holidays/Create.vue create mode 100644 resources/js/Pages/Holidays/Edit.vue create mode 100644 resources/js/Pages/Holidays/Index.vue create mode 100644 tests/Feature/HolidayTest.php diff --git a/app/Helpers/PolishHolidaysRetriever.php b/app/Helpers/PolishHolidaysRetriever.php new file mode 100644 index 0000000..cc80633 --- /dev/null +++ b/app/Helpers/PolishHolidaysRetriever.php @@ -0,0 +1,34 @@ +year); + + $holidays = $polishProvider->getHolidays(); + + return $this->prepareHolidays($holidays); + } + + protected function prepareHolidays(array $holidays): Collection + { + return collect($holidays)->map(fn(Holiday $holiday) => [ + "name" => $holiday->getName([static::LANG_KEY]), + "date" => Carbon::createFromTimestamp($holiday->getTimestamp()), + ])->values(); + } +} diff --git a/app/Http/Controllers/HolidayController.php b/app/Http/Controllers/HolidayController.php new file mode 100644 index 0000000..d82190b --- /dev/null +++ b/app/Http/Controllers/HolidayController.php @@ -0,0 +1,65 @@ +orderBy("date") + ->get(); + + return inertia("Holidays/Index", [ + "holidays" => HolidayResource::collection($holidays), + ]); + } + + public function create(): Response + { + return inertia("Holidays/Create"); + } + + public function store(HolidayRequest $request): RedirectResponse + { + Holiday::query()->create($request->data()); + + return redirect() + ->route("holidays.index") + ->with("success", __("Holiday has been created")); + } + + public function edit(Holiday $holiday): Response + { + return inertia("Holidays/Edit", [ + "holiday" => new HolidayFormDataResource($holiday), + ]); + } + + public function update(HolidayRequest $request, Holiday $holiday): RedirectResponse + { + $holiday->update($request->data()); + + return redirect() + ->route("holidays.index") + ->with("success", __("Holiday has been updated")); + } + + public function destroy(Holiday $holiday): RedirectResponse + { + $holiday->delete(); + + return redirect() + ->route("holidays.index") + ->with("success", __("Holiday has been deleted")); + } +} diff --git a/app/Http/Requests/HolidayRequest.php b/app/Http/Requests/HolidayRequest.php new file mode 100644 index 0000000..9a45b6e --- /dev/null +++ b/app/Http/Requests/HolidayRequest.php @@ -0,0 +1,37 @@ + ["required", "min:3", "max:150"], + "date" => ["required", + "date_format:Y-m-d", + Rule::unique("holidays", "date")->ignore($this->holiday), + new YearPeriodExists(), + ], + ]; + } + + public function data(): array + { + $date = $this->get("date"); + + return [ + "name" => $this->get("name"), + "date" => $date, + "year_period_id" => YearPeriod::findByYear(Carbon::create($date)->year)->id, + ]; + } +} diff --git a/app/Http/Requests/UserRequest.php b/app/Http/Requests/UserRequest.php index 0c1d59e..d48873a 100644 --- a/app/Http/Requests/UserRequest.php +++ b/app/Http/Requests/UserRequest.php @@ -18,7 +18,7 @@ class UserRequest extends FormRequest "lastName" => ["required", "min:3", "max:80"], "email" => ["required", "email", Rule::unique("users", "email")->ignore($this->user)], "employmentForm" => ["required", new Enum(EmploymentForm::class)], - "employmentDate" => ["required", "date"], + "employmentDate" => ["required", "date_format:Y-m-d"], ]; } diff --git a/app/Http/Resources/HolidayFormDataResource.php b/app/Http/Resources/HolidayFormDataResource.php new file mode 100644 index 0000000..dc99444 --- /dev/null +++ b/app/Http/Resources/HolidayFormDataResource.php @@ -0,0 +1,21 @@ + $this->id, + "name" => $this->name, + "date" => $this->date->toDateString(), + ]; + } +} diff --git a/app/Http/Resources/HolidayResource.php b/app/Http/Resources/HolidayResource.php new file mode 100644 index 0000000..3852f2a --- /dev/null +++ b/app/Http/Resources/HolidayResource.php @@ -0,0 +1,22 @@ + $this->id, + "name" => $this->name, + "displayDate" => $this->date->toDisplayString(), + "dayOfWeek" => $this->date->dayName, + ]; + } +} diff --git a/app/Models/Holiday.php b/app/Models/Holiday.php new file mode 100644 index 0000000..f9df73e --- /dev/null +++ b/app/Models/Holiday.php @@ -0,0 +1,32 @@ + "date", + ]; + + public function yearPeriod(): BelongsTo + { + return $this->belongsTo(YearPeriod::class); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 49ccd11..22b2d6b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -34,7 +34,7 @@ class User extends Authenticatable protected $casts = [ "employment_form" => EmploymentForm::class, - "employment_date" => "datetime", + "employment_date" => "date", ]; protected $hidden = [ diff --git a/app/Models/YearPeriod.php b/app/Models/YearPeriod.php index 569ad03..5cc75a1 100644 --- a/app/Models/YearPeriod.php +++ b/app/Models/YearPeriod.php @@ -4,29 +4,33 @@ declare(strict_types=1); namespace Toby\Models; -use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Support\Carbon; use Illuminate\Support\Collection; /** * @property int $id * @property int $year * @property Collection $vacationLimits + * @property Collection $holidays */ class YearPeriod extends Model { use HasFactory; - protected $fillable = [ - "year", - ]; + protected $guarded = []; public static function current(): ?static + { + return static::findByYear(Carbon::now()->year); + } + + public static function findByYear(int $year): ?static { /** @var YearPeriod $year */ - $year = static::query()->where("year", Carbon::now()->year)->first(); + $year = static::query()->where("year", $year)->first(); return $year; } @@ -35,4 +39,9 @@ class YearPeriod extends Model { return $this->hasMany(VacationLimit::class); } + + public function holidays(): HasMany + { + return $this->hasMany(Holiday::class); + } } diff --git a/app/Observers/YearPeriodObserver.php b/app/Observers/YearPeriodObserver.php index 71caa82..cd5beec 100644 --- a/app/Observers/YearPeriodObserver.php +++ b/app/Observers/YearPeriodObserver.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Toby\Observers; +use Toby\Helpers\PolishHolidaysRetriever; use Toby\Helpers\UserAvatarGenerator; use Toby\Models\User; use Toby\Models\YearPeriod; @@ -12,10 +13,17 @@ class YearPeriodObserver { public function __construct( protected UserAvatarGenerator $generator, + protected PolishHolidaysRetriever $polishHolidaysRetriever, ) { } public function created(YearPeriod $yearPeriod): void + { + $this->createVacationLimitsFor($yearPeriod); + $this->createHolidaysFor($yearPeriod); + } + + protected function createVacationLimitsFor(YearPeriod $yearPeriod): void { $users = User::all(); @@ -25,4 +33,16 @@ class YearPeriodObserver ]); } } + + protected function createHolidaysFor(YearPeriod $yearPeriod): void + { + $holidays = $this->polishHolidaysRetriever->getForYearPeriod($yearPeriod); + + foreach ($holidays as $holiday) { + $yearPeriod->holidays()->create([ + "name" => $holiday["name"], + "date" => $holiday["date"], + ]); + } + } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index cf3fe47..b341244 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -6,6 +6,7 @@ namespace Toby\Providers; use Illuminate\Support\Carbon; use Illuminate\Support\ServiceProvider; +use Toby\Models\Holiday; use Toby\Models\VacationLimit; use Toby\Scopes\SelectedYearPeriodScope; @@ -15,6 +16,9 @@ class AppServiceProvider extends ServiceProvider { Carbon::macro("toDisplayString", fn() => $this->translatedFormat("j F Y")); - VacationLimit::addGlobalScope($this->app->make(SelectedYearPeriodScope::class)); + $selectedYearPeriodScope = $this->app->make(SelectedYearPeriodScope::class); + + VacationLimit::addGlobalScope($selectedYearPeriodScope); + Holiday::addGlobalScope($selectedYearPeriodScope); } } diff --git a/app/Rules/YearPeriodExists.php b/app/Rules/YearPeriodExists.php new file mode 100644 index 0000000..ef9ccbc --- /dev/null +++ b/app/Rules/YearPeriodExists.php @@ -0,0 +1,24 @@ +year); + + return $yearPeriod !== null; + } + + public function message(): string + { + return "The year period for given year doesn't exist."; + } +} diff --git a/composer.json b/composer.json index 3d13d3f..b464c21 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,7 @@ "require": { "php": "^8.1", "ext-pdo": "*", + "azuyalabs/yasumi": "^2.4", "fruitcake/laravel-cors": "^2.0", "guzzlehttp/guzzle": "^7.0.1", "inertiajs/inertia-laravel": "^0.5.1", diff --git a/composer.lock b/composer.lock index bc06f33..7984d2b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3412dd2a403927b829237ae4db36351a", + "content-hash": "e3c6ffae4c01db02d0471c52d2370b79", "packages": [ { "name": "asm89/stack-cors", @@ -62,6 +62,79 @@ }, "time": "2022-01-18T09:12:03+00:00" }, + { + "name": "azuyalabs/yasumi", + "version": "2.4.0", + "source": { + "type": "git", + "url": "https://github.com/azuyalabs/yasumi.git", + "reference": "083a0d0579fee17e68d688d463bc01098ac2691f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/azuyalabs/yasumi/zipball/083a0d0579fee17e68d688d463bc01098ac2691f", + "reference": "083a0d0579fee17e68d688d463bc01098ac2691f", + "shasum": "" + }, + "require": { + "ext-json": "*", + "php": ">=7.3" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.16", + "infection/infection": "^0.17 | ^0.22", + "mikey179/vfsstream": "^1.6", + "phan/phan": "^4.0", + "phpstan/phpstan": "^0.12.66", + "phpunit/phpunit": "^8.5 | ^9.4", + "vimeo/psalm": "^4" + }, + "suggest": { + "ext-calendar": "For calculating the date of Easter" + }, + "type": "library", + "autoload": { + "psr-4": { + "Yasumi\\": "src/Yasumi/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Sacha Telgenhof", + "email": "me@sachatelgenhof.com", + "role": "Maintainer" + } + ], + "description": "The easy PHP Library for calculating holidays.", + "homepage": "https://www.yasumi.dev", + "keywords": [ + "Bank", + "calculation", + "calendar", + "celebration", + "date", + "holiday", + "holidays", + "national", + "time" + ], + "support": { + "docs": "https://www.yasumi.dev", + "issues": "https://github.com/azuyalabs/yasumi/issues", + "source": "https://github.com/azuyalabs/yasumi" + }, + "funding": [ + { + "url": "https://www.buymeacoffee.com/sachatelgenhof", + "type": "other" + } + ], + "time": "2021-05-09T09:03:34+00:00" + }, { "name": "brick/math", "version": "0.9.3", diff --git a/database/factories/HolidayFactory.php b/database/factories/HolidayFactory.php new file mode 100644 index 0000000..70da3e8 --- /dev/null +++ b/database/factories/HolidayFactory.php @@ -0,0 +1,20 @@ + $this->faker->word, + "date" => $this->faker->unique->date, + "year_period_id" => YearPeriod::current()->id, + ]; + } +} diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 426ec66..15c06a7 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Support\Carbon; use Illuminate\Support\Str; use Toby\Enums\EmploymentForm; @@ -17,7 +18,7 @@ class UserFactory extends Factory "last_name" => $this->faker->lastName(), "email" => $this->faker->unique()->safeEmail(), "employment_form" => $this->faker->randomElement(EmploymentForm::cases()), - "employment_date" => $this->faker->dateTimeBetween("2020-10-27"), + "employment_date" => Carbon::createFromInterface($this->faker->dateTimeBetween("2020-10-27"))->toDateString(), "remember_token" => Str::random(10), ]; } diff --git a/database/factories/YearPeriodFactory.php b/database/factories/YearPeriodFactory.php index 2437dd3..e0768c7 100644 --- a/database/factories/YearPeriodFactory.php +++ b/database/factories/YearPeriodFactory.php @@ -11,7 +11,7 @@ class YearPeriodFactory extends Factory public function definition(): array { return [ - "year" => $this->faker->unique()->year, + "year" => (int)$this->faker->unique()->year, ]; } } diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index f1bc601..b782864 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -16,7 +16,7 @@ return new class() extends Migration { $table->string("email")->unique(); $table->string("avatar")->nullable(); $table->string("employment_form"); - $table->dateTime("employment_date"); + $table->date("employment_date"); $table->rememberToken(); $table->softDeletes(); $table->timestamps(); diff --git a/database/migrations/2022_01_20_140544_create_holidays_table.php b/database/migrations/2022_01_20_140544_create_holidays_table.php new file mode 100644 index 0000000..941418c --- /dev/null +++ b/database/migrations/2022_01_20_140544_create_holidays_table.php @@ -0,0 +1,26 @@ +id(); + $table->foreignIdFor(YearPeriod::class)->constrained()->cascadeOnDelete(); + $table->string("name"); + $table->date("date")->unique(); + $table->timestamps(); + }); + } + + public function down(): void + { + Schema::dropIfExists("holidays"); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index d6a4f27..e3f9f68 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -7,6 +7,7 @@ namespace Database\Seeders; use Illuminate\Database\Seeder; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; +use Toby\Helpers\PolishHolidaysRetriever; use Toby\Helpers\UserAvatarGenerator; use Toby\Models\User; use Toby\Models\VacationLimit; @@ -54,6 +55,16 @@ class DatabaseSeeder extends Seeder ->create(); } }) + ->afterCreating(function (YearPeriod $yearPeriod): void { + $polishHolidaysRetriever = new PolishHolidaysRetriever(); + + foreach ($polishHolidaysRetriever->getForYearPeriod($yearPeriod) as $holiday) { + $yearPeriod->holidays()->create([ + "name" => $holiday["name"], + "date" => $holiday["date"], + ]); + } + }) ->create(); } diff --git a/resources/js/Pages/Holidays/Create.vue b/resources/js/Pages/Holidays/Create.vue new file mode 100644 index 0000000..70b8bea --- /dev/null +++ b/resources/js/Pages/Holidays/Create.vue @@ -0,0 +1,106 @@ + + + diff --git a/resources/js/Pages/Holidays/Edit.vue b/resources/js/Pages/Holidays/Edit.vue new file mode 100644 index 0000000..5dbd100 --- /dev/null +++ b/resources/js/Pages/Holidays/Edit.vue @@ -0,0 +1,113 @@ + + + diff --git a/resources/js/Pages/Holidays/Index.vue b/resources/js/Pages/Holidays/Index.vue new file mode 100644 index 0000000..3999192 --- /dev/null +++ b/resources/js/Pages/Holidays/Index.vue @@ -0,0 +1,167 @@ + + + diff --git a/resources/js/Pages/Users/Create.vue b/resources/js/Pages/Users/Create.vue index 8f9bacd..1478b4e 100644 --- a/resources/js/Pages/Users/Create.vue +++ b/resources/js/Pages/Users/Create.vue @@ -211,7 +211,7 @@ export default { lastName: null, email: null, employmentForm: props.employmentForms[0], - employmentDate: new Date(), + employmentDate: null, }); return { form }; diff --git a/resources/js/Pages/Users/Edit.vue b/resources/js/Pages/Users/Edit.vue index 771c001..a58e49a 100644 --- a/resources/js/Pages/Users/Edit.vue +++ b/resources/js/Pages/Users/Edit.vue @@ -215,7 +215,7 @@ export default { lastName: props.user.lastName, email: props.user.email, employmentForm: props.employmentForms.find(form => form.value === props.user.employmentForm), - employmentDate: new Date(props.user.employmentDate), + employmentDate: props.user.employmentDate, }); return { form }; diff --git a/resources/js/Shared/MainMenu.vue b/resources/js/Shared/MainMenu.vue index 4966645..ac465eb 100644 --- a/resources/js/Shared/MainMenu.vue +++ b/resources/js/Shared/MainMenu.vue @@ -322,8 +322,7 @@ export default { {name: 'Strona główna', href: '/', current: true}, {name: 'Użytkownicy', href: '/users', current: false}, {name: 'Dostępne urlopy', href: '/vacation-limits', current: false}, - {name: 'Company Directory', href: '#', current: false}, - {name: 'Openings', href: '#', current: false}, + {name: 'Dni wolne', href: '/holidays', current: false}, ]; const userNavigation = [ {name: 'Your Profile', href: '#'}, diff --git a/routes/web.php b/routes/web.php index acd4d43..b6711bc 100644 --- a/routes/web.php +++ b/routes/web.php @@ -4,6 +4,7 @@ declare(strict_types=1); use Illuminate\Support\Facades\Route; use Toby\Http\Controllers\GoogleController; +use Toby\Http\Controllers\HolidayController; use Toby\Http\Controllers\LogoutController; use Toby\Http\Controllers\SelectYearPeriodController; use Toby\Http\Controllers\UserController; @@ -14,7 +15,9 @@ Route::middleware("auth")->group(function (): void { Route::post("/logout", LogoutController::class); Route::resource("users", UserController::class); - Route::post("users/{user}/restore", [UserController::class, "restore"])->withTrashed(); + Route::post("/users/{user}/restore", [UserController::class, "restore"])->withTrashed(); + + Route::resource("holidays", HolidayController::class); Route::get("/vacation-limits", [VacationLimitController::class, "edit"])->name("vacation.limits"); Route::put("/vacation-limits", [VacationLimitController::class, "update"]); diff --git a/tests/Feature/HolidayTest.php b/tests/Feature/HolidayTest.php new file mode 100644 index 0000000..f62d7d2 --- /dev/null +++ b/tests/Feature/HolidayTest.php @@ -0,0 +1,127 @@ +count(10)->create(); + $user = User::factory()->create(); + + $this->assertDatabaseCount("holidays", 10); + + $this->actingAs($user) + ->get("/holidays") + ->assertInertia( + fn(Assert $page) => $page + ->component("Holidays/Index") + ->has("holidays.data", 10), + ); + } + + public function testAdminCanCreateHoliday(): void + { + $admin = User::factory()->create(); + $currentYearPeriod = YearPeriod::current(); + + $this->actingAs($admin) + ->post("/holidays", [ + "name" => "Holiday 1", + "date" => Carbon::create($currentYearPeriod->year, 5, 20)->toDateString(), + ]) + ->assertSessionHasNoErrors(); + + $this->assertDatabaseHas("holidays", [ + "name" => "Holiday 1", + "date" => Carbon::create($currentYearPeriod->year, 5, 20), + "year_period_id" => YearPeriod::current()->id, + ]); + } + + public function testAdminCannotCreateHolidayForYearPeriodThatDoesntExist(): void + { + $admin = User::factory()->create(); + $year = YearPeriod::query()->max("year") + 1; + + $this->actingAs($admin) + ->post("/holidays", [ + "name" => "Holiday 1", + "date" => Carbon::create($year, 5, 20)->toDateString(), + ]) + ->assertSessionHasErrors(["date"]); + } + + public function testAdminCannotCreateHolidayIfGivenDataIsUsed(): void + { + $admin = User::factory()->create(); + $currentYearPeriod = YearPeriod::current(); + $sameDate = Carbon::create($currentYearPeriod->year, 5, 20)->toDateString(); + + Holiday::factory()->create([ + "name" => "Holiday", + "date" => $sameDate, + ]); + + $this->actingAs($admin) + ->post("/holidays", [ + "name" => "Holiday 1", + "date" => $sameDate, + ]) + ->assertSessionHasErrors(["date"]); + } + + public function testAdminCanEditHoliday(): void + { + $admin = User::factory()->create(); + $currentYearPeriod = YearPeriod::current(); + + $holiday = Holiday::factory()->create([ + "name" => "Name to change", + "date" => Carbon::create($currentYearPeriod->year, 5, 20), + ]); + + $this->assertDatabaseHas("holidays", [ + "name" => $holiday->name, + "date" => $holiday->date->toDateString(), + "year_period_id" => $currentYearPeriod->id, + ]); + + $this->actingAs($admin) + ->put("/holidays/{$holiday->id}", [ + "name" => "Holiday 1", + "date" => Carbon::create($currentYearPeriod->year, 10, 25)->toDateString(), + ]) + ->assertSessionHasNoErrors(); + + $this->assertDatabaseHas("holidays", [ + "name" => "Holiday 1", + "date" => Carbon::create($currentYearPeriod->year, 10, 25)->toDateString(), + "year_period_id" => $currentYearPeriod->id, + ]); + } + + public function testAdminCanDeleteHoliday(): void + { + $admin = User::factory()->create(); + $holiday = Holiday::factory()->create(); + + $this->actingAs($admin) + ->delete("/holidays/{$holiday->id}") + ->assertSessionHasNoErrors(); + + $this->assertDeleted($holiday); + } +} diff --git a/tests/Feature/UserTest.php b/tests/Feature/UserTest.php index cbf1277..081e6fe 100644 --- a/tests/Feature/UserTest.php +++ b/tests/Feature/UserTest.php @@ -89,7 +89,7 @@ class UserTest extends FeatureTestCase "lastName" => "Doe", "email" => "john.doe@example.com", "employmentForm" => EmploymentForm::B2B_CONTRACT->value, - "employmentDate" => Carbon::now()->toDateTimeString(), + "employmentDate" => Carbon::now()->toDateString(), ]) ->assertSessionHasNoErrors(); @@ -98,7 +98,7 @@ class UserTest extends FeatureTestCase "last_name" => "Doe", "email" => "john.doe@example.com", "employment_form" => EmploymentForm::B2B_CONTRACT->value, - "employment_date" => Carbon::now()->toDateTimeString(), + "employment_date" => Carbon::now()->toDateString(), ]); } @@ -114,7 +114,7 @@ class UserTest extends FeatureTestCase "last_name" => $user->last_name, "email" => $user->email, "employment_form" => $user->employment_form->value, - "employment_date" => $user->employment_date->toDateTimeString(), + "employment_date" => $user->employment_date->toDateString(), ]); $this->actingAs($admin) @@ -123,7 +123,7 @@ class UserTest extends FeatureTestCase "lastName" => "Doe", "email" => "john.doe@example.com", "employmentForm" => EmploymentForm::B2B_CONTRACT->value, - "employmentDate" => Carbon::now()->toDateTimeString(), + "employmentDate" => Carbon::now()->toDateString(), ]) ->assertSessionHasNoErrors(); @@ -132,7 +132,7 @@ class UserTest extends FeatureTestCase "last_name" => "Doe", "email" => "john.doe@example.com", "employment_form" => EmploymentForm::B2B_CONTRACT->value, - "employment_date" => Carbon::now()->toDateTimeString(), + "employment_date" => Carbon::now()->toDateString(), ]); } diff --git a/tests/Traits/InteractsWithYearPeriods.php b/tests/Traits/InteractsWithYearPeriods.php index fb74ab3..6ab5073 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()->create([ + $yearPeriod = YearPeriod::factory()->createQuietly([ "year" => $year, ]);