Merge pull request #325 from nabeelio/282-import-special-chars

Change exporter to use utf-8 close #282
This commit is contained in:
Nabeel S 2019-07-17 14:01:49 -04:00 committed by GitHub
commit a0a6ab027f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 38 deletions

View File

@ -29,7 +29,7 @@ class ExportService extends Service
public function openCsv($path): Writer public function openCsv($path): Writer
{ {
$writer = Writer::createFromPath($path, 'w+'); $writer = Writer::createFromPath($path, 'w+');
CharsetConverter::addTo($writer, 'utf-8', 'iso-8859-15'); CharsetConverter::addTo($writer, 'utf-8', 'utf-8');
return $writer; return $writer;
} }

View File

@ -1,7 +1,22 @@
<?php <?php
use App\Contracts\ImportExport;
use App\Models\Aircraft;
use App\Models\Airport;
use App\Models\Enums\Days;
use App\Models\Enums\ExpenseType;
use App\Models\Enums\FlightType; use App\Models\Enums\FlightType;
use App\Models\Expense;
use App\Models\Fare;
use App\Models\Flight;
use App\Models\FlightFieldValue;
use App\Models\Subfleet;
use App\Services\ExportService;
use App\Services\FareService; use App\Services\FareService;
use App\Services\ImportExport\AircraftExporter;
use App\Services\ImportExport\AirportExporter;
use App\Services\ImportExport\FlightExporter;
use App\Services\ImportService;
/** /**
* Class ImporterTest * Class ImporterTest
@ -15,9 +30,9 @@ class ImporterTest extends TestCase
public function setUp(): void public function setUp(): void
{ {
parent::setUp(); parent::setUp();
$this->importBaseClass = new \App\Contracts\ImportExport(); $this->importBaseClass = new ImportExport();
$this->importSvc = app(\App\Services\ImportService::class); $this->importSvc = app(ImportService::class);
$this->fareSvc = app(\App\Services\FareService::class); $this->fareSvc = app(FareService::class);
Storage::fake('local'); Storage::fake('local');
} }
@ -235,7 +250,7 @@ class ImporterTest extends TestCase
{ {
$aircraft = factory(App\Models\Aircraft::class)->create(); $aircraft = factory(App\Models\Aircraft::class)->create();
$exporter = new \App\Services\ImportExport\AircraftExporter(); $exporter = new AircraftExporter();
$exported = $exporter->export($aircraft); $exported = $exporter->export($aircraft);
$this->assertEquals($aircraft->iata, $exported['iata']); $this->assertEquals($aircraft->iata, $exported['iata']);
@ -244,8 +259,8 @@ class ImporterTest extends TestCase
$this->assertEquals($aircraft->zfw, $exported['zfw']); $this->assertEquals($aircraft->zfw, $exported['zfw']);
$this->assertEquals($aircraft->subfleet->type, $exported['subfleet']); $this->assertEquals($aircraft->subfleet->type, $exported['subfleet']);
$importer = app(\App\Services\ImportService::class); $importer = app(ImportService::class);
$exporter = app(\App\Services\ExportService::class); $exporter = app(ExportService::class);
$file = $exporter->exportAircraft(collect([$aircraft])); $file = $exporter->exportAircraft(collect([$aircraft]));
$status = $importer->importAircraft($file); $status = $importer->importAircraft($file);
$this->assertCount(1, $status['success']); $this->assertCount(1, $status['success']);
@ -257,18 +272,24 @@ class ImporterTest extends TestCase
*/ */
public function testAirportExporter(): void public function testAirportExporter(): void
{ {
$airport = factory(App\Models\Airport::class)->create(); $airport_name = 'Adolfo Suárez MadridBarajas Airport';
$exporter = new \App\Services\ImportExport\AirportExporter();
$airport = factory(App\Models\Airport::class)->create([
'name' => $airport_name,
]);
$exporter = new AirportExporter();
$exported = $exporter->export($airport); $exported = $exporter->export($airport);
$this->assertEquals($airport->iata, $exported['iata']); $this->assertEquals($airport->iata, $exported['iata']);
$this->assertEquals($airport->icao, $exported['icao']); $this->assertEquals($airport->icao, $exported['icao']);
$this->assertEquals($airport->name, $exported['name']); $this->assertEquals($airport->name, $exported['name']);
$importer = app(\App\Services\ImportService::class); $importer = app(ImportService::class);
$exporter = app(\App\Services\ExportService::class); $exporter = app(ExportService::class);
$file = $exporter->exportAirports(collect([$airport])); $file = $exporter->exportAirports(collect([$airport]));
$status = $importer->importAirports($file); $status = $importer->importAirports($file);
$this->assertCount(1, $status['success']); $this->assertCount(1, $status['success']);
$this->assertCount(0, $status['errors']); $this->assertCount(0, $status['errors']);
} }
@ -283,15 +304,15 @@ class ImporterTest extends TestCase
[$airline, $subfleet] = $this->insertFlightsScaffoldData(); [$airline, $subfleet] = $this->insertFlightsScaffoldData();
$subfleet2 = factory(App\Models\Subfleet::class)->create(['type' => 'B74X']); $subfleet2 = factory(App\Models\Subfleet::class)->create(['type' => 'B74X']);
$fareY = \App\Models\Fare::where('code', 'Y')->first(); $fareY = Fare::where('code', 'Y')->first();
$fareF = \App\Models\Fare::where('code', 'F')->first(); $fareF = Fare::where('code', 'F')->first();
$flight = factory(App\Models\Flight::class)->create([ $flight = factory(App\Models\Flight::class)->create([
'airline_id' => $airline->id, 'airline_id' => $airline->id,
'flight_type' => 'J', 'flight_type' => 'J',
'days' => \App\Models\Enums\Days::getDaysMask([ 'days' => Days::getDaysMask([
\App\Models\Enums\Days::TUESDAY, Days::TUESDAY,
\App\Models\Enums\Days::SUNDAY, Days::SUNDAY,
]), ]),
]); ]);
@ -302,13 +323,13 @@ class ImporterTest extends TestCase
$fareSvc->setForFlight($flight, $fareF); $fareSvc->setForFlight($flight, $fareF);
// Add some custom fields // Add some custom fields
\App\Models\FlightFieldValue::create([ FlightFieldValue::create([
'flight_id' => $flight->id, 'flight_id' => $flight->id,
'name' => 'Departure Gate', 'name' => 'Departure Gate',
'value' => '4', 'value' => '4',
]); ]);
\App\Models\FlightFieldValue::create([ FlightFieldValue::create([
'flight_id' => $flight->id, 'flight_id' => $flight->id,
'name' => 'Arrival Gate', 'name' => 'Arrival Gate',
'value' => 'C41', 'value' => 'C41',
@ -316,7 +337,7 @@ class ImporterTest extends TestCase
// Test the conversion // Test the conversion
$exporter = new \App\Services\ImportExport\FlightExporter(); $exporter = new FlightExporter();
$exported = $exporter->export($flight); $exported = $exporter->export($flight);
$this->assertEquals('27', $exported['days']); $this->assertEquals('27', $exported['days']);
@ -327,8 +348,8 @@ class ImporterTest extends TestCase
$this->assertEquals('Y?capacity=100;F', $exported['fares']); $this->assertEquals('Y?capacity=100;F', $exported['fares']);
$this->assertEquals('Departure Gate=4;Arrival Gate=C41', $exported['fields']); $this->assertEquals('Departure Gate=4;Arrival Gate=C41', $exported['fields']);
$importer = app(\App\Services\ImportService::class); $importer = app(ImportService::class);
$exporter = app(\App\Services\ExportService::class); $exporter = app(ExportService::class);
$file = $exporter->exportFlights(collect([$flight])); $file = $exporter->exportFlights(collect([$flight]));
$status = $importer->importFlights($file); $status = $importer->importFlights($file);
$this->assertCount(1, $status['success']); $this->assertCount(1, $status['success']);
@ -378,7 +399,7 @@ class ImporterTest extends TestCase
$this->assertCount(8, $status['success']); $this->assertCount(8, $status['success']);
$this->assertCount(0, $status['errors']); $this->assertCount(0, $status['errors']);
$expenses = \App\Models\Expense::all(); $expenses = Expense::all();
$on_airline = $expenses->where('name', 'Per-Flight (multiplier, on airline)')->first(); $on_airline = $expenses->where('name', 'Per-Flight (multiplier, on airline)')->first();
$this->assertEquals(200, $on_airline->amount); $this->assertEquals(200, $on_airline->amount);
@ -386,16 +407,16 @@ class ImporterTest extends TestCase
$pf = $expenses->where('name', 'Per-Flight (no muliplier)')->first(); $pf = $expenses->where('name', 'Per-Flight (no muliplier)')->first();
$this->assertEquals(100, $pf->amount); $this->assertEquals(100, $pf->amount);
$this->assertEquals(\App\Models\Enums\ExpenseType::FLIGHT, $pf->type); $this->assertEquals(ExpenseType::FLIGHT, $pf->type);
$catering = $expenses->where('name', 'Catering Staff')->first(); $catering = $expenses->where('name', 'Catering Staff')->first();
$this->assertEquals(1000, $catering->amount); $this->assertEquals(1000, $catering->amount);
$this->assertEquals(\App\Models\Enums\ExpenseType::DAILY, $catering->type); $this->assertEquals(ExpenseType::DAILY, $catering->type);
$this->assertEquals(\App\Models\Subfleet::class, $catering->ref_model); $this->assertEquals(Subfleet::class, $catering->ref_model);
$this->assertEquals($subfleet->id, $catering->ref_model_id); $this->assertEquals($subfleet->id, $catering->ref_model_id);
$mnt = $expenses->where('name', 'Maintenance')->first(); $mnt = $expenses->where('name', 'Maintenance')->first();
$this->assertEquals(\App\Models\Aircraft::class, $mnt->ref_model); $this->assertEquals(Aircraft::class, $mnt->ref_model);
$this->assertEquals($aircraft->id, $mnt->ref_model_id); $this->assertEquals($aircraft->id, $mnt->ref_model_id);
} }
@ -410,7 +431,7 @@ class ImporterTest extends TestCase
$this->assertCount(3, $status['success']); $this->assertCount(3, $status['success']);
$this->assertCount(0, $status['errors']); $this->assertCount(0, $status['errors']);
$fares = \App\Models\Fare::all(); $fares = Fare::all();
$y_class = $fares->where('code', 'Y')->first(); $y_class = $fares->where('code', 'Y')->first();
$this->assertEquals('Economy', $y_class->name); $this->assertEquals('Economy', $y_class->name);
@ -453,7 +474,7 @@ class ImporterTest extends TestCase
$this->assertCount(1, $status['errors']); $this->assertCount(1, $status['errors']);
// See if it imported // See if it imported
$flight = \App\Models\Flight::where([ $flight = Flight::where([
'airline_id' => $airline->id, 'airline_id' => $airline->id,
'flight_number' => '1972', 'flight_number' => '1972',
])->first(); ])->first();
@ -474,12 +495,12 @@ class ImporterTest extends TestCase
$this->assertEquals(true, $flight->active); $this->assertEquals(true, $flight->active);
// Test that the days were set properly // Test that the days were set properly
$this->assertTrue($flight->on_day(\App\Models\Enums\Days::MONDAY)); $this->assertTrue($flight->on_day(Days::MONDAY));
$this->assertTrue($flight->on_day(\App\Models\Enums\Days::FRIDAY)); $this->assertTrue($flight->on_day(Days::FRIDAY));
$this->assertFalse($flight->on_day(\App\Models\Enums\Days::TUESDAY)); $this->assertFalse($flight->on_day(Days::TUESDAY));
// Check the custom fields entered // Check the custom fields entered
$fields = \App\Models\FlightFieldValue::where([ $fields = FlightFieldValue::where([
'flight_id' => $flight->id, 'flight_id' => $flight->id,
])->get(); ])->get();
@ -525,7 +546,7 @@ class ImporterTest extends TestCase
$this->assertCount(0, $status['errors']); $this->assertCount(0, $status['errors']);
// See if it imported // See if it imported
$flight = \App\Models\Flight::where([ $flight = Flight::where([
'airline_id' => $airline->id, 'airline_id' => $airline->id,
'flight_number' => '1972', 'flight_number' => '1972',
])->first(); ])->first();
@ -533,7 +554,7 @@ class ImporterTest extends TestCase
$this->assertNotNull($flight); $this->assertNotNull($flight);
// Check the custom fields entered // Check the custom fields entered
$fields = \App\Models\FlightFieldValue::where([ $fields = FlightFieldValue::where([
'flight_id' => $flight->id, 'flight_id' => $flight->id,
])->get(); ])->get();
@ -554,7 +575,7 @@ class ImporterTest extends TestCase
$this->assertCount(1, $status['errors']); $this->assertCount(1, $status['errors']);
// See if it imported // See if it imported
$aircraft = \App\Models\Aircraft::where([ $aircraft = Aircraft::where([
'registration' => 'N309US', 'registration' => 'N309US',
])->first(); ])->first();
@ -580,7 +601,7 @@ class ImporterTest extends TestCase
$this->assertCount(1, $status['errors']); $this->assertCount(1, $status['errors']);
// See if it imported // See if it imported
$airport = \App\Models\Airport::where([ $airport = Airport::where([
'id' => 'KAUS', 'id' => 'KAUS',
])->first(); ])->first();
@ -597,7 +618,7 @@ class ImporterTest extends TestCase
$this->assertEquals('-97.6699', $airport->lon); $this->assertEquals('-97.6699', $airport->lon);
// See if it imported // See if it imported
$airport = \App\Models\Airport::where([ $airport = Airport::where([
'id' => 'KSFO', 'id' => 'KSFO',
])->first(); ])->first();
@ -623,7 +644,7 @@ class ImporterTest extends TestCase
$this->assertCount(1, $status['errors']); $this->assertCount(1, $status['errors']);
// See if it imported // See if it imported
$subfleet = \App\Models\Subfleet::where([ $subfleet = Subfleet::where([
'type' => 'A32X', 'type' => 'A32X',
])->first(); ])->first();
@ -653,4 +674,18 @@ class ImporterTest extends TestCase
$this->assertEquals(100, $busi->pivot->capacity); $this->assertEquals(100, $busi->pivot->capacity);
$this->assertEquals(null, $busi->pivot->cost); $this->assertEquals(null, $busi->pivot->cost);
} }
public function testAirportSpecialCharsImporter(): void
{
$file_path = base_path('tests/data/airports_special_chars.csv');
$status = $this->importSvc->importAirports($file_path);
// See if it imported
$airport = Airport::where([
'id' => 'LEMD',
])->first();
$this->assertNotNull($airport);
$this->assertEquals('Adolfo Suárez MadridBarajas Airport', $airport->name);
}
} }

