Files
cv-roberto/tests/Feature/Controllers/WorkExperienceControllerTest.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.9 KiB
PHP

<?php
use App\Models\User;
use App\Models\WorkExperience;
use Illuminate\Http\UploadedFile;
test('guests cannot manage work experiences', function () {
$experience = WorkExperience::factory()->create();
$this->get(route('work-experiences.index'))->assertRedirect(route('login'));
$this->get(route('work-experiences.create'))->assertRedirect(route('login'));
$this->post(route('work-experiences.store'), [])->assertRedirect(route('login'));
$this->get(route('work-experiences.edit', $experience))->assertRedirect(route('login'));
$this->patch(route('work-experiences.update', $experience), [])->assertRedirect(route('login'));
$this->delete(route('work-experiences.destroy', $experience))->assertRedirect(route('login'));
});
test('an authenticated user can view the work experience overview', function () {
$user = User::factory()->create();
$experience = WorkExperience::factory()->create();
$this->actingAs($user)
->get(route('work-experiences.index'))
->assertOk()
->assertViewIs('work_experiences.index')
->assertViewHas('experiences', fn ($experiences) => $experiences->contains($experience));
});
test('an authenticated user can create a work experience with an image', function () {
$user = User::factory()->create();
$response = $this->actingAs($user)->post(route('work-experiences.store'), [
'werkgever' => 'Acme',
'functie' => 'Laravel Developer',
'startdatum' => '2022-01-01',
'einddatum' => null,
'beschrijving' => 'Bouwde maatwerkapplicaties.',
'afbeelding' => UploadedFile::fake()->image('experience.jpg'),
]);
$response
->assertSessionHasNoErrors()
->assertRedirect(route('work-experiences.index'));
$experience = WorkExperience::where('werkgever', 'Acme')->firstOrFail();
$this->assertDatabaseHas('work_experiences', [
'id' => $experience->id,
'functie' => 'Laravel Developer',
]);
$this->assertDatabaseHas('media', [
'model_type' => WorkExperience::class,
'model_id' => $experience->id,
'collection_name' => 'image',
]);
});
test('an authenticated user can update a work experience and replace its image', function () {
$user = User::factory()->create();
$experience = WorkExperience::factory()->current()->create([
'werkgever' => 'Acme',
'functie' => 'Developer',
'startdatum' => '2022-01-01',
]);
$experience
->addMedia(UploadedFile::fake()->image('old-experience.jpg'))
->toMediaCollection('image');
$response = $this->actingAs($user)->patch(route('work-experiences.update', $experience), [
'werkgever' => 'Globex',
'functie' => 'Senior Laravel Developer',
'startdatum' => '2023-01-01',
'einddatum' => null,
'beschrijving' => 'Leidde backend development.',
'afbeelding' => UploadedFile::fake()->image('new-experience.jpg'),
]);
$response
->assertSessionHasNoErrors()
->assertRedirect(route('work-experiences.index'));
expect($experience->refresh())
->werkgever->toBe('Globex')
->functie->toBe('Senior Laravel Developer')
->getMedia('image')->toHaveCount(1);
});
test('an authenticated user can delete a work experience and its image', function () {
$user = User::factory()->create();
$experience = WorkExperience::factory()->current()->create();
$experience
->addMedia(UploadedFile::fake()->image('experience.jpg'))
->toMediaCollection('image');
$this->actingAs($user)
->delete(route('work-experiences.destroy', $experience))
->assertRedirect(route('work-experiences.index'));
$this->assertDatabaseMissing('work_experiences', ['id' => $experience->id]);
$this->assertDatabaseMissing('media', [
'model_type' => WorkExperience::class,
'model_id' => $experience->id,
]);
});