Add importers in console and admin for flights/aircraft/subfleets and airport #194

This commit is contained in:
Nabeel Shahzad
2018-03-20 19:17:11 -05:00
parent 782121829a
commit b9beb6c804
41 changed files with 1270 additions and 225 deletions

View File

@@ -1,16 +1,50 @@
<?php
use App\Services\FareService;
use App\Models\Enums\FlightType;
/**
* Class ImporterTest
*/
class ImporterTest extends TestCase
{
protected $importerSvc;
private $importBaseClass,
$importSvc,
$fareSvc;
public function setUp()
{
$this->importerSvc = app(\App\Services\ImporterService::class);
parent::setUp();
$this->importBaseClass = new \App\Interfaces\ImportExport();
$this->importSvc = app(\App\Services\ImporterService::class);
$this->fareSvc = app(\App\Services\FareService::class);
}
/**
* Add some of the basic data needed to properly import the flights.csv file
* @return mixed
*/
protected function insertFlightsScaffoldData()
{
$fare_svc = app(FareService::class);
$al = [
'icao' => 'VMS',
'name' => 'phpVMS Airlines',
];
$airline = factory(App\Models\Airline::class)->create($al);
$subfleet = factory(App\Models\Subfleet::class)->create(['type' => 'A32X']);
# Add the economy class
$fare_economy = factory(App\Models\Fare::class)->create(['code' => 'Y', 'capacity' => 150]);
$fare_svc->setForSubfleet($subfleet, $fare_economy);
# Add first class
$fare_first = factory(App\Models\Fare::class)->create(['code' => 'F', 'capacity' => 10]);
$fare_svc->setForSubfleet($subfleet, $fare_first);
return $airline;
}
/**
@@ -22,7 +56,7 @@ class ImporterTest extends TestCase
$tests = [
[
'input' => 'gate',
'expected' => 'gate'
'expected' => ['gate']
],
[
'input' => 'gate;cost index',
@@ -61,12 +95,155 @@ class ImporterTest extends TestCase
'price' => 1200
]
]
]
],
[
'input' => 'Y; F?price=1200',
'expected' => [
0 => 'Y',
'F' => [
'price' => 1200
]
]
],
[
'input' => 'Departure Gate=4;Arrival Gate=C61',
'expected' => [
'Departure Gate' => '4',
'Arrival Gate' => 'C61',
]
],
];
foreach($tests as $test) {
$parsed = $this->importerSvc->parseMultiColumnValues($test['input']);
$parsed = $this->importBaseClass->parseMultiColumnValues($test['input']);
$this->assertEquals($parsed, $test['expected']);
}
}
/**
* Test the flight importer
* @throws \League\Csv\Exception
*/
public function testFlightImporter(): void
{
$airline = $this->insertFlightsScaffoldData();
$file_path = base_path('tests/data/flights.csv');
$this->importSvc->importFlights($file_path);
// See if it imported
$flight = \App\Models\Flight::where([
'airline_id' => $airline->id,
'flight_number' => '1972'
])->first();
$this->assertNotNull($flight);
// Check the flight itself
$this->assertEquals('KAUS', $flight->dpt_airport_id);
$this->assertEquals('KJFK', $flight->arr_airport_id);
$this->assertEquals('0810 CST', $flight->dpt_time);
$this->assertEquals('1235 EST', $flight->arr_time);
$this->assertEquals('350', $flight->level);
$this->assertEquals('1477', $flight->distance);
$this->assertEquals('207', $flight->flight_time);
$this->assertEquals(FlightType::PASSENGER, $flight->flight_type);
$this->assertEquals('ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6', $flight->route);
$this->assertEquals('Just a flight', $flight->notes);
$this->assertEquals(true, $flight->active);
// Check the custom fields entered
$fields = \App\Models\FlightFieldValue::where([
'flight_id' => $flight->id,
])->get();
$this->assertCount(2, $fields);
$dep_gate = $fields->where('name', 'Departure Gate')->first();
$this->assertEquals('4', $dep_gate['value']);
$dep_gate = $fields->where('name', 'Arrival Gate')->first();
$this->assertEquals('C41', $dep_gate['value']);
// Check the fare class
$fares = $this->fareSvc->getForFlight($flight);
$this->assertCount(2, $fares);
$first = $fares->where('code', 'Y')->first();
$this->assertEquals(300, $first->price);
$this->assertEquals(100, $first->cost);
$this->assertEquals(130, $first->capacity);
$first = $fares->where('code', 'F')->first();
$this->assertEquals(600, $first->price);
$this->assertEquals(400, $first->cost);
$this->assertEquals(10, $first->capacity);
// Check the subfleets
$subfleets = $flight->subfleets;
$this->assertCount(1, $subfleets);
}
/**
*
* @throws \League\Csv\Exception
*/
public function testAircraftImporter()
{
$subfleet = factory(App\Models\Subfleet::class)->create(['type' => 'A32X']);
$file_path = base_path('tests/data/aircraft.csv');
$this->importSvc->importAircraft($file_path);
// See if it imported
$aircraft = \App\Models\Aircraft::where([
'registration' => 'N309US',
])->first();
$this->assertNotNull($aircraft);
$this->assertEquals($subfleet->id, $aircraft->id);
$this->assertEquals('A320-211', $aircraft->name);
$this->assertEquals('N309US', $aircraft->registration);
}
/**
*
* @throws \League\Csv\Exception
*/
public function testAirportImporter()
{
$file_path = base_path('tests/data/airports.csv');
$this->importSvc->importAirports($file_path);
// See if it imported
$airport = \App\Models\Airport::where([
'id' => 'KAUS',
])->first();
$this->assertNotNull($airport);
$this->assertEquals('KAUS', $airport->id);
$this->assertEquals('AUS', $airport->iata);
$this->assertEquals('KAUS', $airport->icao);
}
/**
* Test importing the subfleets
* @throws \League\Csv\Exception
*/
public function testSubfleetImporter(): void
{
$airline = factory(App\Models\Airline::class)->create(['icao' => 'VMS']);
$file_path = base_path('tests/data/subfleets.csv');
$this->importSvc->importSubfleets($file_path);
// See if it imported
$subfleet = \App\Models\Subfleet::where([
'type' => 'A32X',
])->first();
$this->assertNotNull($subfleet);
$this->assertEquals($airline->id, $subfleet->id);
$this->assertEquals('A32X', $subfleet->type);
$this->assertEquals('Airbus A320', $subfleet->name);
}
}

