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\Support\Carbon;
use Illuminate\Validation\Rule;
use Toby\Models\YearPeriod;
use Toby\Rules\YearPeriodExists;
@ -15,7 +16,10 @@ class HolidayRequest extends FormRequest
{
return [
"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"],
"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"],
];
}

View File

@ -19,6 +19,6 @@ class YearPeriodExists implements Rule
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;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;
use Toby\Models\YearPeriod;
class HolidayFactory extends Factory
{
public function definition(): array
{
$now = Carbon::now();
return [
"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
{
return [
"year" => $this->faker->unique()->year,
"year" => (int)$this->faker->unique()->year,
];
}
}

View File

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

View File

@ -85,7 +85,7 @@ import { useForm } from '@inertiajs/inertia-vue3';
import FlatPickr from 'vue-flatpickr-component';
export default {
name: 'HolidayCreate',
name: 'HolidayEdit',
components: {
FlatPickr,
},
@ -98,14 +98,15 @@ export default {
setup(props) {
const form = useForm({
name: props.holiday.name,
date: new Date(props.holiday.date),
date: props.holiday.date,
});
return { form };
},
methods: {
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,
email: null,
employmentForm: props.employmentForms[0],
employmentDate: new Date(),
employmentDate: null,
});
return { form };

View File

@ -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 };

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",
"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(),
]);
}
@ -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(),
]);
}

View File

@ -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,
]);