validator = $this->createValidator([163959 => $this->hotel()]); $this->validator->initialize($this->context); $this->validator->validate('163959', new ResolvableHotel()); $this->assertNoViolation(); } public function testAHotelMissingFromALoadedListIsRejectedAsUnknown(): void { $constraint = new ResolvableHotel(); $this->validator = $this->createValidator([163959 => $this->hotel()]); $this->validator->initialize($this->context); $this->validator->validate('999999', $constraint); $this->buildViolation($constraint->unknownMessage)->assertRaised(); } /** * StaleWhileErrorCache answers [] rather than throwing, so an empty list is the only signal * that MyE&P could not be reached - and on a cold cache there is no stale copy to fall back to. */ public function testAnEmptyListIsReportedAsAnOutageRatherThanABadChoice(): void { $constraint = new ResolvableHotel(); $this->validator = $this->createValidator([]); $this->validator->initialize($this->context); $this->validator->validate('163959', $constraint); $this->buildViolation($constraint->unavailableMessage)->assertRaised(); } /** * Whether a hotel was chosen at all belongs to NotBlank, which already sits on the property. */ public function testAnEmptyValueIsLeftToNotBlank(): void { $this->validator = $this->createValidator([]); $this->validator->initialize($this->context); $this->validator->validate(null, new ResolvableHotel()); $this->validator->validate('', new ResolvableHotel()); $this->assertNoViolation(); } protected function createValidator(array $hotels = []): ResolvableHotelValidator { return new ResolvableHotelValidator($this->provider($hotels)); } /** * @return HotelDataProvider&MockObject */ private function provider(array $hotels): object { $provider = $this->createStub(HotelDataProvider::class); $provider->method('getAll')->willReturn($hotels); return $provider; } private function hotel(): Hotel { return (new Hotel()) ->setBusProId(163959) ->setCode('LPJGR') ->setName('Sportclub Jenatsch') ; } }