View File

@ -0,0 +1,10 @@
icao,iata,name,location,country,timezone,hub,lat,lon,ground_handling_cost,fuel_100ll_cost,fuel_jeta_cost,fuel_mogas_cost
EGLL,LHR,"London Heathrow","London, England",,Europe/London,,51.4775,-0.4614,500,0,0,0
KAUS,AUS,Austin-Bergstrom,"Austin, Texas, USA","United States",America/Chicago,1,30.1945,-97.6699,0,0,0,0
KJFK,JFK,"John F Kennedy","New York, New York, USA","United States",America/New_York,1,40.6399,-73.7787,250,0,0,0
KPAE,PAE,"Snohomish County (Paine Field) Airport",Everett,"United States",America/Los_Angeles,,47.9063,-122.282,0,0,0,0
KSEA,SEA,"Seattle Tacoma International Airport",Seattle,"United States",America/Los_Angeles,,47.449,-122.309,0,0,0,0
LEMD,MAD,"Adolfo Suárez MadridBarajas Airport",Madrid,Spain,Europe/Madrid,,40.4719,-3.5626,,0,0,0
MKJP,KIN,"Norman Manley International Airport","Kingston, Jamaica",,America/Jamaica,,17.9357,-76.7875,50,0,0,0
MWCR,GCM,"Owen Roberts International Airport",Georgetown,Cayman,America/Cayman,,19.2928,-81.3577,50,0,0,0
OMDB,DXB,"Dubai International Airport","Dubai, UAE",,Asia/Dubai,,25.2528,55.3644,50,0,0,0
1 icao iata name location country timezone hub lat lon ground_handling_cost fuel_100ll_cost fuel_jeta_cost fuel_mogas_cost
2 EGLL LHR London Heathrow London, England Europe/London 51.4775 -0.4614 500 0 0 0
3 KAUS AUS Austin-Bergstrom Austin, Texas, USA United States America/Chicago 1 30.1945 -97.6699 0 0 0 0
4 KJFK JFK John F Kennedy New York, New York, USA United States America/New_York 1 40.6399 -73.7787 250 0 0 0
5 KPAE PAE Snohomish County (Paine Field) Airport Everett United States America/Los_Angeles 47.9063 -122.282 0 0 0 0
6 KSEA SEA Seattle Tacoma International Airport Seattle United States America/Los_Angeles 47.449 -122.309 0 0 0 0
7 LEMD MAD Adolfo Suárez Madrid–Barajas Airport Madrid Spain Europe/Madrid 40.4719 -3.5626 0 0 0
8 MKJP KIN Norman Manley International Airport Kingston, Jamaica America/Jamaica 17.9357 -76.7875 50 0 0 0
9 MWCR GCM Owen Roberts International Airport Georgetown Cayman America/Cayman 19.2928 -81.3577 50 0 0 0
10 OMDB DXB Dubai International Airport Dubai, UAE Asia/Dubai 25.2528 55.3644 50 0 0 0