87 lines
2.6 KiB
PHP
87 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Model;
|
|
|
|
use App\Model\DestinationDto;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface;
|
|
use Symfony\Component\Validator\Validation;
|
|
|
|
/**
|
|
* The stored country moved to ISO-3166-1 alpha-2, but the constraint on this DTO kept the
|
|
* old BusProNet abbreviations while the form was already offering the new codes. Every
|
|
* country except Schweiz and Niederlande - the two whose code happens to be unchanged -
|
|
* was rejected as "Ungültige Auswahl" on save, so a destination could not be created or
|
|
* edited for Österreich, Deutschland, Frankreich or Italien at all.
|
|
*
|
|
* The codes are asserted literally rather than read back off the map they come from, so
|
|
* that changing the map has to be a deliberate act rather than something a test agrees
|
|
* with by construction.
|
|
*/
|
|
class DestinationDtoTest extends TestCase
|
|
{
|
|
public function testTheAuthorableCountriesAreTheIsoCodes(): void
|
|
{
|
|
$this->assertSame(['at', 'ch', 'fr', 'it', 'de', 'nl'], DestinationDto::countryCodes());
|
|
}
|
|
|
|
/**
|
|
* @dataProvider isoCountryCodes
|
|
*/
|
|
public function testAnIsoCountryCodeIsAccepted(string $code): void
|
|
{
|
|
$this->assertCount(0, $this->validateCountry($code));
|
|
}
|
|
|
|
/**
|
|
* The four that the form offered and the constraint refused.
|
|
*
|
|
* @dataProvider busProNetCountryAbbreviations
|
|
*/
|
|
public function testABusProNetAbbreviationIsNoLongerAccepted(string $abbreviation): void
|
|
{
|
|
$this->assertCount(1, $this->validateCountry($abbreviation));
|
|
}
|
|
|
|
/**
|
|
* MyE&P drops a country it cannot map to two letters, so a destination without one is
|
|
* a normal row rather than an invalid one.
|
|
*/
|
|
public function testNoCountryIsAccepted(): void
|
|
{
|
|
$this->assertCount(0, $this->validateCountry(null));
|
|
}
|
|
|
|
/**
|
|
* @return iterable<array{string}>
|
|
*/
|
|
public static function isoCountryCodes(): iterable
|
|
{
|
|
foreach (DestinationDto::countryCodes() as $code) {
|
|
yield $code => [$code];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return iterable<array{string}>
|
|
*/
|
|
public static function busProNetCountryAbbreviations(): iterable
|
|
{
|
|
yield 'a' => ['a'];
|
|
yield 'd' => ['d'];
|
|
yield 'f' => ['f'];
|
|
yield 'i' => ['i'];
|
|
}
|
|
|
|
private function validateCountry(?string $country): ConstraintViolationListInterface
|
|
{
|
|
return Validation::createValidatorBuilder()
|
|
->enableAttributeMapping()
|
|
->getValidator()
|
|
->validateProperty((new DestinationDto())->setCountry($country), 'country')
|
|
;
|
|
}
|
|
}
|