matricule = $matricule; $employee->code = $code; $employee->nom = 'Doe'; $employee->prenom = 'John'; $employee->etablissement = new ShopperEstablishmentCode('ETAB01'); $employee->site = new ShopperSiteCode('SITE'); $employee->service = new ShopperServiceCode('1'); $employee->saveOrFail(); $crmReturnedHashcode = Uuid::uuid4()->toString(); $crmService = \Mockery::mock(CrmService::class); $crmService->shouldReceive('persist') ->once() ->withArgs(function (CrmCustomer $crmCust) use ($email): bool { return $crmCust->email === $email->value && $crmCust->firstname === 'John' && $crmCust->lastname === 'Doe' && $crmCust->city === 'Paris' && $crmCust->country === Country::france; }) ->andReturn(new CrmCustomer( hashcode: $crmReturnedHashcode, email: $email->value, password: null, firstname: 'John', lastname: 'Doe', birthDate: '1990-01-01', phone: '+33612345678', address: '10 rue de Paris', address2: null, city: 'Paris', postcode: '75001', country: Country::france, optIn: true, status: false, lastConnection: null, )); App::instance(CrmService::class, $crmService); $authService = \Mockery::mock(AppCustomerAuthService::class); $authService->shouldReceive('logIn') ->once() ->withArgs(fn (Customer $c) => $c->email->value === $email->value); App::instance(AppCustomerAuthService::class, $authService); $employeeRepo = resolve(EmployeeRepository::class); $action = new RegisterCustomer( authService: $authService, crmService: $crmService, employeeRepository: $employeeRepo, ); $input = new RegisterCustomerInput( matricule: $matricule, code: $code, firstname: 'John', lastname: 'Doe', email: $email, password: 'secretPassword123!', birthDate: $birthDate, phone: $phone, address: '10 rue de Paris', postcode: '75001', city: 'Paris', optIn: true, ); // WHEN $customer = $action($input); // THEN expect($customer)->toBeInstanceOf(Customer::class) ->and($customer->email->value)->toBe('john.doe@example.com') ->and($customer->hashcode->value)->toBe($crmReturnedHashcode); $freshEmployee = Employee::query()->find($employee->id); expect($freshEmployee?->customer_id)->toBe($customer->id); }); it('throws ModelNotFoundException when employee does not exist', function (): void { // GIVEN $crmService = \Mockery::mock(CrmService::class); $authService = \Mockery::mock(AppCustomerAuthService::class); App::instance(CrmService::class, $crmService); App::instance(AppCustomerAuthService::class, $authService); $employeeRepo = resolve(EmployeeRepository::class); $action = new RegisterCustomer( authService: $authService, crmService: $crmService, employeeRepository: $employeeRepo, ); $input = new RegisterCustomerInput( matricule: new Matricule('9999999'), code: new Code('ABCDYZ'), // no I, L, O, V in Code VO firstname: 'John', lastname: 'Doe', email: new Email('ghost@example.com'), password: 'secretPassword123!', birthDate: new \DateTimeImmutable('1990-01-01'), phone: null, address: null, postcode: '75001', city: 'Paris', ); // WHEN & THEN expect(fn () => $action($input))->toThrow(ModelNotFoundException::class); }); it('throws ValidationException when CRM indicates customer already exists', function (): void { // GIVEN $matricule = new Matricule('2345678'); $code = new Code('BCDEFG'); $email = new Email('existing@example.com'); $employee = new Employee(); $employee->matricule = $matricule; $employee->code = $code; $employee->nom = 'Existing'; $employee->prenom = 'User'; $employee->etablissement = new ShopperEstablishmentCode('ETAB01'); $employee->site = new ShopperSiteCode('SITE'); $employee->service = new ShopperServiceCode('1'); $employee->saveOrFail(); $crmService = \Mockery::mock(CrmService::class); $crmService->shouldReceive('persist') ->once() ->andThrow(new CrmCustomerExistsException()); App::instance(CrmService::class, $crmService); $authService = \Mockery::mock(AppCustomerAuthService::class); App::instance(AppCustomerAuthService::class, $authService); $employeeRepo = resolve(EmployeeRepository::class); $action = new RegisterCustomer( authService: $authService, crmService: $crmService, employeeRepository: $employeeRepo, ); $input = new RegisterCustomerInput( matricule: $matricule, code: $code, firstname: 'User', lastname: 'Existing', email: $email, password: 'secretPassword123!', birthDate: new \DateTimeImmutable('1990-01-01'), phone: null, address: null, postcode: '75001', city: 'Paris', ); // WHEN & THEN expect(fn () => $action($input))->toThrow(ValidationException::class); }); it('throws ValidationException when CRM fails with CrmServiceException', function (): void { // GIVEN $matricule = new Matricule('3456789'); $code = new Code('CDEFGH'); $email = new Email('crmerror@example.com'); $employee = new Employee(); $employee->matricule = $matricule; $employee->code = $code; $employee->nom = 'Error'; $employee->prenom = 'Crm'; $employee->etablissement = new ShopperEstablishmentCode('ETAB01'); $employee->site = new ShopperSiteCode('SITE'); $employee->service = new ShopperServiceCode('1'); $employee->saveOrFail(); $crmService = \Mockery::mock(CrmService::class); $crmService->shouldReceive('persist') ->once() ->andThrow(CrmServiceException::of('CRM service offline')); App::instance(CrmService::class, $crmService); $authService = \Mockery::mock(AppCustomerAuthService::class); App::instance(AppCustomerAuthService::class, $authService); $employeeRepo = resolve(EmployeeRepository::class); $action = new RegisterCustomer( authService: $authService, crmService: $crmService, employeeRepository: $employeeRepo, ); $input = new RegisterCustomerInput( matricule: $matricule, code: $code, firstname: 'Crm', lastname: 'Error', email: $email, password: 'secretPassword123!', birthDate: new \DateTimeImmutable('1990-01-01'), phone: null, address: null, postcode: '75001', city: 'Paris', ); // WHEN & THEN expect(fn () => $action($input))->toThrow(ValidationException::class); }); it('throws UnexpectedValueException when CRM returns no hashcode', function (): void { // GIVEN $matricule = new Matricule('4567890'); $code = new Code('DEFGHJ'); $email = new Email('nohash@example.com'); $employee = new Employee(); $employee->matricule = $matricule; $employee->code = $code; $employee->nom = 'No'; $employee->prenom = 'Hash'; $employee->etablissement = new ShopperEstablishmentCode('ETAB01'); $employee->site = new ShopperSiteCode('SITE'); $employee->service = new ShopperServiceCode('1'); $employee->saveOrFail(); $crmService = \Mockery::mock(CrmService::class); $crmService->shouldReceive('persist') ->once() ->andReturn(new CrmCustomer( hashcode: null, email: $email->value, password: null, firstname: 'Hash', lastname: 'No', birthDate: '1990-01-01', phone: null, address: null, address2: null, city: 'Paris', postcode: '75001', country: Country::france, optIn: false, status: false, lastConnection: null, )); App::instance(CrmService::class, $crmService); $authService = \Mockery::mock(AppCustomerAuthService::class); App::instance(AppCustomerAuthService::class, $authService); $employeeRepo = resolve(EmployeeRepository::class); $action = new RegisterCustomer( authService: $authService, crmService: $crmService, employeeRepository: $employeeRepo, ); $input = new RegisterCustomerInput( matricule: $matricule, code: $code, firstname: 'Hash', lastname: 'No', email: $email, password: 'secretPassword123!', birthDate: new \DateTimeImmutable('1990-01-01'), phone: null, address: null, postcode: '75001', city: 'Paris', ); // WHEN & THEN expect(fn () => $action($input))->toThrow(\UnexpectedValueException::class); }); it('throws when invalid ActionInput is passed', function (): void { // GIVEN $crmService = \Mockery::mock(CrmService::class); $authService = \Mockery::mock(AppCustomerAuthService::class); App::instance(CrmService::class, $crmService); App::instance(AppCustomerAuthService::class, $authService); $employeeRepo = resolve(EmployeeRepository::class); $action = new RegisterCustomer( authService: $authService, crmService: $crmService, employeeRepository: $employeeRepo, ); $invalidInput = new class () implements ActionInput { }; // WHEN & THEN expect(fn () => $action($invalidInput))->toThrow(\InvalidArgumentException::class); }); });