ldc-cocotte/tests/Feature/App/Http/Requests/Bo/Dashboard/DashboardRequestTest.php

79 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
namespace Tests\Feature\App\Http\Requests\Bo\Dashboard;
2026-06-14 21:32:07 +02:00
use App\Http\Requests\Bo\Dashboard\DashboardRequest;
use Illuminate\Support\Facades\Validator;
2026-06-14 21:32:07 +02:00
use Infrastructure\Adapters\Services\AppAdminAuthService;
describe('DashboardRequest', 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 DashboardRequest($authService);
$request->initialize();
// 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 paired start and end date parameters', function (): void {
2026-06-14 21:32:07 +02:00
// GIVEN
$authService = \Mockery::mock(AppAdminAuthService::class);
$rules = new DashboardRequest($authService)->rules();
2026-06-14 21:32:07 +02:00
// WHEN
$missingStart = Validator::make(['end' => '2026-01-31'], $rules);
$invalidDate = Validator::make(['start' => 'not-a-date', 'end' => '2026-01-31'], $rules);
$valid = Validator::make(['start' => '2026-01-01', 'end' => '2026-01-31'], $rules);
2026-06-14 21:32:07 +02:00
// THEN
expect($missingStart->errors()->has('start'))->toBeTrue()
->and($invalidDate->errors()->has('start'))->toBeTrue()
->and($valid->passes())->toBeTrue();
2026-06-14 21:32:07 +02:00
});
it('gets start and end dates when present', function (): void {
2026-06-14 21:32:07 +02:00
// GIVEN
$authService = \Mockery::mock(AppAdminAuthService::class);
2026-06-14 21:32:07 +02:00
$request = new DashboardRequest($authService);
$request->initialize();
expect($request->getStart())->toBeNull()
->and($request->getEnd())->toBeNull();
2026-06-14 21:32:07 +02:00
// WHEN
$request->merge(['start' => '2023-01-01', 'end' => '2023-12-31']);
2026-06-14 21:32:07 +02:00
// THEN
expect($request->getStart()?->format('Y-m-d'))->toBe('2023-01-01')
->and($request->getEnd()?->format('Y-m-d'))->toBe('2023-12-31');
2026-06-14 21:32:07 +02:00
});
it('maps to GetBoDashboardQueryInput', function (): void {
// GIVEN
$authService = \Mockery::mock(AppAdminAuthService::class);
$request = new DashboardRequest($authService);
$request->initialize(query: [
'start' => '2026-01-01',
'end' => '2026-01-31',
]);
// WHEN
$input = $request->toGetBoDashboardQueryInput();
// THEN
expect($input->start?->format('Y-m-d'))->toBe('2026-01-01')
->and($input->end?->format('Y-m-d'))->toBe('2026-01-31');
});
2026-06-14 21:32:07 +02:00
});