shouldReceive('unsubscribeCustomer') ->once() ->with(\Mockery::on(static fn (CrmHashcode $h): bool => $h->value === $hashcode->value)) ->andReturnTrue(); $action = new UnsubscribeFromNewsletter($crmService); // WHEN $output = $action(new UnsubscribeFromNewsletterInput($hashcode)); // THEN expect($output)->toBe(UnsubscribeFromNewsletterOutput::SUCCESS); }); it('returns FAILURE when CRM service returns false', function (): void { // GIVEN $hashcode = new CrmHashcode(UuidHelper::v4()); /** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */ $crmService = \Mockery::mock(CrmService::class); $crmService->shouldReceive('unsubscribeCustomer') ->once() ->with(\Mockery::on(static fn (CrmHashcode $h): bool => $h->value === $hashcode->value)) ->andReturnFalse(); $action = new UnsubscribeFromNewsletter($crmService); // WHEN $output = $action(new UnsubscribeFromNewsletterInput($hashcode)); // THEN expect($output)->toBe(UnsubscribeFromNewsletterOutput::FAILURE); }); it('returns FAILURE without throwing when CRM service throws an exception', function (): void { // GIVEN $hashcode = new CrmHashcode(UuidHelper::v4()); /** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */ $crmService = \Mockery::mock(CrmService::class); $crmService->shouldReceive('unsubscribeCustomer') ->once() ->andThrow(new \RuntimeException('CRM timeout')); $action = new UnsubscribeFromNewsletter($crmService); // WHEN $output = $action(new UnsubscribeFromNewsletterInput($hashcode)); // THEN expect($output)->toBe(UnsubscribeFromNewsletterOutput::FAILURE); }); });