Files
cv-roberto/tests/Feature/Controllers/EducationControllerTest.php
Roberto 9df6c0ab46
All checks were successful
Tests / Laravel tests (pull_request) Successful in 3m31s
feat: include testing, factories, git workflow for testing.
2026-06-03 21:23:39 +02:00

111 lines
3.8 KiB
PHP

<?php
use App\Models\Education;
use App\Models\User;
use Illuminate\Http\UploadedFile;
test('guests cannot manage educations', function () {
$education = Education::factory()->create();
$this->get(route('educations.index'))->assertRedirect(route('login'));
$this->get(route('educations.create'))->assertRedirect(route('login'));
$this->post(route('educations.store'), [])->assertRedirect(route('login'));
$this->get(route('educations.edit', $education))->assertRedirect(route('login'));
$this->patch(route('educations.update', $education), [])->assertRedirect(route('login'));
$this->delete(route('educations.destroy', $education))->assertRedirect(route('login'));
});
test('an authenticated user can view the education overview', function () {
$user = User::factory()->create();
$education = Education::factory()->create();
$this->actingAs($user)
->get(route('educations.index'))
->assertOk()
->assertViewIs('educations.index')
->assertViewHas('educations', fn ($educations) => $educations->contains($education));
});
test('an authenticated user can create an education with an image', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->post(route('educations.store'), [
'opleiding' => 'HBO-ICT',
'instituut' => 'Hogeschool Utrecht',
'startdatum' => '2020-09-01',
'einddatum' => '2024-07-01',
'beschrijving' => 'Software engineering en web development.',
'afbeelding' => UploadedFile::fake()->image('education.jpg'),
]);
$response
->assertSessionHasNoErrors()
->assertRedirect(route('educations.index'));
$education = Education::where('opleiding', 'HBO-ICT')->firstOrFail();
$this->assertDatabaseHas('education', [
'id' => $education->id,
'instituut' => 'Hogeschool Utrecht',
]);
$this->assertDatabaseHas('media', [
'model_type' => Education::class,
'model_id' => $education->id,
'collection_name' => 'image',
]);
});
test('an authenticated user can update an education and replace its image', function () {
$user = User::factory()->create();
$education = Education::factory()->current()->create([
'opleiding' => 'HBO-ICT',
'instituut' => 'Hogeschool Utrecht',
'startdatum' => '2020-09-01',
]);
$education
->addMedia(UploadedFile::fake()->image('old-education.jpg'))
->toMediaCollection('image');
$response = $this->actingAs($user)->patch(route('educations.update', $education), [
'opleiding' => 'Software Engineering',
'instituut' => 'Avans Hogeschool',
'startdatum' => '2021-09-01',
'einddatum' => '2025-07-01',
'beschrijving' => 'Verdieping in backend development.',
'afbeelding' => UploadedFile::fake()->image('new-education.jpg'),
]);
$response
->assertSessionHasNoErrors()
->assertRedirect(route('educations.index'));
expect($education->refresh())
->opleiding->toBe('Software Engineering')
->instituut->toBe('Avans Hogeschool')
->getMedia('image')->toHaveCount(1);
});
test('an authenticated user can delete an education and its image', function () {
$user = User::factory()->create();
$education = Education::factory()->current()->create();
$education
->addMedia(UploadedFile::fake()->image('education.jpg'))
->toMediaCollection('image');
$this->actingAs($user)
->delete(route('educations.destroy', $education))
->assertRedirect(route('educations.index'));
$this->assertDatabaseMissing('education', ['id' => $education->id]);
$this->assertDatabaseMissing('media', [
'model_type' => Education::class,
'model_id' => $education->id,
]);
});