shouldNotReceive('getCustomerFromEmail'); $provider = new CrmUserProvider($hasher, Customer::class, $crmService); // WHEN $result = $provider->retrieveByCredentials(['email' => 'crm-only@example.com']); // THEN expect($result)->toBeNull() ->and(Customer::query()->count())->toBe(0); }); it('returns the existing local customer when CRM also confirms it', function (): void { // GIVEN /** @var Customer $customer */ $customer = Customer::query()->forceCreate([ 'email' => new Email('existing-local@example.com'), 'hashcode' => (string) Str::uuid(), ]); $crmCustomer = new CrmCustomer( hashcode: $customer->hashcode->value, email: 'existing-local@example.com', password: null, firstname: 'John', lastname: 'Doe', birthDate: null, phone: null, address: null, address2: null, city: null, postcode: null, country: Country::france, optIn: false, status: true, lastConnection: null, ); $hasher = new BcryptHasher(); /** @var \Mockery\MockInterface&\App\Services\CrmService $crmService */ $crmService = \Mockery::mock(CrmService::class); $crmService->shouldReceive('getCustomerFromEmail') ->once() ->with('existing-local@example.com') ->andReturn($crmCustomer); $provider = new CrmUserProvider($hasher, Customer::class, $crmService); // WHEN $result = $provider->retrieveByCredentials(['email' => 'existing-local@example.com']); // THEN expect($result)->toBeInstanceOf(Customer::class) ->and($result?->getAuthIdentifier())->toBe($customer->id); }); it('catches mail delivery failures when resending activation email for unactivated accounts', function (): void { // GIVEN $customer = new Customer(['email' => new Email('unverified@example.com')]); $dispatcher = \Mockery::mock(Dispatcher::class); $dispatcher->shouldReceive('send') ->once() ->andThrow(new \RuntimeException('SMTP connection refused')); app()->instance(Dispatcher::class, $dispatcher); $hasher = new BcryptHasher(); /** @var \Mockery\MockInterface&\App\Services\CrmService $crmService */ $crmService = \Mockery::mock(CrmService::class); $crmService->shouldReceive('login') ->once() ->with('unverified@example.com', 'valid-password') ->andThrow(new CrmCustomerNotActivatedException()); $provider = new CrmUserProvider($hasher, Customer::class, $crmService); // WHEN $isValid = $provider->validateCredentials($customer, [ 'email' => 'unverified@example.com', 'password' => 'valid-password', ]); // THEN expect($isValid)->toBeTrue(); }); });