102 lines
3.3 KiB
PHP
102 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Validator\Constraints;
|
|
|
|
use App\BusProNet\DataProvider\HotelDataProvider;
|
|
use App\BusProNet\Model\Hotel;
|
|
use App\Validator\Constraints\ResolvableHotel;
|
|
use App\Validator\Constraints\ResolvableHotelValidator;
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
|
|
|
/**
|
|
* Everything a destination records about its house is derived from this lookup, so an id that
|
|
* does not resolve used to reach DestinationTrait::updateEntity() and fail there on a null -
|
|
* a 500 on a form submit rather than a message on a field.
|
|
*
|
|
* The two causes are reported apart on purpose. An unreachable MyE&P is not the operator's
|
|
* doing, and telling them their choice was invalid sends them hunting for a different answer
|
|
* when the one they gave was right.
|
|
*/
|
|
class ResolvableHotelValidatorTest extends ConstraintValidatorTestCase
|
|
{
|
|
public function testAResolvableHotelPasses(): void
|
|
{
|
|
$this->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')
|
|
;
|
|
}
|
|
}
|