Files
myep-team/tests/MyEp/Api/ResponseMapperTest.php
T

168 lines
6.1 KiB
PHP

<?php
namespace App\Tests\MyEp\Api;
use App\BusProNet\Model\Country;
use App\BusProNet\Model\Hotel;
use App\BusProNet\Model\Pickup;
use App\MyEp\Api\ResponseMapper;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
class ResponseMapperTest extends TestCase
{
public function testHotelsAreKeyedByTheirBusProId(): void
{
$hotels = $this->getMapperInstance()->mapHotels([
['id' => 163959, 'code' => 'LPJGR', 'name' => 'Sportclub Jenatsch'],
['id' => 202003, 'code' => 'SRR', 'name' => 'Berggasthof Ranalt'],
]);
$this->assertEquals([163959, 202003], array_keys($hotels));
$this->assertInstanceOf(Hotel::class, $hotels[163959]);
$this->assertEquals(163959, $hotels[163959]->getBusProId());
$this->assertEquals('LPJGR', $hotels[163959]->getCode());
$this->assertEquals('Sportclub Jenatsch', $hotels[163959]->getName());
}
public function testHotelFieldsAreMapped(): void
{
$hotels = $this->getMapperInstance()->mapHotels([[
'id' => 163959,
'code' => 'LPJGR',
'name' => 'Sportclub Jenatsch',
'city' => 'Lenzerheide',
'street' => 'Voa Principala 1',
'phone' => '+41 81 000000',
'country' => 'CH',
'countryCode' => 'ch',
'costUnit' => '4711',
]]);
$hotel = $hotels[163959];
$this->assertEquals('Lenzerheide', $hotel->getCity());
$this->assertEquals('Voa Principala 1', $hotel->getStreet());
$this->assertEquals('+41 81 000000', $hotel->getPhone());
$this->assertEquals('4711', $hotel->getCostUnit());
}
public function testTheRawCountryTokenIsKeptAsReported(): void
{
// the raw token is not a standard, it is the join key into the country base data
$hotels = $this->getMapperInstance()->mapHotels([['id' => 1, 'country' => 'AUT']]);
$this->assertEquals('AUT', $hotels[1]->getCountry());
}
public function testTheCountryCodeIsLowercasedIso(): void
{
// this is what reaches the two character column and the flag icons
$hotels = $this->getMapperInstance()->mapHotels([['id' => 1, 'country' => 'AUT', 'countryCode' => 'AT']]);
$this->assertEquals('at', $hotels[1]->getCountryCode());
}
public function testAnUnresolvedCountryCodeIsNull(): void
{
$hotels = $this->getMapperInstance()->mapHotels([
['id' => 1, 'country' => 'XYZ'],
['id' => 2, 'country' => 'SLO', 'countryCode' => null],
['id' => 3, 'country' => 'D', 'countryCode' => 'deu'],
]);
$this->assertNull($hotels[1]->getCountryCode());
$this->assertNull($hotels[2]->getCountryCode());
$this->assertNull($hotels[3]->getCountryCode(), 'a three letter code does not fit the column');
}
public function testOptionalHotelFieldsMayBeAbsent(): void
{
$hotels = $this->getMapperInstance()->mapHotels([['id' => 1, 'code' => 'AAA']]);
$this->assertNull($hotels[1]->getCountryCode());
$this->assertNull($hotels[1]->getCostUnit());
$this->assertNull($hotels[1]->getType());
$this->assertNull($hotels[1]->getPhone());
}
public function testHotelsWithoutAnIdAreSkipped(): void
{
$hotels = $this->getMapperInstance()->mapHotels([
['code' => 'AAA'],
['id' => 0, 'code' => 'BBB'],
['id' => 7, 'code' => 'CCC'],
]);
$this->assertEquals([7], array_keys($hotels));
}
public function testPickupsAreKeyedByTheirBusProId(): void
{
$pickups = $this->getMapperInstance()->mapPickups([
['id' => 4, 'code' => '298', 'city' => 'Karlsruhe', 'street' => 'A5 - Autohof Bruchsal'],
]);
$this->assertEquals([4], array_keys($pickups));
$this->assertInstanceOf(Pickup::class, $pickups[4]);
$this->assertEquals(4, $pickups[4]->getBusProId());
$this->assertEquals(4, $pickups[4]->getId());
$this->assertEquals('298', $pickups[4]->getCode());
$this->assertEquals('Karlsruhe', $pickups[4]->getCity());
$this->assertEquals('A5 - Autohof Bruchsal', $pickups[4]->getStreet());
}
public function testAPickupCarriesNoTimeOfItsOwn(): void
{
// a departure time belongs to a boarding stop on a date, not to the stop itself
$pickups = $this->getMapperInstance()->mapPickups([['id' => 4, 'time' => '23:59']]);
$this->assertNull($pickups[4]->getTime());
}
public function testPickupsWithoutAnIdAreSkipped(): void
{
$pickups = $this->getMapperInstance()->mapPickups([['city' => 'Karlsruhe'], ['id' => 4]]);
$this->assertEquals([4], array_keys($pickups));
}
public function testCountriesAreKeyedByTheirToken(): void
{
$countries = $this->getMapperInstance()->mapCountries([
['id' => 1, 'name' => 'Schweiz', 'token' => 'CH', 'nationality' => 'schweizerisch'],
]);
$this->assertEquals(['CH'], array_keys($countries));
$this->assertInstanceOf(Country::class, $countries['CH']);
$this->assertEquals(1, $countries['CH']->getId());
$this->assertEquals('Schweiz', $countries['CH']->getName());
$this->assertEquals('CH', $countries['CH']->getToken());
$this->assertEquals('schweizerisch', $countries['CH']->getNationality());
}
public function testCountriesWithoutATokenAreSkipped(): void
{
$countries = $this->getMapperInstance()->mapCountries([
['id' => 1, 'name' => 'Schweiz'],
['id' => 2, 'name' => 'Österreich', 'token' => 'A'],
]);
$this->assertEquals(['A'], array_keys($countries));
}
public function testAnEmptyListMapsToAnEmptyResult(): void
{
$mapper = $this->getMapperInstance();
$this->assertEquals([], $mapper->mapHotels([]));
$this->assertEquals([], $mapper->mapPickups([]));
$this->assertEquals([], $mapper->mapCountries([]));
}
private function getMapperInstance(): ResponseMapper
{
return new ResponseMapper(new NullLogger());
}
}