toString(); $customer = new Customer(); $customer->email = new Email('jeanne.martin@example.com'); $customer->hashcode = new CrmHashcode($hashcodeUuid); $customer->saveOrFail(); $this->actingAs($customer, 'frontend'); $expectedCrmCustomer = makeCrmCustomerDto($hashcode, $customer->email->value); /** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */ $crmService = \Mockery::mock(CrmService::class); $crmService->shouldReceive('updateCustomerInfos') ->once() ->withArgs(function (Customer $passedCustomer, array $data) use ($customer): bool { return $passedCustomer->id === $customer->id && $data === [ 'firstname' => 'Jeanne', 'lastname' => 'Martin', 'birth_date' => '1992-05-14', 'postcode' => '53000', 'city' => 'Laval', 'opt_in' => true, 'phone' => '+33611223344', 'address' => '12 rue des Lilas', ]; }) ->andReturn($expectedCrmCustomer); $authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService); $action = new UpdateCustomer($authService, $crmService); $input = new UpdateCustomerInput( firstname: 'Jeanne', lastname: 'Martin', birthDate: '1992-05-14', phone: '+33611223344', address: '12 rue des Lilas', postcode: '53000', city: 'Laval', optIn: true, ); // WHEN $output = $action($input); // THEN expect($output->result)->toBe($expectedCrmCustomer); }); it('omits nullable phone and address when null and propagates validation exceptions', function (): void { /** @var \Tests\TestCase $this */ // GIVEN $hashcodeUuid = UuidHelper::v4(); $customer = new Customer(); $customer->email = new Email('no.phone@example.com'); $customer->hashcode = new CrmHashcode($hashcodeUuid); $customer->saveOrFail(); $this->actingAs($customer, 'frontend'); $validationException = ValidationException::withMessages(['postcode' => ['Code postal invalide']]); /** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */ $crmService = \Mockery::mock(CrmService::class); $crmService->shouldReceive('updateCustomerInfos') ->once() ->withArgs(function (Customer $_, array $data): bool { return ! array_key_exists('phone', $data) && ! array_key_exists('address', $data) && $data['opt_in'] === false; }) ->andReturn($validationException); $authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService); $action = new UpdateCustomer($authService, $crmService); $input = new UpdateCustomerInput( firstname: 'Paul', lastname: 'Durand', birthDate: '1985-03-10', phone: null, address: null, postcode: '00000', city: 'Sablé', optIn: false, ); // WHEN $output = $action($input); // THEN expect($output->result)->toBe($validationException); }); it('returns false output when CRM service throws an unexpected error', function (): void { /** @var \Tests\TestCase $this */ // GIVEN $customer = new Customer(); $customer->email = new Email('err.crm@example.com'); $customer->hashcode = new CrmHashcode(UuidHelper::v4()); $customer->saveOrFail(); $this->actingAs($customer, 'frontend'); /** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */ $crmService = \Mockery::mock(CrmService::class); $crmService->shouldReceive('updateCustomerInfos') ->once() ->andThrow(new \RuntimeException('CRM unavailable')); $authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService); $action = new UpdateCustomer($authService, $crmService); $input = new UpdateCustomerInput( firstname: 'Paul', lastname: 'Durand', birthDate: '1985-03-10', phone: null, address: null, postcode: '72300', city: 'Sablé', optIn: true, ); // WHEN $output = $action($input); // THEN expect($output->result)->toBeFalse(); }); it('throws AuthServiceException when no customer is authenticated', function (): void { // GIVEN /** @var \App\Services\CrmService&\Mockery\MockInterface $crmService */ $crmService = \Mockery::mock(CrmService::class); $crmService->shouldNotReceive('updateCustomerInfos'); $authService = new AppCustomerAuthService(Auth::guard('frontend'), $crmService); $action = new UpdateCustomer($authService, $crmService); $input = new UpdateCustomerInput( firstname: 'Guest', lastname: 'User', birthDate: '1990-01-01', phone: null, address: null, postcode: '72300', city: 'Sablé', optIn: false, ); // WHEN & THEN expect(fn () => $action($input)) ->toThrow(AuthServiceException::class); }); });