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\Fo\Auth;
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
use App\Http\Requests\Fo\Auth\RegisterRequest;
|
2026-11-14 22:26:03 +01:00
|
|
|
use App\Models\Customer;
|
|
|
|
|
use App\Models\Employee;
|
|
|
|
|
use Domain\Models\ValueObjects\Code;
|
|
|
|
|
use Domain\Models\ValueObjects\Email;
|
|
|
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperServiceCode;
|
|
|
|
|
use Domain\Models\ValueObjects\ShopperSiteCode;
|
2026-11-16 15:31:41 +01:00
|
|
|
use Illuminate\Support\Carbon;
|
2026-11-16 02:35:42 +01:00
|
|
|
use Illuminate\Support\Facades\Date;
|
2026-11-14 22:26:03 +01:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Infrastructure\Adapters\Services\AppCustomerAuthService;
|
2026-11-17 12:15:39 +01:00
|
|
|
use Shared\Helpers\UuidHelper;
|
2026-11-14 22:26:03 +01:00
|
|
|
use Shared\ValueObjects\CrmHashcode;
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-11-16 15:31:41 +01:00
|
|
|
describe('RegisterRequest', function (): void {
|
|
|
|
|
it('authorizes only when customer is a guest', function (): void {
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
2026-11-16 15:31:41 +01:00
|
|
|
$authService->shouldReceive('guest')->twice()->andReturn(true, false);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
$request = new RegisterRequest($authService);
|
|
|
|
|
$request->initialize();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
2026-11-16 15:31:41 +01:00
|
|
|
$authorized = $request->authorize();
|
|
|
|
|
$denied = $request->authorize();
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:31:41 +01:00
|
|
|
expect($authorized)->toBeTrue()
|
|
|
|
|
->and($denied)->toBeFalse();
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
|
2026-11-16 15:31:41 +01:00
|
|
|
it('builds RegisterCustomerInput via toRegisterCustomerInput()', function (): void {
|
2026-06-14 21:32:07 +02:00
|
|
|
// GIVEN
|
|
|
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
|
|
|
$request = new RegisterRequest($authService);
|
2026-11-16 15:31:41 +01:00
|
|
|
$request->initialize(
|
|
|
|
|
request: [
|
|
|
|
|
'matricule' => '1234567',
|
|
|
|
|
'code' => 'ABCDEF',
|
|
|
|
|
'lastname' => 'Doe',
|
|
|
|
|
'firstname' => 'John',
|
|
|
|
|
'postal_code' => '75000',
|
|
|
|
|
'city' => 'Paris',
|
|
|
|
|
'email' => 'john.doe@example.com',
|
|
|
|
|
'password' => 'secret',
|
|
|
|
|
'optin' => '1',
|
|
|
|
|
'birth_date' => '1990-01-01',
|
|
|
|
|
],
|
|
|
|
|
server: ['REQUEST_METHOD' => 'POST'],
|
|
|
|
|
);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// WHEN
|
2026-10-03 16:50:38 +02:00
|
|
|
$input = $request->toRegisterCustomerInput();
|
2026-06-14 21:32:07 +02:00
|
|
|
|
|
|
|
|
// THEN
|
2026-11-16 15:31:41 +01:00
|
|
|
expect($input->email->value)->toBe('john.doe@example.com')
|
2026-10-03 16:50:38 +02:00
|
|
|
->and($input->firstname)->toBe('John')
|
|
|
|
|
->and($input->birthDate->format('Y-m-d'))->toBe('1990-01-01')
|
|
|
|
|
->and($input->optIn)->toBeTrue();
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|
|
|
|
|
|
2026-11-16 15:31:41 +01:00
|
|
|
it('throws InvalidArgumentException for invalid birth date in toRegisterCustomerInput()', function (): void {
|
2026-10-03 16:50:38 +02:00
|
|
|
// GIVEN
|
2026-06-14 21:32:07 +02:00
|
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
|
|
|
$request = new RegisterRequest($authService);
|
2026-11-16 15:31:41 +01:00
|
|
|
$request->initialize(
|
|
|
|
|
request: ['birth_date' => 'invalid-date'],
|
|
|
|
|
server: ['REQUEST_METHOD' => 'POST'],
|
|
|
|
|
);
|
2026-06-14 21:32:07 +02:00
|
|
|
|
2026-10-03 16:50:38 +02:00
|
|
|
// WHEN & THEN
|
|
|
|
|
$request->toRegisterCustomerInput();
|
2026-06-14 21:32:07 +02:00
|
|
|
})->throws(\InvalidArgumentException::class, 'Invalid birth date format');
|
|
|
|
|
|
2026-11-14 22:26:03 +01:00
|
|
|
it('rejects matricule and code when employee already has a customer_id', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$customer = new Customer(['email' => new Email('existing@example.com')]);
|
2026-11-17 12:15:39 +01:00
|
|
|
$customer->hashcode = new CrmHashcode(UuidHelper::v4());
|
2026-11-14 22:26:03 +01:00
|
|
|
$customer->saveOrFail();
|
|
|
|
|
|
|
|
|
|
$employee = new Employee();
|
|
|
|
|
$employee->matricule = new Matricule('1234567');
|
|
|
|
|
$employee->code = new Code('ABCDEF');
|
|
|
|
|
$employee->nom = 'Doe';
|
|
|
|
|
$employee->prenom = 'John';
|
|
|
|
|
$employee->etablissement = new ShopperEstablishmentCode('ETAB01');
|
|
|
|
|
$employee->site = new ShopperSiteCode('SITE');
|
|
|
|
|
$employee->service = new ShopperServiceCode('1');
|
|
|
|
|
$employee->customer()->associate($customer);
|
|
|
|
|
$employee->saveOrFail();
|
|
|
|
|
|
|
|
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
|
|
|
$request = new RegisterRequest($authService);
|
2026-11-16 15:31:41 +01:00
|
|
|
$request->initialize(
|
|
|
|
|
request: [
|
|
|
|
|
'matricule' => '1234567',
|
|
|
|
|
'code' => 'ABCDEF',
|
|
|
|
|
],
|
|
|
|
|
server: ['REQUEST_METHOD' => 'POST'],
|
|
|
|
|
);
|
2026-11-14 22:26:03 +01:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$validator = Validator::make(
|
|
|
|
|
['matricule' => '1234567', 'code' => 'ABCDEF'],
|
|
|
|
|
[
|
|
|
|
|
'matricule' => $request->rules()['matricule'],
|
|
|
|
|
'code' => $request->rules()['code'],
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($validator->fails())->toBeTrue()
|
|
|
|
|
->and($validator->errors()->has('matricule'))->toBeTrue()
|
|
|
|
|
->and($validator->errors()->has('code'))->toBeTrue();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('accepts matricule and code when employee has no customer_id', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$employee = new Employee();
|
|
|
|
|
$employee->matricule = new Matricule('7654321');
|
|
|
|
|
$employee->code = new Code('FGHJKM');
|
|
|
|
|
$employee->nom = 'Doe';
|
|
|
|
|
$employee->prenom = 'Jane';
|
|
|
|
|
$employee->etablissement = new ShopperEstablishmentCode('ETAB01');
|
|
|
|
|
$employee->site = new ShopperSiteCode('SITE');
|
|
|
|
|
$employee->service = new ShopperServiceCode('1');
|
|
|
|
|
$employee->saveOrFail();
|
|
|
|
|
|
|
|
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
|
|
|
$request = new RegisterRequest($authService);
|
2026-11-16 15:31:41 +01:00
|
|
|
$request->initialize(
|
|
|
|
|
request: [
|
|
|
|
|
'matricule' => '7654321',
|
|
|
|
|
'code' => 'FGHJKM',
|
|
|
|
|
],
|
|
|
|
|
server: ['REQUEST_METHOD' => 'POST'],
|
|
|
|
|
);
|
2026-11-14 22:26:03 +01:00
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$validator = Validator::make(
|
|
|
|
|
['matricule' => '7654321', 'code' => 'FGHJKM'],
|
|
|
|
|
[
|
|
|
|
|
'matricule' => $request->rules()['matricule'],
|
|
|
|
|
'code' => $request->rules()['code'],
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($validator->passes())->toBeTrue();
|
|
|
|
|
});
|
2026-11-16 02:35:42 +01:00
|
|
|
|
|
|
|
|
it('rejects invalid matricule and code format in validation rules', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
|
|
|
$request = new RegisterRequest($authService);
|
|
|
|
|
$request->initialize();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$validator = Validator::make(
|
|
|
|
|
['matricule' => 'not-digits', 'code' => '0OIl!'],
|
|
|
|
|
[
|
|
|
|
|
'matricule' => $request->rules()['matricule'],
|
|
|
|
|
'code' => $request->rules()['code'],
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($validator->fails())->toBeTrue()
|
|
|
|
|
->and($validator->errors()->has('matricule'))->toBeTrue()
|
|
|
|
|
->and($validator->errors()->has('code'))->toBeTrue();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('rejects matricule and code when employee is inactive due to end_date or future start_date', function (): void {
|
|
|
|
|
// GIVEN
|
|
|
|
|
Date::setTestNow(Date::parse('2026-06-15 10:00:00', 'Europe/Paris'));
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$endedEmployee = new Employee();
|
|
|
|
|
$endedEmployee->matricule = new Matricule('7654322');
|
|
|
|
|
$endedEmployee->code = new Code('FGHJK2');
|
|
|
|
|
$endedEmployee->nom = 'Doe';
|
|
|
|
|
$endedEmployee->prenom = 'Ended';
|
2026-11-16 15:31:41 +01:00
|
|
|
$endedEmployee->end_date = Carbon::parse('2026-06-15');
|
2026-11-16 02:35:42 +01:00
|
|
|
$endedEmployee->etablissement = new ShopperEstablishmentCode('ETAB01');
|
|
|
|
|
$endedEmployee->site = new ShopperSiteCode('SITE');
|
|
|
|
|
$endedEmployee->service = new ShopperServiceCode('1');
|
|
|
|
|
$endedEmployee->saveOrFail();
|
|
|
|
|
|
|
|
|
|
$futureEmployee = new Employee();
|
|
|
|
|
$futureEmployee->matricule = new Matricule('7654323');
|
|
|
|
|
$futureEmployee->code = new Code('FGHJK3');
|
|
|
|
|
$futureEmployee->nom = 'Doe';
|
|
|
|
|
$futureEmployee->prenom = 'Future';
|
2026-11-16 15:31:41 +01:00
|
|
|
$futureEmployee->start_date = Carbon::parse('2026-06-16');
|
2026-11-16 02:35:42 +01:00
|
|
|
$futureEmployee->etablissement = new ShopperEstablishmentCode('ETAB01');
|
|
|
|
|
$futureEmployee->site = new ShopperSiteCode('SITE');
|
|
|
|
|
$futureEmployee->service = new ShopperServiceCode('1');
|
|
|
|
|
$futureEmployee->saveOrFail();
|
|
|
|
|
|
|
|
|
|
$authService = \Mockery::mock(AppCustomerAuthService::class);
|
|
|
|
|
$request = new RegisterRequest($authService);
|
|
|
|
|
$request->initialize();
|
|
|
|
|
|
|
|
|
|
// WHEN
|
|
|
|
|
$endedValidator = Validator::make(
|
|
|
|
|
['matricule' => '7654322', 'code' => 'FGHJK2'],
|
|
|
|
|
['matricule' => $request->rules()['matricule'], 'code' => $request->rules()['code']],
|
|
|
|
|
);
|
|
|
|
|
$futureValidator = Validator::make(
|
|
|
|
|
['matricule' => '7654323', 'code' => 'FGHJK3'],
|
|
|
|
|
['matricule' => $request->rules()['matricule'], 'code' => $request->rules()['code']],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// THEN
|
|
|
|
|
expect($endedValidator->fails())->toBeTrue()
|
|
|
|
|
->and($endedValidator->errors()->has('matricule'))->toBeTrue()
|
|
|
|
|
->and($endedValidator->errors()->has('code'))->toBeTrue()
|
|
|
|
|
->and($futureValidator->fails())->toBeTrue()
|
|
|
|
|
->and($futureValidator->errors()->has('matricule'))->toBeTrue()
|
|
|
|
|
->and($futureValidator->errors()->has('code'))->toBeTrue();
|
|
|
|
|
} finally {
|
|
|
|
|
Date::setTestNow();
|
|
|
|
|
}
|
|
|
|
|
});
|
2026-06-14 21:32:07 +02:00
|
|
|
});
|