132 lines
4.6 KiB
PHP
132 lines
4.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\App\Http\Requests\Fo\Customer;
|
|
|
|
use App\Http\Requests\Fo\Customer\UpdateInfosRequest;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
|
|
|
describe('UpdateInfosRequest', function (): void {
|
|
it('authorizes only when customer auth service returns true', function (): void {
|
|
// GIVEN
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
$authService->shouldReceive('check')->twice()->andReturn(true, false);
|
|
|
|
$request = new UpdateInfosRequest($authService);
|
|
$request->initialize();
|
|
|
|
// WHEN
|
|
$authorized = $request->authorize();
|
|
$denied = $request->authorize();
|
|
|
|
// THEN
|
|
expect($authorized)->toBeTrue()
|
|
->and($denied)->toBeFalse();
|
|
});
|
|
|
|
it('validates adult birth_date, French postal code and phone with custom attributes and messages', function (): void {
|
|
// 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();
|
|
}
|
|
});
|
|
|
|
it('converts request data into UpdateCustomerInput with nullable phone and address', function (): void {
|
|
// 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'],
|
|
);
|
|
|
|
// WHEN
|
|
$input = $request->toUpdateCustomerInput();
|
|
|
|
// 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();
|
|
|
|
// 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'],
|
|
);
|
|
|
|
// WHEN
|
|
$emptyInput = $emptyRequest->toUpdateCustomerInput();
|
|
|
|
// THEN
|
|
expect($emptyInput->phone)->toBeNull()
|
|
->and($emptyInput->address)->toBeNull()
|
|
->and($emptyInput->optIn)->toBeFalse();
|
|
});
|
|
});
|