forceCreate([ 'email' => new Email('action-reset@example.com'), 'hashcode' => (string) Str::uuid(), ]); $shopper = new Shopper( id: $customer->getId(), email: new Email('action-reset@example.com'), matricule: new Matricule('1234567'), nom: 'Doe', prenom: 'John', startDate: CarbonImmutable::now()->subMonth(), endDate: null, etablissement: new ShopperEstablishmentCode('FAC01'), site: new ShopperSiteCode('SITE'), service: new ShopperServiceCode('101'), ); $shopperService = \Mockery::mock(ShopperService::class); $shopperService->shouldReceive('getShopperFor') ->once() ->with(\Mockery::on(fn (ShopperId $id): bool => $id->equals($customer->getId()))) ->andReturn($shopper); $authService = \Mockery::mock(AppCustomerAuthService::class); $authService->shouldReceive('resetPasswordFor') ->once() ->with($customer, 'NewPassword123!') ->andReturnTrue(); $authService->shouldReceive('hasVerifiedEmail') ->once() ->with($customer) ->andReturnTrue(); $action = new ResetCustomerPassword($shopperService, $authService); $input = new ResetCustomerPasswordInput($customer, 'NewPassword123!'); // WHEN $output = $action($input); // THEN expect($output)->toBeInstanceOf(ResetCustomerPasswordOutput::class) ->and($output->status)->toBe(ResetCustomerPasswordStatus::SUCCESS) ->and($output->emailVerified)->toBeTrue() ->and($customer->fresh()?->getRememberToken())->not->toBeNull(); }); it('returns notEmployed output when shopper is missing or inactive', function (): void { // GIVEN /** @var Customer $customer */ $customer = Customer::query()->forceCreate([ 'email' => new Email('action-inactive@example.com'), 'hashcode' => (string) Str::uuid(), ]); $inactiveShopper = new Shopper( id: $customer->getId(), email: new Email('action-inactive@example.com'), matricule: new Matricule('1234568'), nom: 'Doe', prenom: 'Jane', startDate: CarbonImmutable::now()->subMonth(), endDate: CarbonImmutable::now()->subDay(), etablissement: new ShopperEstablishmentCode('FAC01'), site: new ShopperSiteCode('SITE'), service: new ShopperServiceCode('101'), ); $shopperService = \Mockery::mock(ShopperService::class); $shopperService->shouldReceive('getShopperFor')->once()->andReturn($inactiveShopper); $authService = \Mockery::mock(AppCustomerAuthService::class); $authService->shouldNotReceive('resetPasswordFor'); $action = new ResetCustomerPassword($shopperService, $authService); // WHEN $output = $action(new ResetCustomerPasswordInput($customer, 'NewPassword123!')); // THEN expect($output->status)->toBe(ResetCustomerPasswordStatus::NOT_EMPLOYED) ->and($customer->fresh()?->getRememberToken())->toBeNull(); }); it('returns invalidPassword output when CRM rejects the new password', function (): void { // GIVEN /** @var Customer $customer */ $customer = Customer::query()->forceCreate([ 'email' => new Email('action-invalid@example.com'), 'hashcode' => (string) Str::uuid(), ]); $shopper = new Shopper( id: $customer->getId(), email: new Email('action-invalid@example.com'), matricule: new Matricule('1234569'), nom: 'Doe', prenom: 'Alice', startDate: CarbonImmutable::now()->subMonth(), endDate: null, etablissement: new ShopperEstablishmentCode('FAC01'), site: new ShopperSiteCode('SITE'), service: new ShopperServiceCode('101'), ); $shopperService = \Mockery::mock(ShopperService::class); $shopperService->shouldReceive('getShopperFor')->once()->andReturn($shopper); $authService = \Mockery::mock(AppCustomerAuthService::class); $authService->shouldReceive('resetPasswordFor') ->once() ->andReturn(ValidationException::withMessages(['password' => 'Too weak'])); $action = new ResetCustomerPassword($shopperService, $authService); // WHEN $output = $action(new ResetCustomerPasswordInput($customer, 'weak')); // THEN expect($output->status)->toBe(ResetCustomerPasswordStatus::INVALID_PASSWORD) ->and($output->errorMessage)->toContain('Too weak') ->and($customer->fresh()?->getRememberToken())->toBeNull(); }); });