From f2952c656718ba660a108f20d19be85963f75a5c Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Fri, 30 Mar 2018 18:53:43 -0500 Subject: [PATCH] Fix blank/empty field being returned as invalid object #222 --- app/Interfaces/ImportExport.php | 4 +++ tests/ImporterTest.php | 38 +++++++++++++++++++++++++++++ tests/data/flights_empty_fields.csv | 2 ++ 3 files changed, 44 insertions(+) create mode 100644 tests/data/flights_empty_fields.csv diff --git a/app/Interfaces/ImportExport.php b/app/Interfaces/ImportExport.php index e11dfd5b..f1961f46 100644 --- a/app/Interfaces/ImportExport.php +++ b/app/Interfaces/ImportExport.php @@ -129,6 +129,10 @@ class ImportExport # No multiple values in here, just a straight value if (\count($split_values) === 1) { + if(trim($split_values[0]) === '') { + return []; + } + return [$split_values[0]]; } diff --git a/tests/ImporterTest.php b/tests/ImporterTest.php index edf66afe..3c8f80b6 100644 --- a/tests/ImporterTest.php +++ b/tests/ImporterTest.php @@ -57,6 +57,10 @@ class ImporterTest extends TestCase public function testConvertStringtoObjects(): void { $tests = [ + [ + 'input' => '', + 'expected' => [], + ], [ 'input' => 'gate', 'expected' => ['gate'] @@ -139,6 +143,10 @@ class ImporterTest extends TestCase public function testConvertObjectToString(): void { $tests = [ + [ + 'input' => '', + 'expected' => '' + ], [ 'input' => ['gate'], 'expected' => 'gate', @@ -440,6 +448,36 @@ class ImporterTest extends TestCase $this->assertCount(1, $subfleets); } + /** + * Test the flight importer + * @throws \Illuminate\Validation\ValidationException + */ + public function testFlightImporterEmptyCustomFields(): void + { + [$airline, $subfleet] = $this->insertFlightsScaffoldData(); + + $file_path = base_path('tests/data/flights_empty_fields.csv'); + $status = $this->importSvc->importFlights($file_path); + + $this->assertCount(1, $status['success']); + $this->assertCount(0, $status['errors']); + + // See if it imported + $flight = \App\Models\Flight::where([ + 'airline_id' => $airline->id, + 'flight_number' => '1972' + ])->first(); + + $this->assertNotNull($flight); + + // Check the custom fields entered + $fields = \App\Models\FlightFieldValue::where([ + 'flight_id' => $flight->id, + ])->get(); + + $this->assertCount(0, $fields); + } + /** * @throws \Illuminate\Validation\ValidationException */ diff --git a/tests/data/flights_empty_fields.csv b/tests/data/flights_empty_fields.csv new file mode 100644 index 00000000..df70414a --- /dev/null +++ b/tests/data/flights_empty_fields.csv @@ -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,15,0810 CST,1235 EST,350,1477,207,J,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;B?,