Files
cv-roberto/tests/Feature/Controllers/FrontendControllerTest.php
Roberto bbfc64031f
Some checks failed
Tests / Laravel tests (pull_request) Failing after 6m57s
Add page visit Telegram notifications
2026-06-03 23:19:06 +02:00

155 lines
4.7 KiB
PHP

<?php
use App\Jobs\NotifyTelegramAboutContactMessage;
use App\Jobs\NotifyTelegramAboutPageVisit;
use App\Jobs\NotifyTelegramAboutPersonaliaClick;
use App\Models\Education;
use App\Models\Personalia;
use App\Models\Skill;
use App\Models\WorkExperience;
use Illuminate\Support\Facades\Queue;
test('the homepage shows the public cv data', function () {
$skill = Skill::factory()->rating()->create([
'title' => 'Laravel',
'description' => 'Framework expertise',
'rating' => 8,
]);
$personalium = Personalia::factory()->hidden()->create([
'key' => 'Email',
'value' => 'roberto@example.com',
'icon' => 'fa-solid fa-envelope',
]);
$education = Education::factory()->create([
'opleiding' => 'HBO-ICT',
'instituut' => 'Hogeschool Utrecht',
'startdatum' => '2020-09-01',
'einddatum' => '2024-07-01',
]);
$experience = WorkExperience::factory()->current()->create([
'werkgever' => 'Acme',
'functie' => 'Developer',
'startdatum' => '2022-01-01',
]);
$this->get(route('home'))
->assertOk()
->assertViewIs('welcome')
->assertViewHas('skills', fn ($skills) => $skills->get('rating')->contains($skill))
->assertViewHas('personalia', fn ($personalia) => $personalia->contains($personalium))
->assertViewHas('education', fn ($educations) => $educations->contains($education))
->assertViewHas('experience', fn ($experiences) => $experiences->contains($experience));
});
test('a hidden personalia value can be requested and the click is queued for notification', function () {
Queue::fake();
$personalium = Personalia::factory()->hidden()->create([
'key' => 'Email',
'value' => 'roberto@example.com',
'icon' => 'fa-solid fa-envelope',
]);
$this->withHeader('User-Agent', 'Pest Browser')
->getJson(route('personalia', $personalium))
->assertOk()
->assertJson([
'value' => 'roberto@example.com',
]);
Queue::assertPushed(NotifyTelegramAboutPersonaliaClick::class);
});
test('requesting unknown personalia returns not found', function () {
Queue::fake();
$this->getJson(route('personalia', 999))->assertNotFound();
Queue::assertNotPushed(NotifyTelegramAboutPersonaliaClick::class);
});
test('a contact message can be submitted and is queued for notification', function () {
Queue::fake();
$this->withHeader('User-Agent', 'Pest Browser')
->postJson(route('contact'), [
'name' => 'Roberto',
'email' => 'roberto@example.com',
'phone' => '+31612345678',
'message' => 'Hoi, ik wil graag contact opnemen.',
])
->assertOk()
->assertJson([
'status' => 'success',
]);
Queue::assertPushed(NotifyTelegramAboutContactMessage::class);
});
test('a contact message requires a name and message', function () {
Queue::fake();
$this->postJson(route('contact'), [
'email' => 'not-an-email',
])
->assertUnprocessable()
->assertJsonValidationErrors(['name', 'message', 'email']);
Queue::assertNotPushed(NotifyTelegramAboutContactMessage::class);
});
test('a page visit can be submitted and is queued for notification', function () {
Queue::fake();
$this->withHeaders([
'User-Agent' => 'Pest Browser',
'Accept-Language' => 'nl-NL,nl;q=0.9',
])->postJson(route('page-visits.store'), [
'visitor_id' => 'visitor-123',
'url' => 'https://cv.robert.ooo/',
'path' => '/',
'title' => 'CV Roberto',
'referrer' => 'https://example.com',
'language' => 'nl-NL',
'timezone' => 'Europe/Amsterdam',
'screen' => [
'width' => 1920,
'height' => 1080,
],
'viewport' => [
'width' => 1440,
'height' => 900,
],
'device_pixel_ratio' => 1,
'color_depth' => 24,
'platform' => 'Linux x86_64',
'vendor' => 'Google Inc.',
'hardware_concurrency' => 8,
'device_memory' => 8,
'cookies_enabled' => true,
'online' => true,
])
->assertOk()
->assertJson([
'status' => 'queued',
]);
Queue::assertPushed(NotifyTelegramAboutPageVisit::class);
});
test('a page visit requires a visitor id and valid url', function () {
Queue::fake();
$this->postJson(route('page-visits.store'), [
'visitor_id' => '',
'url' => 'not-a-url',
])
->assertUnprocessable()
->assertJsonValidationErrors(['visitor_id', 'url']);
Queue::assertNotPushed(NotifyTelegramAboutPageVisit::class);
});