From 9475a737f0f1ede79f11e744ab015b4c507e2f50 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Wed, 17 Jul 2019 13:36:16 -0400 Subject: [PATCH] Change exporter to use utf-8 close #282 --- app/Services/ExportService.php | 2 +- tests/ImporterTest.php | 128 +++++++++++++++++--------- tests/data/airports_special_chars.csv | 10 ++ 3 files changed, 93 insertions(+), 47 deletions(-) create mode 100755 tests/data/airports_special_chars.csv diff --git a/app/Services/ExportService.php b/app/Services/ExportService.php index 0b253d99..87a5366d 100644 --- a/app/Services/ExportService.php +++ b/app/Services/ExportService.php @@ -29,7 +29,7 @@ class ExportService extends Service public function openCsv($path): Writer { $writer = Writer::createFromPath($path, 'w+'); - CharsetConverter::addTo($writer, 'utf-8', 'iso-8859-15'); + CharsetConverter::addTo($writer, 'utf-8', 'utf-8'); return $writer; } diff --git a/tests/ImporterTest.php b/tests/ImporterTest.php index 38a179a1..e1e02acb 100644 --- a/tests/ImporterTest.php +++ b/tests/ImporterTest.php @@ -1,7 +1,23 @@ importBaseClass = new \App\Contracts\ImportExport(); - $this->importSvc = app(\App\Services\ImportService::class); - $this->fareSvc = app(\App\Services\FareService::class); + $this->importBaseClass = new ImportExport(); + $this->importSvc = app(ImportService::class); + $this->fareSvc = app(FareService::class); Storage::fake('local'); } @@ -229,13 +245,13 @@ class ImporterTest extends TestCase /** * Test exporting all the flights to a file * - * @throws \Illuminate\Validation\ValidationException + * @throws ValidationException */ public function testAircraftExporter(): void { $aircraft = factory(App\Models\Aircraft::class)->create(); - $exporter = new \App\Services\ImportExport\AircraftExporter(); + $exporter = new AircraftExporter(); $exported = $exporter->export($aircraft); $this->assertEquals($aircraft->iata, $exported['iata']); @@ -244,8 +260,8 @@ class ImporterTest extends TestCase $this->assertEquals($aircraft->zfw, $exported['zfw']); $this->assertEquals($aircraft->subfleet->type, $exported['subfleet']); - $importer = app(\App\Services\ImportService::class); - $exporter = app(\App\Services\ExportService::class); + $importer = app(ImportService::class); + $exporter = app(ExportService::class); $file = $exporter->exportAircraft(collect([$aircraft])); $status = $importer->importAircraft($file); $this->assertCount(1, $status['success']); @@ -257,18 +273,24 @@ class ImporterTest extends TestCase */ public function testAirportExporter(): void { - $airport = factory(App\Models\Airport::class)->create(); - $exporter = new \App\Services\ImportExport\AirportExporter(); + $airport_name = 'Adolfo Suárez Madrid–Barajas Airport'; + + $airport = factory(App\Models\Airport::class)->create([ + 'name' => $airport_name, + ]); + + $exporter = new AirportExporter(); $exported = $exporter->export($airport); $this->assertEquals($airport->iata, $exported['iata']); $this->assertEquals($airport->icao, $exported['icao']); $this->assertEquals($airport->name, $exported['name']); - $importer = app(\App\Services\ImportService::class); - $exporter = app(\App\Services\ExportService::class); + $importer = app(ImportService::class); + $exporter = app(ExportService::class); $file = $exporter->exportAirports(collect([$airport])); $status = $importer->importAirports($file); + $this->assertCount(1, $status['success']); $this->assertCount(0, $status['errors']); } @@ -283,15 +305,15 @@ class ImporterTest extends TestCase [$airline, $subfleet] = $this->insertFlightsScaffoldData(); $subfleet2 = factory(App\Models\Subfleet::class)->create(['type' => 'B74X']); - $fareY = \App\Models\Fare::where('code', 'Y')->first(); - $fareF = \App\Models\Fare::where('code', 'F')->first(); + $fareY = Fare::where('code', 'Y')->first(); + $fareF = Fare::where('code', 'F')->first(); $flight = factory(App\Models\Flight::class)->create([ 'airline_id' => $airline->id, 'flight_type' => 'J', - 'days' => \App\Models\Enums\Days::getDaysMask([ - \App\Models\Enums\Days::TUESDAY, - \App\Models\Enums\Days::SUNDAY, + 'days' => Days::getDaysMask([ + Days::TUESDAY, + Days::SUNDAY, ]), ]); @@ -302,13 +324,13 @@ class ImporterTest extends TestCase $fareSvc->setForFlight($flight, $fareF); // Add some custom fields - \App\Models\FlightFieldValue::create([ + FlightFieldValue::create([ 'flight_id' => $flight->id, 'name' => 'Departure Gate', 'value' => '4', ]); - \App\Models\FlightFieldValue::create([ + FlightFieldValue::create([ 'flight_id' => $flight->id, 'name' => 'Arrival Gate', 'value' => 'C41', @@ -316,7 +338,7 @@ class ImporterTest extends TestCase // Test the conversion - $exporter = new \App\Services\ImportExport\FlightExporter(); + $exporter = new FlightExporter(); $exported = $exporter->export($flight); $this->assertEquals('27', $exported['days']); @@ -327,8 +349,8 @@ class ImporterTest extends TestCase $this->assertEquals('Y?capacity=100;F', $exported['fares']); $this->assertEquals('Departure Gate=4;Arrival Gate=C41', $exported['fields']); - $importer = app(\App\Services\ImportService::class); - $exporter = app(\App\Services\ExportService::class); + $importer = app(ImportService::class); + $exporter = app(ExportService::class); $file = $exporter->exportFlights(collect([$flight])); $status = $importer->importFlights($file); $this->assertCount(1, $status['success']); @@ -338,7 +360,7 @@ class ImporterTest extends TestCase /** * Try importing the aicraft in the airports. Should fail * - * @expectedException \Illuminate\Validation\ValidationException + * @expectedException ValidationException */ public function testInvalidFileImport(): void { @@ -361,7 +383,7 @@ class ImporterTest extends TestCase /** * Test the importing of expenses * - * @throws \Illuminate\Validation\ValidationException + * @throws ValidationException */ public function testExpenseImporter(): void { @@ -378,7 +400,7 @@ class ImporterTest extends TestCase $this->assertCount(8, $status['success']); $this->assertCount(0, $status['errors']); - $expenses = \App\Models\Expense::all(); + $expenses = Expense::all(); $on_airline = $expenses->where('name', 'Per-Flight (multiplier, on airline)')->first(); $this->assertEquals(200, $on_airline->amount); @@ -386,21 +408,21 @@ class ImporterTest extends TestCase $pf = $expenses->where('name', 'Per-Flight (no muliplier)')->first(); $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(); $this->assertEquals(1000, $catering->amount); - $this->assertEquals(\App\Models\Enums\ExpenseType::DAILY, $catering->type); - $this->assertEquals(\App\Models\Subfleet::class, $catering->ref_model); + $this->assertEquals(ExpenseType::DAILY, $catering->type); + $this->assertEquals(Subfleet::class, $catering->ref_model); $this->assertEquals($subfleet->id, $catering->ref_model_id); $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); } /** - * @throws \Illuminate\Validation\ValidationException + * @throws ValidationException */ public function testFareImporter(): void { @@ -410,7 +432,7 @@ class ImporterTest extends TestCase $this->assertCount(3, $status['success']); $this->assertCount(0, $status['errors']); - $fares = \App\Models\Fare::all(); + $fares = Fare::all(); $y_class = $fares->where('code', 'Y')->first(); $this->assertEquals('Economy', $y_class->name); @@ -440,7 +462,7 @@ class ImporterTest extends TestCase /** * Test the flight importer * - * @throws \Illuminate\Validation\ValidationException + * @throws ValidationException */ public function testFlightImporter(): void { @@ -453,7 +475,7 @@ class ImporterTest extends TestCase $this->assertCount(1, $status['errors']); // See if it imported - $flight = \App\Models\Flight::where([ + $flight = Flight::where([ 'airline_id' => $airline->id, 'flight_number' => '1972', ])->first(); @@ -474,12 +496,12 @@ class ImporterTest extends TestCase $this->assertEquals(true, $flight->active); // Test that the days were set properly - $this->assertTrue($flight->on_day(\App\Models\Enums\Days::MONDAY)); - $this->assertTrue($flight->on_day(\App\Models\Enums\Days::FRIDAY)); - $this->assertFalse($flight->on_day(\App\Models\Enums\Days::TUESDAY)); + $this->assertTrue($flight->on_day(Days::MONDAY)); + $this->assertTrue($flight->on_day(Days::FRIDAY)); + $this->assertFalse($flight->on_day(Days::TUESDAY)); // Check the custom fields entered - $fields = \App\Models\FlightFieldValue::where([ + $fields = FlightFieldValue::where([ 'flight_id' => $flight->id, ])->get(); @@ -512,7 +534,7 @@ class ImporterTest extends TestCase /** * Test the flight importer * - * @throws \Illuminate\Validation\ValidationException + * @throws ValidationException */ public function testFlightImporterEmptyCustomFields(): void { @@ -525,7 +547,7 @@ class ImporterTest extends TestCase $this->assertCount(0, $status['errors']); // See if it imported - $flight = \App\Models\Flight::where([ + $flight = Flight::where([ 'airline_id' => $airline->id, 'flight_number' => '1972', ])->first(); @@ -533,7 +555,7 @@ class ImporterTest extends TestCase $this->assertNotNull($flight); // Check the custom fields entered - $fields = \App\Models\FlightFieldValue::where([ + $fields = FlightFieldValue::where([ 'flight_id' => $flight->id, ])->get(); @@ -541,7 +563,7 @@ class ImporterTest extends TestCase } /** - * @throws \Illuminate\Validation\ValidationException + * @throws ValidationException */ public function testAircraftImporter(): void { @@ -554,7 +576,7 @@ class ImporterTest extends TestCase $this->assertCount(1, $status['errors']); // See if it imported - $aircraft = \App\Models\Aircraft::where([ + $aircraft = Aircraft::where([ 'registration' => 'N309US', ])->first(); @@ -569,7 +591,7 @@ class ImporterTest extends TestCase } /** - * @throws \Illuminate\Validation\ValidationException + * @throws ValidationException */ public function testAirportImporter(): void { @@ -580,7 +602,7 @@ class ImporterTest extends TestCase $this->assertCount(1, $status['errors']); // See if it imported - $airport = \App\Models\Airport::where([ + $airport = Airport::where([ 'id' => 'KAUS', ])->first(); @@ -597,7 +619,7 @@ class ImporterTest extends TestCase $this->assertEquals('-97.6699', $airport->lon); // See if it imported - $airport = \App\Models\Airport::where([ + $airport = Airport::where([ 'id' => 'KSFO', ])->first(); @@ -608,7 +630,7 @@ class ImporterTest extends TestCase /** * Test importing the subfleets * - * @throws \Illuminate\Validation\ValidationException + * @throws ValidationException */ public function testSubfleetImporter(): void { @@ -623,7 +645,7 @@ class ImporterTest extends TestCase $this->assertCount(1, $status['errors']); // See if it imported - $subfleet = \App\Models\Subfleet::where([ + $subfleet = Subfleet::where([ 'type' => 'A32X', ])->first(); @@ -653,4 +675,18 @@ class ImporterTest extends TestCase $this->assertEquals(100, $busi->pivot->capacity); $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 Madrid–Barajas Airport', $airport->name); + } } diff --git a/tests/data/airports_special_chars.csv b/tests/data/airports_special_chars.csv new file mode 100755 index 00000000..33de991d --- /dev/null +++ b/tests/data/airports_special_chars.csv @@ -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 Madrid–Barajas 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