2026-06-14 21:32:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-11-10 14:28:00 +01:00
|
|
|
use App\Cqs\Actions\Crm\UpdateCustomerInput;
|
2026-06-14 21:32:07 +02:00
|
|
|
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();
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-02 12:14:48 +02:00
|
|
|
it('converts to UpdateCustomerInput', function () {
|
2026-06-14 21:32:07 +02:00
|
|
|
// 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
|
2026-10-02 12:14:48 +02:00
|
|
|
$input = $request->toUpdateCustomerInput();
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// THEN
|
2026-10-02 12:14:48 +02:00
|
|
|
expect($input)->toBeInstanceOf(UpdateCustomerInput::class)
|
|
|
|
|
->and($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
|
|
|
});
|
|
|
|
|
|
2026-10-02 12:14:48 +02:00
|
|
|
it('converts to UpdateCustomerInput with empty address and phone as null', function () {
|
2026-06-14 21:32:07 +02:00
|
|
|
// 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',
|
2026-10-02 12:14:48 +02:00
|
|
|
'postcode' => '75000',
|
2026-06-14 21:32:07 +02:00
|
|
|
'address' => '',
|
|
|
|
|
'opt_in' => false,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// WHEN
|
2026-10-02 12:14:48 +02:00
|
|
|
$input = $request->toUpdateCustomerInput();
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// THEN
|
2026-10-02 12:14:48 +02:00
|
|
|
expect($input)->toBeInstanceOf(UpdateCustomerInput::class)
|
|
|
|
|
->and($input->phone)->toBeNull()
|
|
|
|
|
->and($input->address)->toBeNull();
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
});
|