users = new InMemoryUserAccountRepository; $this->userId = $this->users->save(UserAccount::register(new PersonName('Olli'), new EmailAddress('olli@example.test'), Role::Employee))->id(); $this->secrets = new class implements LoginSecrets { /** @var array */ public array $passwords = []; /** @var array */ public array $pins = []; public function passwordHashOf(int $userId): ?string { return $this->passwords[$userId] ?? null; } public function pinHashOf(int $userId): ?string { return $this->pins[$userId] ?? null; } public function storePinHash(int $userId, ?string $hash): void { if ($hash === null) { unset($this->pins[$userId]); } else { $this->pins[$userId] = $hash; } } }; $this->secrets->passwords[$this->userId] = 'hashed:Salasana-123!'; $hasher = new class implements SecretHasher { public function hash(string $secret): string { return 'hashed:'.$secret; } public function check(string $secret, string $hash): bool { return $hash === 'hashed:'.$secret; } }; $this->clock = new FakeClock(new DateTimeImmutable('2027-05-31')); $this->pins = new LoginPins($this->secrets, $hasher, new CheckLoginAllowed($this->users, $this->clock), new FakeTransactionManager); }); it('sets a PIN code only with the right password and verifies it', function () { expect(fn () => $this->pins->set($this->userId, 'väärä', '2468'))->toThrow(WrongPassword::class); $this->pins->set($this->userId, 'Salasana-123!', '2468'); expect($this->secrets->pins[$this->userId])->toBe('hashed:2468') ->and($this->pins->verify($this->userId, '2468'))->toBeTrue() ->and($this->pins->verify($this->userId, '1111'))->toBeFalse() ->and($this->pins->verify(999, '2468'))->toBeFalse(); }); it('refuses a right PIN code of an intern whose last day has passed', function () { $internId = $this->users->save(UserAccount::register(new PersonName('Ida'), new EmailAddress('ida@example.test'), Role::Intern, new DateTimeImmutable('2027-05-31'), $this->userId))->id(); $this->secrets->passwords[$internId] = 'hashed:Salasana-123!'; $this->pins->set($internId, 'Salasana-123!', '1357'); expect($this->pins->verify($internId, '1357'))->toBeTrue(); $this->clock->today = new DateTimeImmutable('2027-06-01'); expect($this->pins->verify($internId, '1357'))->toBeFalse(); });