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

153 lines
5 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;
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', fn ($slides) => $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), 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();
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');
});
});