ldc-cocotte/tests/Feature/App/Http/Controllers/Bo/SlidesControllerTest.php
Jérémy Munsch 08c9028f91
test: update tests to use Assert for type checking
- Refactored tests to utilize Webmozart Assert for type assertions.
- Improved clarity and consistency in test expectations across multiple test files.
- Ensured that all relevant tests now check for expected types and values.
2026-11-16 16:26:51 +01:00

229 lines
7.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\App\Http\Controllers\Bo;
use App\Dto\SlideDto;
use App\Http\Requests\Bo\Slides\StoreSlideRequest;
use App\Models\Slide;
use App\Models\User;
use Domain\Enums\UserRole;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Gate;
use Webmozart\Assert\Assert;
use function Pest\Laravel\from;
use function Pest\Laravel\get;
use function Pest\Laravel\post;
use function Pest\Laravel\postJson;
describe('SlidesController', function (): void {
beforeEach(function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$this->user = User::factory()->create(['role' => UserRole::SuperAdmin]);
$this->actingAs($this->user, 'backend');
});
it('can access index', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
Slide::factory()->create();
// WHEN
$response = $this->get('/bo/slides');
// THEN
expect($response->status())->toBe(200);
$response->assertViewIs('bo.slides.index');
$response->assertViewHas('slides', static function (mixed $slides): bool {
Assert::isInstanceOf($slides, LengthAwarePaginator::class);
return $slides->items()[0] instanceof SlideDto;
});
$response->assertSee(__('Visible'));
});
it('can access create', function (): void {
/** @var \Tests\TestCase $this */
// WHEN
$response = $this->get('/bo/slides/create');
// THEN
expect($response->status())->toBe(200);
$response->assertViewIs('bo.slides.create');
$extensions = implode(', ', array_map(
static fn (string $ext): string => '.' . ltrim($ext, '.'),
StoreSlideRequest::ALLOWED_EXTENSIONS,
));
$expectedHelper = __('Allowed extensions: :extensions (:maxSize MB max). Required dimensions: :width px by :height px', [
'extensions' => $extensions,
'maxSize' => StoreSlideRequest::MAX_FILE_SIZE_KB / 1024,
'width' => StoreSlideRequest::PICTURE_WIDTH,
'height' => StoreSlideRequest::PICTURE_HEIGHT,
]);
$response->assertSee(json_encode($expectedHelper, JSON_THROW_ON_ERROR), false);
});
it('can store a slide', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$data = [
'title' => 'New Slide Title',
'text' => 'This is the new slide text',
'link' => 'https://example.com',
'link_label' => 'Click here',
'link_new_tab' => false,
'connected' => false,
'is_mobile' => false,
'is_published' => true,
'client_timezone' => 'Europe/Paris',
];
// WHEN
$response = $this->post('/bo/slides', $data);
// THEN
$slide = Slide::query()->where('title', 'New Slide Title')->first();
Assert::notNull($slide);
expect($slide)->not->toBeNull();
$response->assertRedirect("/bo/slides/{$slide->id}/edit");
});
it('can access edit', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$slide = Slide::factory()->create();
// WHEN
$response = $this->get("/bo/slides/{$slide->id}/edit");
// THEN
expect($response->status())->toBe(200);
$response->assertViewIs('bo.slides.edit');
$response->assertViewHas('slide', fn ($slideData) => $slideData instanceof SlideDto && $slideData->id === $slide->id);
});
it('can update a slide', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$slide = Slide::factory()->create();
$data = [
'title' => 'Updated Slide Title',
'text' => 'This is the new slide text updated',
'link' => 'https://example.com/updated',
'link_label' => 'Click here updated',
'link_new_tab' => true,
'connected' => true,
'is_mobile' => true,
'is_published' => false,
'client_timezone' => 'Europe/Paris',
];
// WHEN
$response = $this->put("/bo/slides/{$slide->id}", $data);
// THEN
$slide->refresh();
expect($slide->title)->toBe('Updated Slide Title');
$response->assertRedirect("/bo/slides/{$slide->id}/edit");
});
it('can delete a slide', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$slide = Slide::factory()->create();
// WHEN
$response = $this->delete("/bo/slides/{$slide->id}");
// THEN
expect(Slide::query()->find($slide->id))->toBeNull();
$response->assertRedirect('/bo/slides');
});
it('can change order', function (): void {
/** @var \Tests\TestCase $this */
// GIVEN
$slide1 = Slide::factory()->create(['order' => 1]);
$slide2 = Slide::factory()->create(['order' => 2]);
// WHEN
$response = $this->from('/bo/slides')->patch("/bo/slides/{$slide2->id}/up");
// THEN
$slide1->refresh();
$slide2->refresh();
expect($response->status())->toBe(302)
->and($slide1->order)->toBe(2)
->and($slide2->order)->toBe(1);
$response->assertRedirect('/bo/slides');
});
it('redirects unauthenticated guest to login', function (): void {
// GIVEN
auth('backend')->logout();
// WHEN
$response = get('/bo/slides');
// THEN
$response->assertRedirect('/bo/login');
});
it('forbids create and store when policy denies access', function (): void {
// GIVEN
$deniedPolicy = new class ()
{
public function create(): bool
{
return false;
}
};
Gate::policy(Slide::class, $deniedPolicy::class);
// WHEN
$createResponse = get('/bo/slides/create');
$storeResponse = post('/bo/slides', [
'title' => 'Slide interdit',
'text' => 'Contenu valide du slide',
'link_new_tab' => false,
'connected' => false,
'client_timezone' => 'Europe/Paris',
]);
// THEN
$createResponse->assertForbidden();
$storeResponse->assertForbidden();
});
it('redirects back with session validation errors on invalid web store payload', function (): void {
// WHEN
$response = from('/bo/slides/create')->post('/bo/slides', [
'title' => 'AB',
'text' => '',
'client_timezone' => 'Europe/Paris',
]);
// THEN
$response->assertRedirect('/bo/slides/create')
->assertSessionHasErrors(['title', 'text']);
});
it('returns 422 JSON validation errors on invalid JSON store payload', function (): void {
// WHEN
$response = postJson('/bo/slides', [
'title' => 'AB',
'text' => '',
'client_timezone' => 'Europe/Paris',
]);
// THEN
$response->assertUnprocessable()
->assertJsonValidationErrors(['title', 'text']);
});
});