ldc-cocotte/tests/Feature/App/Http/Requests/Fo/Customer/UpdateInfosRequestTest.php
2026-10-27 09:58:12 +01:00

172 lines
5 KiB
PHP

<?php
declare(strict_types=1);
use App\Http\Requests\Fo\Customer\UpdateInfosRequest;
use Infrastructure\Adapters\Services\AppCustomerAuthService;
describe('UpdateInfosRequest', function () {
it('authorizes when user is logged in', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$authService->shouldReceive('check')->once()->andReturn(true);
$request = new UpdateInfosRequest($authService);
// WHEN
$request->initialize();
// THEN
expect($request->authorize())->toBeTrue();
});
it('does not authorize when user is not logged in', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$authService->shouldReceive('check')->once()->andReturn(false);
$request = new UpdateInfosRequest($authService);
// WHEN
$request->initialize();
// THEN
expect($request->authorize())->toBeFalse();
});
it('merges opt_in in prepareForValidation', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$request = new UpdateInfosRequest($authService);
$request->initialize();
$request->merge(['opt_in' => '1']);
$reflection = new \ReflectionClass($request);
$method = $reflection->getMethod('prepareForValidation');
$method->setAccessible(true);
// WHEN
$method->invoke($request);
// THEN
expect($request->input('opt_in'))->toBeTrue();
});
it('converts to crm data', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$request = new UpdateInfosRequest($authService);
$request->initialize();
$request->merge([
'firstname' => 'John',
'lastname' => 'Doe',
'birth_date' => '1990-01-01',
'phone' => '0102030405',
'city' => 'Paris',
'postcode' => '75000',
'address' => '123 Main St',
'opt_in' => true,
]);
// WHEN
$data = $request->toCrmData();
// THEN
expect($data)->toBeArray()
->and($data)->toMatchArray([
'firstname' => 'John',
'lastname' => 'Doe',
'birth_date' => '1990-01-01',
'phone' => '0102030405',
'city' => 'Paris',
'postcode' => '75000',
'address' => '123 Main St',
'opt_in' => true,
]);
});
it('converts to crm data with empty postcode and address as null', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$request = new UpdateInfosRequest($authService);
$request->initialize();
$request->merge([
'firstname' => 'John',
'lastname' => 'Doe',
'birth_date' => '1990-01-01',
'phone' => '',
'city' => 'Paris',
'postcode' => '',
'address' => '',
'opt_in' => false,
]);
// WHEN
$data = $request->toCrmData();
// THEN
expect($data)->toBeArray()
->and($data['postcode'])->toBe('')
->and($data['address'])->toBe('');
});
it('validates rules', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$request = new UpdateInfosRequest($authService);
// WHEN
$request->initialize();
// THEN
expect($request->rules())->toBeArray()
->and($request->rules())->toHaveKeys([
'firstname',
'lastname',
'birth_date',
'address',
'phone',
'city',
'postcode',
'opt_in',
]);
});
it('returns custom messages', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$request = new UpdateInfosRequest($authService);
// WHEN
$request->initialize();
// THEN
expect($request->messages())->toBeArray()
->and($request->messages())->toHaveKeys([
'birth_date.before_or_equal',
'birth_date.date',
'birth_date.date_format',
]);
});
it('returns custom attributes', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$request = new UpdateInfosRequest($authService);
// WHEN
$request->initialize();
// THEN
expect($request->attributes())->toBeArray()
->and($request->attributes())->toHaveKeys([
'firstname',
'lastname',
'birth_date',
'address',
'phone',
'city',
'postcode',
]);
});
});