View File

@@ -92,6 +92,24 @@ class TestCase extends Illuminate\Foundation\Testing\TestCase
}
}
/**
* So we can test private/protected methods
* http://bit.ly/1mr5hMq
* @param $object
* @param $methodName
* @param array $parameters
* @return mixed
* @throws ReflectionException
*/
public function invokeMethod(&$object, $methodName, array $parameters = [])
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
/**
* Override the GET call to inject the user API key
* @param string $uri

2
tests/data/aircraft.csv Normal file
View File

@@ -0,0 +1,2 @@
subfleet,name,registration,hex_code,status
A32X,A320-211,N309US,,
1 subfleet name registration hex_code status
2 A32X A320-211 N309US

2
tests/data/airports.csv Normal file
View File

@@ -0,0 +1,2 @@
iata,icao,name,location,country,timezone,hub,lat,lon
AUS,KAUS,Austin-Bergstrom,"Austin, Texas, USA", United States,America/Chicago,1,30.1945,-97.6699
1 iata icao name location country timezone hub lat lon
2 AUS KAUS Austin-Bergstrom Austin, Texas, USA United States America/Chicago 1 30.1945 -97.6699

2
tests/data/flights.csv Normal file
View File

@@ -0,0 +1,2 @@
airline,flight_number,route_code,route_leg,dpt_airport,arr_airport,alt_airport,days,dpt_time,arr_time,level,distance,flight_time,flight_type,route,notes,active,subfleets,fares,fields
VMS,1972,,,KAUS,KJFK,KLGA,,0810 CST,1235 EST,350,1477,207,P,ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400,Departure Gate=4;Arrival Gate=C41
1 airline flight_number route_code route_leg dpt_airport arr_airport alt_airport days dpt_time arr_time level distance flight_time flight_type route notes active subfleets fares fields
2 VMS 1972 KAUS KJFK KLGA 0810 CST 1235 EST 350 1477 207 P ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6 Just a flight 1 A32X Y?price=300&cost=100&capacity=130;F?price=600&cost=400 Departure Gate=4;Arrival Gate=C41

2
tests/data/subfleets.csv Normal file
View File

@@ -0,0 +1,2 @@
airline,type,name
VMS,A32X,Airbus A320
1 airline type name
2 VMS A32X Airbus A320