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
This commit is contained in:
Nabeel S 2020-01-16 14:23:23 -05:00 committed by GitHub
parent 6fcbd603ba
commit d03a77bd4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 9 deletions

View File

@ -4,6 +4,7 @@ namespace App\Services\ImportExport;
use App\Contracts\ImportExport; use App\Contracts\ImportExport;
use App\Models\Aircraft; use App\Models\Aircraft;
use App\Models\Airline;
use App\Models\Enums\AircraftState; use App\Models\Enums\AircraftState;
use App\Models\Enums\AircraftStatus; use App\Models\Enums\AircraftStatus;
use App\Models\Subfleet; 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 * @param $type
* *
@ -40,11 +42,12 @@ class AircraftImporter extends ImportExport
*/ */
protected function getSubfleet($type) protected function getSubfleet($type)
{ {
$subfleet = Subfleet::firstOrCreate([ return Subfleet::firstOrCreate([
'type' => $type, 'type' => $type,
], ['name' => $type]); ], [
'name' => $type,
return $subfleet; 'airline_id' => Airline::where('active', true)->first()->id,
]);
} }
/** /**

View File

@ -15,7 +15,7 @@
{{ $subfleet->name }} {{ $subfleet->name }}
</a> </a>
</td> </td>
<td>{{ $subfleet->airline->name }}</td> <td>{{ optional($subfleet->airline)->name }}</td>
<td>{{ $subfleet->type }}</td> <td>{{ $subfleet->type }}</td>
<td>{{ $subfleet->aircraft->count() }}</td> <td>{{ $subfleet->aircraft->count() }}</td>
<td class="text-right"> <td class="text-right">

View File

@ -564,7 +564,8 @@ class ImporterTest extends TestCase
*/ */
public function testAircraftImporter(): 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'); $file_path = base_path('tests/data/aircraft.csv');
$status = $this->importSvc->importAircraft($file_path); $status = $this->importSvc->importAircraft($file_path);
@ -579,8 +580,9 @@ class ImporterTest extends TestCase
$this->assertNotNull($aircraft); $this->assertNotNull($aircraft);
$this->assertNotNull($aircraft->hex_code); $this->assertNotNull($aircraft->hex_code);
$this->assertEquals($subfleet->id, $aircraft->id); $this->assertNotNull($aircraft->subfleet);
$this->assertEquals($subfleet->type, $aircraft->subfleet->type); $this->assertNotNull($aircraft->subfleet->airline);
$this->assertEquals('A32X', $aircraft->subfleet->type);
$this->assertEquals('A320-211', $aircraft->name); $this->assertEquals('A320-211', $aircraft->name);
$this->assertEquals('N309US', $aircraft->registration); $this->assertEquals('N309US', $aircraft->registration);
$this->assertEquals(null, $aircraft->zfw); $this->assertEquals(null, $aircraft->zfw);