Skip to content

Commit

Permalink
New subfleet not being attached to an airline on import #479 (#505)
Browse files Browse the repository at this point in the history
* Fix subfleet not being attached to an airline on creation in import #479

* Call airline name with optional() around subfleet

* Minor cleanup
  • Loading branch information
nabeelio authored Jan 16, 2020
1 parent 6fcbd60 commit d03a77b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
13 changes: 8 additions & 5 deletions app/Services/ImportExport/AircraftImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,19 +33,21 @@ 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
*
* @return Subfleet|\Illuminate\Database\Eloquent\Model|null|object|static
*/
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,
]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion resources/views/admin/subfleets/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{{ $subfleet->name }}
</a>
</td>
<td>{{ $subfleet->airline->name }}</td>
<td>{{ optional($subfleet->airline)->name }}</td>
<td>{{ $subfleet->type }}</td>
<td>{{ $subfleet->aircraft->count() }}</td>
<td class="text-right">
Expand Down
8 changes: 5 additions & 3 deletions tests/ImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit d03a77b

Please sign in to comment.