shouldReceive('guest')->once()->andReturn(true); $request = new RegisterRequest($authService); // WHEN $request->initialize(); // THEN expect($request->authorize())->toBeTrue(); }); it('does not authorize when not guest', function () { // GIVEN $authService = \Mockery::mock(AppCustomerAuthService::class); $authService->shouldReceive('guest')->once()->andReturn(false); $request = new RegisterRequest($authService); // WHEN $request->initialize(); // THEN expect($request->authorize())->toBeFalse(); }); it('validates rules', function () { // GIVEN $authService = \Mockery::mock(AppCustomerAuthService::class); $request = new RegisterRequest($authService); // WHEN $request->initialize(); // THEN expect($request->rules())->toBeArray() ->and($request->rules())->toHaveKeys([ 'matricule', 'code', 'lastname', 'firstname', 'phone', 'address', 'postal_code', 'city', 'email', 'password', 'ok_cgv', 'optin', 'birth_date', ]); }); it('builds RegisterCustomerInput via toRegisterCustomerInput()', function () { // GIVEN $authService = \Mockery::mock(AppCustomerAuthService::class); $request = new RegisterRequest($authService); $request->initialize(); $request->merge([ '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', ]); $reflection = new \ReflectionClass($request); $method = $reflection->getMethod('prepareForValidation'); $method->setAccessible(true); $method->invoke($request); // WHEN $input = $request->toRegisterCustomerInput(); // THEN expect($input)->toBeInstanceOf(RegisterCustomerInput::class) ->and($input->email->value)->toBe('john.doe@example.com') ->and($input->firstname)->toBe('John') ->and($input->birthDate->format('Y-m-d'))->toBe('1990-01-01') ->and($input->optIn)->toBeTrue(); }); it('throws InvalidArgumentException for invalid birth date in toRegisterCustomerInput()', function () { // GIVEN $authService = \Mockery::mock(AppCustomerAuthService::class); $request = new RegisterRequest($authService); $request->initialize(); $request->merge([ 'birth_date' => 'invalid-date', ]); // WHEN & THEN $request->toRegisterCustomerInput(); })->throws(\InvalidArgumentException::class, 'Invalid birth date format'); it('returns custom attributes', function () { $authService = \Mockery::mock(AppCustomerAuthService::class); // GIVEN $request = new RegisterRequest($authService); // WHEN $request->initialize(); // THEN expect($request->attributes())->toBeArray() ->and($request->attributes())->toHaveKeys([ 'firstname', 'lastname', 'birth_date', 'address', 'phone', 'city', 'postcode', 'password', 'matricule', 'code', 'ok_cgv', 'optin', ]); }); it('rejects matricule and code when employee already has a customer_id', function (): void { // GIVEN $customer = new Customer(['email' => new Email('existing@example.com')]); $customer->hashcode = new CrmHashcode(Uuid::uuid4()); $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); $request->initialize(); $request->merge([ 'matricule' => '1234567', 'code' => 'ABCDEF', ]); // 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); $request->initialize(); $request->merge([ 'matricule' => '7654321', 'code' => 'FGHJKM', ]); // WHEN $validator = Validator::make( ['matricule' => '7654321', 'code' => 'FGHJKM'], [ 'matricule' => $request->rules()['matricule'], 'code' => $request->rules()['code'], ], ); // THEN expect($validator->passes())->toBeTrue(); }); 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'; $endedEmployee->end_date = '2026-06-15'; $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'; $futureEmployee->start_date = '2026-06-16'; $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(); } }); });