ldc-cocotte/tests/Feature/App/Http/Controllers/Bo/FaqsControllerTest.php

127 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
use App\Models\Faq;
use App\Models\User;
use Domain\Enums\UserRole;
describe('FaqsController', function (): void {
beforeEach(function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$user = User::factory()->create(['role' => UserRole::SuperAdmin]);
$this->actingAs($user, 'backend');
2026-06-14 21:32:07 +02:00
});
it('can access index', function (): void {
/** @var \Tests\TestCase $this */
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->get('/bo/faqs');
// THEN
2026-06-14 21:32:07 +02:00
expect($response->status())->toBe(200);
$response->assertViewIs('bo.faqs.index');
});
it('can access create', function (): void {
/** @var \Tests\TestCase $this */
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->get('/bo/faqs/create');
// THEN
2026-06-14 21:32:07 +02:00
expect($response->status())->toBe(200);
$response->assertViewIs('bo.faqs.create');
});
it('can store a faq', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
2026-06-14 21:32:07 +02:00
$data = [
'question' => 'How does it work?',
'answer' => '<p>Like <strong>this</strong>.</p>',
2026-06-14 21:32:07 +02:00
];
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->post('/bo/faqs', $data);
// THEN
$faq = Faq::query()->firstOrFail();
expect($faq->question)->toBe('How does it work?');
expect($faq->answer)->toBe('<p>Like <strong>this</strong>.</p>');
2026-06-14 21:32:07 +02:00
$response->assertRedirect("/bo/faqs/{$faq->id}/edit");
});
it('can access edit', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
2026-06-14 21:32:07 +02:00
$faq = new Faq();
$faq->question = 'What is this?';
$faq->answer = 'This is an answer.';
2026-06-14 21:32:07 +02:00
$faq->save();
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->get("/bo/faqs/{$faq->id}/edit");
// THEN
2026-06-14 21:32:07 +02:00
expect($response->status())->toBe(200);
$response->assertViewIs('bo.faqs.edit');
});
it('can update a faq', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
2026-06-14 21:32:07 +02:00
$faq = new Faq();
$faq->question = 'Initial question?';
$faq->answer = 'Initial answer.';
2026-06-14 21:32:07 +02:00
$faq->save();
$data = [
'question' => 'Updated question?',
'answer' => '<p>Updated <em>answer</em>.</p>',
2026-06-14 21:32:07 +02:00
];
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->put("/bo/faqs/{$faq->id}", $data);
// THEN
2026-06-14 21:32:07 +02:00
$faq->refresh();
expect($faq->question)->toBe('Updated question?');
expect($faq->answer)->toBe('<p>Updated <em>answer</em>.</p>');
2026-06-14 21:32:07 +02:00
$response->assertRedirect("/bo/faqs/{$faq->id}/edit");
});
it('can delete a faq', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
2026-06-14 21:32:07 +02:00
$faq = new Faq();
$faq->question = 'Question to delete?';
$faq->answer = 'Answer to delete.';
2026-06-14 21:32:07 +02:00
$faq->save();
// WHEN
2026-06-14 21:32:07 +02:00
$response = $this->delete("/bo/faqs/{$faq->id}");
// THEN
expect(Faq::query()->find($faq->id))->toBeNull();
2026-06-14 21:32:07 +02:00
$response->assertRedirect('/bo/faqs');
});
it('can change order', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$faq = new Faq();
$faq->question = 'Question to reorder?';
$faq->answer = 'Answer to reorder.';
$faq->save();
// WHEN
$response = $this->patch("/bo/faqs/{$faq->id}/up");
// THEN
expect($response->status())->toBe(302);
});
2026-06-14 21:32:07 +02:00
});