103 lines
3.3 KiB
PHP
103 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\BusProNet\Utility;
|
|
|
|
use App\BusProNet\Utility\CountryCodeMapper;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Psr\Log\LoggerInterface;
|
|
use Psr\Log\NullLogger;
|
|
|
|
class CountryCodeMapperTest extends TestCase
|
|
{
|
|
/**
|
|
* @return array<string, array{string, string}>
|
|
*/
|
|
public static function tokenProvider(): array
|
|
{
|
|
return [
|
|
'iso-2 passes through' => ['CH', 'ch'],
|
|
'vehicle code D' => ['D', 'de'],
|
|
'iso-3 DEU' => ['DEU', 'de'],
|
|
'vehicle code A' => ['A', 'at'],
|
|
'iso-2 AT' => ['AT', 'at'],
|
|
'iso-3 AUT' => ['AUT', 'at'],
|
|
'vehicle code F' => ['F', 'fr'],
|
|
'vehicle code I' => ['I', 'it'],
|
|
'iso-2 IT' => ['IT', 'it'],
|
|
'vehicle code E' => ['E', 'es'],
|
|
'vehicle code B' => ['B', 'be'],
|
|
'vehicle code S is Sweden' => ['S', 'se'],
|
|
'vehicle code N is Norway' => ['N', 'no'],
|
|
'vehicle code FL is Liechtenstein' => ['FL', 'li'],
|
|
'IR is Ireland, not Iran' => ['IR', 'ie'],
|
|
'UK maps to the iso GB' => ['UK', 'gb'],
|
|
'iso-3 PRT' => ['PRT', 'pt'],
|
|
'iso-3 GRE' => ['GRE', 'gr'],
|
|
'iso-3 HRV' => ['HRV', 'hr'],
|
|
'iso-3 RUS' => ['RUS', 'ru'],
|
|
'iso-3 USA' => ['USA', 'us'],
|
|
'SI is Slovenia' => ['SI', 'si'],
|
|
'SLO is Slovakia' => ['SLO', 'sk'],
|
|
'ZA is South Africa' => ['ZA', 'za'],
|
|
];
|
|
}
|
|
|
|
#[DataProvider('tokenProvider')]
|
|
public function testMapsKnownToken(string $token, string $expected): void
|
|
{
|
|
$this->assertSame($expected, (new CountryCodeMapper(new NullLogger()))->map($token));
|
|
}
|
|
|
|
public function testEveryMappedCodeIsLowercaseIsoTwo(): void
|
|
{
|
|
$mapper = new CountryCodeMapper(new NullLogger());
|
|
|
|
foreach (array_keys(self::tokenProvider()) as $name) {
|
|
[$token] = self::tokenProvider()[$name];
|
|
$code = $mapper->map($token);
|
|
|
|
$this->assertNotNull($code);
|
|
$this->assertMatchesRegularExpression('/^[a-z]{2}$/', $code, $token.' must map to lowercase ISO-2');
|
|
}
|
|
}
|
|
|
|
public function testIsCaseAndWhitespaceInsensitive(): void
|
|
{
|
|
$mapper = new CountryCodeMapper(new NullLogger());
|
|
|
|
$this->assertSame('ch', $mapper->map('ch'));
|
|
$this->assertSame('at', $mapper->map(' aut '));
|
|
}
|
|
|
|
public function testUnknownTokenMapsToNullAndWarnsOnce(): void
|
|
{
|
|
$logger = $this->createMock(LoggerInterface::class);
|
|
$logger
|
|
->expects($this->once())
|
|
->method('warning')
|
|
->with('Unmapped BusPro country token', ['token' => 'XX']);
|
|
|
|
$mapper = new CountryCodeMapper($logger);
|
|
|
|
$this->assertNull($mapper->map('XX'));
|
|
$this->assertNull($mapper->map('XX'));
|
|
}
|
|
|
|
public function testAbsentTokenMapsToNullWithoutWarning(): void
|
|
{
|
|
$logger = $this->createMock(LoggerInterface::class);
|
|
$logger
|
|
->expects($this->never())
|
|
->method('warning');
|
|
|
|
$mapper = new CountryCodeMapper($logger);
|
|
|
|
$this->assertNull($mapper->map(null));
|
|
$this->assertNull($mapper->map(''));
|
|
$this->assertNull($mapper->map(' '));
|
|
}
|
|
}
|