shouldReceive('guest')->twice()->andReturn(true, false); $request = new RegisterRequest($authService); $request->initialize(); // WHEN $authorized = $request->authorize(); $denied = $request->authorize(); // THEN expect($authorized)->toBeTrue() ->and($denied)->toBeFalse(); }); it('builds RegisterCustomerInput via toRegisterCustomerInput()', function (): void { // GIVEN $authService = \Mockery::mock(AppCustomerAuthService::class); $request = new RegisterRequest($authService); $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'], ); // WHEN $input = $request->toRegisterCustomerInput(); // THEN expect($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 (): void { // GIVEN $authService = \Mockery::mock(AppCustomerAuthService::class); $request = new RegisterRequest($authService); $request->initialize( request: ['birth_date' => 'invalid-date'], server: ['REQUEST_METHOD' => 'POST'], ); // WHEN & THEN $request->toRegisterCustomerInput(); })->throws(\InvalidArgumentException::class, 'Invalid birth date format'); 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(UuidHelper::v4()); $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: [ 'matricule' => '1234567', 'code' => 'ABCDEF', ], server: ['REQUEST_METHOD' => 'POST'], ); // 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: [ 'matricule' => '7654321', 'code' => 'FGHJKM', ], server: ['REQUEST_METHOD' => 'POST'], ); // 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 = Carbon::parse('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 = Carbon::parse('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(); } }); });