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