This commit is contained in:
Adrian Hopek 2022-01-24 14:57:59 +01:00
parent 595bb707e4
commit ed2fc06caa
12 changed files with 150 additions and 19 deletions

View File

@ -6,6 +6,7 @@ namespace Toby\Http\Requests;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Validation\Rule;
use Toby\Models\YearPeriod; use Toby\Models\YearPeriod;
use Toby\Rules\YearPeriodExists; use Toby\Rules\YearPeriodExists;
@ -15,7 +16,10 @@ class HolidayRequest extends FormRequest
{ {
return [ return [
"name" => ["required", "min:3", "max:150"], "name" => ["required", "min:3", "max:150"],
"date" => ["required", "date", new YearPeriodExists()], "date" => ["required",
"date_format:Y-m-d",
Rule::unique("holidays", "date")->ignore($this->holiday),
new YearPeriodExists(), ],
]; ];
} }

View File

@ -18,7 +18,7 @@ class UserRequest extends FormRequest
"lastName" => ["required", "min:3", "max:80"], "lastName" => ["required", "min:3", "max:80"],
"email" => ["required", "email", Rule::unique("users", "email")->ignore($this->user)], "email" => ["required", "email", Rule::unique("users", "email")->ignore($this->user)],
"employmentForm" => ["required", new Enum(EmploymentForm::class)], "employmentForm" => ["required", new Enum(EmploymentForm::class)],
"employmentDate" => ["required", "date"], "employmentDate" => ["required", "date_format:Y-m-d"],
]; ];
} }

View File

@ -19,6 +19,6 @@ class YearPeriodExists implements Rule
public function message(): string public function message(): string
{ {
return "The year period for given year doesn't exist"; return "The year period for given year doesn't exist.";
} }
} }

View File

@ -5,17 +5,16 @@ declare(strict_types=1);
namespace Database\Factories; namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon; use Toby\Models\YearPeriod;
class HolidayFactory extends Factory class HolidayFactory extends Factory
{ {
public function definition(): array public function definition(): array
{ {
$now = Carbon::now();
return [ return [
"name" => $this->faker->word, "name" => $this->faker->word,
"date" => $this->faker->dateTimeBetween($now->startOfYear(), $now->endOfYear()), "date" => $this->faker->unique->date,
"year_period_id" => YearPeriod::current()->id,
]; ];
} }
} }

View File

@ -11,7 +11,7 @@ class YearPeriodFactory extends Factory
public function definition(): array public function definition(): array
{ {
return [ return [
"year" => $this->faker->unique()->year, "year" => (int)$this->faker->unique()->year,
]; ];
} }
} }

View File

@ -92,7 +92,7 @@ export default {
setup() { setup() {
const form = useForm({ const form = useForm({
name: null, name: null,
date: new Date(), date: null,
}); });
return { form }; return { form };

View File

@ -85,7 +85,7 @@ import { useForm } from '@inertiajs/inertia-vue3';
import FlatPickr from 'vue-flatpickr-component'; import FlatPickr from 'vue-flatpickr-component';
export default { export default {
name: 'HolidayCreate', name: 'HolidayEdit',
components: { components: {
FlatPickr, FlatPickr,
}, },
@ -98,14 +98,15 @@ export default {
setup(props) { setup(props) {
const form = useForm({ const form = useForm({
name: props.holiday.name, name: props.holiday.name,
date: new Date(props.holiday.date), date: props.holiday.date,
}); });
return { form }; return { form };
}, },
methods: { methods: {
editHoliday() { editHoliday() {
this.form.put(`/holidays/${this.holiday.id}`); this.form
.put(`/holidays/${this.holiday.id}`);
}, },
}, },
}; };

View File

@ -211,7 +211,7 @@ export default {
lastName: null, lastName: null,
email: null, email: null,
employmentForm: props.employmentForms[0], employmentForm: props.employmentForms[0],
employmentDate: new Date(), employmentDate: null,
}); });
return { form }; return { form };

View File

@ -215,7 +215,7 @@ export default {
lastName: props.user.lastName, lastName: props.user.lastName,
email: props.user.email, email: props.user.email,
employmentForm: props.employmentForms.find(form => form.value === props.user.employmentForm), employmentForm: props.employmentForms.find(form => form.value === props.user.employmentForm),
employmentDate: new Date(props.user.employmentDate), employmentDate: props.user.employmentDate,
}); });
return { form }; return { form };

View File

@ -0,0 +1,127 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Carbon;
use Inertia\Testing\AssertableInertia as Assert;
use Tests\FeatureTestCase;
use Toby\Models\Holiday;
use Toby\Models\User;
use Toby\Models\YearPeriod;
class HolidayTest extends FeatureTestCase
{
use DatabaseMigrations;
public function testUserCanSeeHolidayList(): void
{
Holiday::factory()->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 testAdminCanDeleteUser(): void
{
$admin = User::factory()->create();
$holiday = Holiday::factory()->create();
$this->actingAs($admin)
->delete("/holidays/{$holiday->id}")
->assertSessionHasNoErrors();
$this->assertDeleted($holiday);
}
}

View File

@ -89,7 +89,7 @@ class UserTest extends FeatureTestCase
"lastName" => "Doe", "lastName" => "Doe",
"email" => "john.doe@example.com", "email" => "john.doe@example.com",
"employmentForm" => EmploymentForm::B2B_CONTRACT->value, "employmentForm" => EmploymentForm::B2B_CONTRACT->value,
"employmentDate" => Carbon::now()->toDateTimeString(), "employmentDate" => Carbon::now()->toDateString(),
]) ])
->assertSessionHasNoErrors(); ->assertSessionHasNoErrors();
@ -98,7 +98,7 @@ class UserTest extends FeatureTestCase
"last_name" => "Doe", "last_name" => "Doe",
"email" => "john.doe@example.com", "email" => "john.doe@example.com",
"employment_form" => EmploymentForm::B2B_CONTRACT->value, "employment_form" => EmploymentForm::B2B_CONTRACT->value,
"employment_date" => Carbon::now()->toDateTimeString(), "employment_date" => Carbon::now()->toDateString(),
]); ]);
} }
@ -123,7 +123,7 @@ class UserTest extends FeatureTestCase
"lastName" => "Doe", "lastName" => "Doe",
"email" => "john.doe@example.com", "email" => "john.doe@example.com",
"employmentForm" => EmploymentForm::B2B_CONTRACT->value, "employmentForm" => EmploymentForm::B2B_CONTRACT->value,
"employmentDate" => Carbon::now()->toDateTimeString(), "employmentDate" => Carbon::now()->toDateString(),
]) ])
->assertSessionHasNoErrors(); ->assertSessionHasNoErrors();
@ -132,7 +132,7 @@ class UserTest extends FeatureTestCase
"last_name" => "Doe", "last_name" => "Doe",
"email" => "john.doe@example.com", "email" => "john.doe@example.com",
"employment_form" => EmploymentForm::B2B_CONTRACT->value, "employment_form" => EmploymentForm::B2B_CONTRACT->value,
"employment_date" => Carbon::now()->toDateTimeString(), "employment_date" => Carbon::now()->toDateString(),
]); ]);
} }

View File

@ -16,7 +16,7 @@ trait InteractsWithYearPeriods
public function createYearPeriod(int $year): YearPeriod public function createYearPeriod(int $year): YearPeriod
{ {
/** @var YearPeriod $yearPeriod */ /** @var YearPeriod $yearPeriod */
$yearPeriod = YearPeriod::factory()->create([ $yearPeriod = YearPeriod::factory()->createQuietly([
"year" => $year, "year" => $year,
]); ]);