toString(); $customer = new Customer(); $customer->email = new Email('claire.dupuis@example.com'); $customer->hashcode = new CrmHashcode($hashcodeUuid); $customer->saveOrFail(); $this->actingAs($customer, 'frontend'); $crmCustomer = makePasswordTestCrmCustomer($hashcode, $customer->email->value); /** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */ $crmService = \Mockery::mock(CrmService::class); $crmService->shouldReceive('getCustomerFromHashCode') ->once() ->with(\Mockery::on(static fn (CrmHashcode $h): bool => $h->value === $hashcode)) ->andReturn($crmCustomer); $crmService->shouldReceive('changePassword') ->once() ->with($crmCustomer, 'OldPass123!', 'NewPass456!') ->andReturn($crmCustomer); $authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService); $action = new UpdateCustomerPassword($authService, $crmService); // WHEN $output = $action(new UpdateCustomerPasswordInput('OldPass123!', 'NewPass456!')); // THEN expect($output)->toBeInstanceOf(UpdateCustomerPasswordOutput::class) ->and($output->result)->toBe($crmCustomer); }); it('returns false output when customer cannot be resolved in CRM', function (): void { /** @var \Tests\TestCase $this */ // GIVEN $customer = new Customer(); $customer->email = new Email('missing.crm@example.com'); $customer->hashcode = new CrmHashcode(Uuid::uuid4()); $customer->saveOrFail(); $this->actingAs($customer, 'frontend'); /** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */ $crmService = \Mockery::mock(CrmService::class); $crmService->shouldReceive('getCustomerFromHashCode') ->once() ->andReturnNull(); $crmService->shouldNotReceive('changePassword'); $authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService); $action = new UpdateCustomerPassword($authService, $crmService); // WHEN $output = $action(new UpdateCustomerPasswordInput('OldPass123!', 'NewPass456!')); // THEN expect($output->result)->toBeFalse(); }); it('returns false output when changePassword throws an unexpected exception', function (): void { /** @var \Tests\TestCase $this */ // GIVEN $hashcodeUuid = Uuid::uuid4(); $hashcode = $hashcodeUuid->toString(); $customer = new Customer(); $customer->email = new Email('pwd.error@example.com'); $customer->hashcode = new CrmHashcode($hashcodeUuid); $customer->saveOrFail(); $this->actingAs($customer, 'frontend'); $crmCustomer = makePasswordTestCrmCustomer($hashcode, $customer->email->value); /** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */ $crmService = \Mockery::mock(CrmService::class); $crmService->shouldReceive('getCustomerFromHashCode') ->once() ->andReturn($crmCustomer); $crmService->shouldReceive('changePassword') ->once() ->andThrow(new \RuntimeException('CRM error')); $authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService); $action = new UpdateCustomerPassword($authService, $crmService); // WHEN $output = $action(new UpdateCustomerPasswordInput('OldPass123!', 'NewPass456!')); // THEN expect($output->result)->toBeFalse(); }); it('throws AuthServiceException when unauthenticated', function (): void { // GIVEN /** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */ $crmService = \Mockery::mock(CrmService::class); $crmService->shouldNotReceive('getCustomerFromHashCode'); $crmService->shouldNotReceive('changePassword'); $authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService); $action = new UpdateCustomerPassword($authService, $crmService); // WHEN & THEN expect(fn () => $action(new UpdateCustomerPasswordInput('OldPass123!', 'NewPass456!'))) ->toThrow(AuthServiceException::class); }); });