ldc-cocotte/tests/Feature/App/Http/Requests/Bo/Pages/UpdatePageRequestTest.php

79 lines
2.7 KiB
PHP
Raw Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace Tests\Feature\App\Http\Requests\Bo\Pages;
2026-06-14 21:32:07 +02:00
use App\Http\Requests\Bo\Pages\UpdatePageRequest;
use App\Models\Page;
use Illuminate\Support\Facades\Validator;
2026-06-14 21:32:07 +02:00
use Infrastructure\Adapters\Services\AppAdminAuthService;
describe('UpdatePageRequest', function (): void {
it('authorizes only when admin auth service returns true', function (): void {
2026-06-14 21:32:07 +02:00
// GIVEN
$authService = \Mockery::mock(AppAdminAuthService::class);
$authService->shouldReceive('check')->twice()->andReturn(true, false);
2026-06-14 21:32:07 +02:00
$request = new UpdatePageRequest($authService);
$request->initialize();
2026-06-14 21:32:07 +02:00
// WHEN
$authorized = $request->authorize();
$denied = $request->authorize();
2026-06-14 21:32:07 +02:00
// THEN
expect($authorized)->toBeTrue()
->and($denied)->toBeFalse();
2026-06-14 21:32:07 +02:00
});
it('validates SEO length bounds and required content', function (): void {
2026-06-14 21:32:07 +02:00
// GIVEN
$authService = \Mockery::mock(AppAdminAuthService::class);
$rules = new UpdatePageRequest($authService)->rules();
2026-06-14 21:32:07 +02:00
// WHEN
$invalid = Validator::make([
'seo_title' => 'Trop court',
'seo_description' => 'Trop courte aussi',
'content' => '',
], $rules);
$valid = Validator::make([
'seo_title' => str_repeat('a', 50),
'seo_description' => str_repeat('b', 80),
'title' => 'Conditions générales',
'content' => '<p>Contenu valide.</p>',
], $rules);
2026-06-14 21:32:07 +02:00
// THEN
expect($invalid->errors()->keys())->toContain('seo_title', 'seo_description', 'content')
->and($valid->passes())->toBeTrue();
2026-06-14 21:32:07 +02:00
});
it('maps request data into UpdatePageInput', function (): void {
2026-06-14 21:32:07 +02:00
// GIVEN
$authService = \Mockery::mock(AppAdminAuthService::class);
$page = new Page();
2026-06-14 21:32:07 +02:00
$request = new UpdatePageRequest($authService);
$request->initialize(
request: [
'seo_title' => str_repeat('T', 50),
'seo_description' => str_repeat('D', 80),
'title' => 'Mentions légales',
'content' => '<p>Texte légal</p>',
],
server: ['REQUEST_METHOD' => 'PUT'],
);
2026-06-14 21:32:07 +02:00
// WHEN
$input = $request->toUpdatePageInput($page);
2026-06-14 21:32:07 +02:00
// THEN
expect($input->page)->toBe($page)
->and($input->seo->title)->toBe(str_repeat('T', 50))
->and($input->seo->description)->toBe(str_repeat('D', 80))
->and($input->title)->toBe('Mentions légales')
->and($input->content)->toBe('<p>Texte légal</p>');
2026-06-14 21:32:07 +02:00
});
});