Skip to content

Commit

Permalink
Set airport and landing time of aircraft after pirep accept #112
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Jan 10, 2018
1 parent 2f33c7b commit dce9723
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use \App\Models\Enums\AircraftState;

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

Expand All @@ -12,14 +14,17 @@ public function up()
$table->unsignedInteger('subfleet_id');
$table->string('icao', 4)->nullable();
$table->string('airport_id', 5)->nullable();
$table->timestamp('landing_time')->nullable();
$table->string('hex_code', 10)->nullable();
$table->string('name', 50);
$table->string('registration', 10)->nullable();
$table->string('tail_number', 10)->nullable();
$table->boolean('active')->default(true);
$table->unsignedTinyInteger('state')->default(AircraftState::PARKED);
$table->timestamps();

$table->unique('registration');
$table->index('airport_id');
});

}
Expand Down
20 changes: 20 additions & 0 deletions app/Models/Enums/AircraftState.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Models\Enums;

/**
* Class AircraftState
* @package App\Models\Enums
*/
class AircraftState extends EnumBase
{
const PARKED = 0;
const IN_USE = 1;
const IN_AIR = 2;

public static $labels = [
AircraftState::PARKED => 'On Ground',
AircraftState::IN_USE => 'In Use',
AircraftState::IN_AIR => 'In Air',
];
}
5 changes: 5 additions & 0 deletions app/Services/PIREPService.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ public function accept(Pirep $pirep): Pirep

Log::info('PIREP ' . $pirep->id . ' state change to ACCEPTED');

# Update the aircraft
$pirep->aircraft->airport_id = $pirep->arr_airport_id;
$pirep->aircraft->landing_time = $pirep->updated_at;
$pirep->aircraft->save();

event(new PirepAccepted($pirep));

return $pirep;
Expand Down
9 changes: 9 additions & 0 deletions tests/PIREPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ protected function getAcarsRoute($pirep)
*/
public function testAddPirep()
{
$user = factory(App\Models\User::class)->create();
$route = $this->createNewRoute();
$pirep = factory(App\Models\Pirep::class)->create([
'user_id' => $user->id,
'route' => implode(' ', $route)
]);

Expand All @@ -73,6 +75,13 @@ public function testAddPirep()
$this->assertEquals($new_flight_time, $pirep->pilot->flight_time);
$this->assertEquals($pirep->arr_airport_id, $pirep->pilot->curr_airport_id);

# Check the location of the current aircraft
$this->assertEquals($pirep->aircraft->airport_id, $pirep->arr_airport_id);

# Also check via API:
$this->get('/api/fleet/aircraft/' . $pirep->aircraft_id, [], $user)
->assertJson(['airport_id' => $pirep->arr_airport_id]);

/**
* Now go from ACCEPTED to REJECTED
*/
Expand Down
13 changes: 8 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,17 @@ public function assertHasKeys($obj, $keys=[])
* Override the GET call to inject the user API key
* @param string $uri
* @param array $headers
* @param null $user
* @return \Illuminate\Foundation\Testing\TestResponse
*/
public function get($uri, array $headers=[]): \Illuminate\Foundation\Testing\TestResponse
public function get($uri, array $headers=[], $user=null): \Illuminate\Foundation\Testing\TestResponse
{
if(empty($headers)) {
if($this->user !== null) {
$headers = $this->headers($this->user);
}
if($this->user !== null) {
$headers = $this->headers($this->user);
}

if($user !== null) {
$headers['x-api-key'] = $user->api_key;
}

return parent::get($uri, $headers);
Expand Down

0 comments on commit dce9723

Please sign in to comment.