ldc-cocotte/tests/Feature/App/Http/Requests/Fo/Customer/UpdateInfosRequestTest.php

133 lines
4.6 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\Fo\Customer;
2026-06-14 21:32:07 +02:00
use App\Http\Requests\Fo\Customer\UpdateInfosRequest;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Validator;
2026-06-14 21:32:07 +02:00
use Infrastructure\Adapters\Services\AppCustomerAuthService;
describe('UpdateInfosRequest', function (): void {
it('authorizes only when customer auth service returns true', function (): void {
2026-06-14 21:32:07 +02:00
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$authService->shouldReceive('check')->twice()->andReturn(true, false);
2026-06-14 21:32:07 +02:00
$request = new UpdateInfosRequest($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 adult birth_date, French postal code and phone with custom attributes and messages', function (): void {
2026-06-14 21:32:07 +02:00
// GIVEN
Date::setTestNow(Date::parse('2026-06-15 10:00:00', 'Europe/Paris'));
try {
$authService = \Mockery::mock(AppCustomerAuthService::class);
$request = new UpdateInfosRequest($authService);
// WHEN
$invalid = Validator::make(
[
'firstname' => '',
'lastname' => 'Doe',
'birth_date' => '2012-01-01',
'phone' => 'invalid',
'city' => 'Paris',
'postcode' => 'ABCDE',
'opt_in' => true,
],
$request->rules(),
$request->messages(),
$request->attributes(),
);
$valid = Validator::make(
[
'firstname' => 'John',
'lastname' => 'Doe',
'birth_date' => '1990-01-01',
'phone' => '01 02 03 04 05',
'city' => 'Paris',
'postcode' => '75001',
'opt_in' => true,
],
$request->rules(),
$request->messages(),
$request->attributes(),
);
// THEN
expect($invalid->errors()->keys())->toContain('firstname', 'birth_date', 'phone', 'postcode')
->and($invalid->errors()->first('birth_date'))->toBe(trans('validation.custom.birth_date.before_or_equal'))
->and($valid->passes())->toBeTrue();
} finally {
Date::setTestNow();
}
2026-06-14 21:32:07 +02:00
});
it('converts request data into UpdateCustomerInput with nullable phone and address', function (): void {
2026-06-14 21:32:07 +02:00
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$request = new UpdateInfosRequest($authService);
$request->initialize(
request: [
'firstname' => 'John',
'lastname' => 'Doe',
'birth_date' => '1990-01-01',
'phone' => '0102030405',
'city' => 'Paris',
'postcode' => '75000',
'address' => '123 Main St',
'opt_in' => '1',
],
server: ['REQUEST_METHOD' => 'POST'],
);
2026-06-14 21:32:07 +02:00
// WHEN
$input = $request->toUpdateCustomerInput();
2026-06-14 21:32:07 +02:00
// THEN
expect($input->firstname)->toBe('John')
->and($input->lastname)->toBe('Doe')
->and($input->birthDate)->toBe('1990-01-01')
->and($input->phone)->toBe('0102030405')
->and($input->city)->toBe('Paris')
->and($input->postcode)->toBe('75000')
->and($input->address)->toBe('123 Main St')
->and($input->optIn)->toBeTrue();
2026-06-14 21:32:07 +02:00
// GIVEN
$emptyRequest = new UpdateInfosRequest($authService);
$emptyRequest->initialize(
request: [
'firstname' => 'John',
'lastname' => 'Doe',
'birth_date' => '1990-01-01',
'phone' => '',
'city' => 'Paris',
'postcode' => '75000',
'address' => '',
'opt_in' => false,
],
server: ['REQUEST_METHOD' => 'POST'],
);
2026-06-14 21:32:07 +02:00
// WHEN
$emptyInput = $emptyRequest->toUpdateCustomerInput();
2026-06-14 21:32:07 +02:00
// THEN
expect($emptyInput->phone)->toBeNull()
->and($emptyInput->address)->toBeNull()
->and($emptyInput->optIn)->toBeFalse();
2026-06-14 21:32:07 +02:00
});
});