From d03a77bd4b687a55e43c4353fcd499e9eaa47ec5 Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Thu, 16 Jan 2020 14:23:23 -0500 Subject: [PATCH] New subfleet not being attached to an airline on import #479 (#505) * Fix subfleet not being attached to an airline on creation in import #479 * Call airline name with optional() around subfleet * Minor cleanup --- app/Services/ImportExport/AircraftImporter.php | 13 ++++++++----- resources/views/admin/subfleets/table.blade.php | 2 +- tests/ImporterTest.php | 8 +++++--- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/Services/ImportExport/AircraftImporter.php b/app/Services/ImportExport/AircraftImporter.php index 95d62e028..d062483c9 100644 --- a/app/Services/ImportExport/AircraftImporter.php +++ b/app/Services/ImportExport/AircraftImporter.php @@ -4,6 +4,7 @@ use App\Contracts\ImportExport; use App\Models\Aircraft; +use App\Models\Airline; use App\Models\Enums\AircraftState; use App\Models\Enums\AircraftStatus; use App\Models\Subfleet; @@ -32,7 +33,8 @@ class AircraftImporter extends ImportExport ]; /** - * Find the subfleet specified, or just create it on the fly + * Find the subfleet specified, or just create it on the fly and attach it to the + * first airline that's been found * * @param $type * @@ -40,11 +42,12 @@ class AircraftImporter extends ImportExport */ protected function getSubfleet($type) { - $subfleet = Subfleet::firstOrCreate([ + return Subfleet::firstOrCreate([ 'type' => $type, - ], ['name' => $type]); - - return $subfleet; + ], [ + 'name' => $type, + 'airline_id' => Airline::where('active', true)->first()->id, + ]); } /** diff --git a/resources/views/admin/subfleets/table.blade.php b/resources/views/admin/subfleets/table.blade.php index ccb49a809..b4bb41a76 100644 --- a/resources/views/admin/subfleets/table.blade.php +++ b/resources/views/admin/subfleets/table.blade.php @@ -15,7 +15,7 @@ {{ $subfleet->name }} - {{ $subfleet->airline->name }} + {{ optional($subfleet->airline)->name }} {{ $subfleet->type }} {{ $subfleet->aircraft->count() }} diff --git a/tests/ImporterTest.php b/tests/ImporterTest.php index 3c8febb00..e7e5ff3c8 100644 --- a/tests/ImporterTest.php +++ b/tests/ImporterTest.php @@ -564,7 +564,8 @@ public function testFlightImporterEmptyCustomFields(): void */ public function testAircraftImporter(): void { - $subfleet = factory(App\Models\Subfleet::class)->create(['type' => 'A32X']); + factory(App\Models\Airline::class)->create(); + // $subfleet = factory(App\Models\Subfleet::class)->create(['type' => 'A32X']); $file_path = base_path('tests/data/aircraft.csv'); $status = $this->importSvc->importAircraft($file_path); @@ -579,8 +580,9 @@ public function testAircraftImporter(): void $this->assertNotNull($aircraft); $this->assertNotNull($aircraft->hex_code); - $this->assertEquals($subfleet->id, $aircraft->id); - $this->assertEquals($subfleet->type, $aircraft->subfleet->type); + $this->assertNotNull($aircraft->subfleet); + $this->assertNotNull($aircraft->subfleet->airline); + $this->assertEquals('A32X', $aircraft->subfleet->type); $this->assertEquals('A320-211', $aircraft->name); $this->assertEquals('N309US', $aircraft->registration); $this->assertEquals(null, $aircraft->zfw);