2017-06-09 12:00:30 +08:00
|
|
|
<?php
|
|
|
|
|
2018-01-03 03:17:22 +08:00
|
|
|
use Carbon\Carbon;
|
|
|
|
|
2018-01-02 03:48:02 +08:00
|
|
|
use App\Models\Acars;
|
|
|
|
use App\Models\Enums\AcarsType;
|
2017-07-04 14:05:23 +08:00
|
|
|
use App\Models\Pirep;
|
2018-01-02 03:48:02 +08:00
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Enums\PirepState;
|
2017-07-04 14:05:23 +08:00
|
|
|
|
2017-06-09 12:00:30 +08:00
|
|
|
class PIREPTest extends TestCase
|
|
|
|
{
|
2017-07-04 14:05:23 +08:00
|
|
|
protected $pirepSvc;
|
2017-07-05 02:57:08 +08:00
|
|
|
|
2017-07-04 14:05:23 +08:00
|
|
|
public function setUp()
|
2017-06-09 12:00:30 +08:00
|
|
|
{
|
2017-07-04 14:05:23 +08:00
|
|
|
parent::setUp(); // TODO: Change the autogenerated stub
|
|
|
|
$this->addData('base');
|
|
|
|
$this->pirepSvc = app('App\Services\PIREPService');
|
|
|
|
}
|
|
|
|
|
2018-01-02 03:48:02 +08:00
|
|
|
protected function createNewRoute()
|
|
|
|
{
|
|
|
|
$route = [];
|
|
|
|
$navpoints = factory(App\Models\Navdata::class, 5)->create();
|
|
|
|
foreach ($navpoints as $point) {
|
|
|
|
$route[] = $point->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $route;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getAcarsRoute($pirep)
|
|
|
|
{
|
|
|
|
$saved_route = [];
|
|
|
|
$route_points = Acars::where(
|
|
|
|
['pirep_id' => $pirep->id, 'type' => AcarsType::ROUTE]
|
2018-01-02 23:40:42 +08:00
|
|
|
)->orderBy('order', 'asc')->get();
|
2018-01-02 03:48:02 +08:00
|
|
|
|
|
|
|
foreach ($route_points as $point) {
|
|
|
|
$saved_route[] = $point->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $saved_route;
|
|
|
|
}
|
|
|
|
|
2017-07-05 02:57:08 +08:00
|
|
|
/**
|
|
|
|
*/
|
2017-07-04 14:05:23 +08:00
|
|
|
public function testAddPirep()
|
|
|
|
{
|
2018-01-02 03:48:02 +08:00
|
|
|
$route = $this->createNewRoute();
|
|
|
|
$pirep = factory(App\Models\Pirep::class)->create([
|
|
|
|
'route' => implode(' ', $route)
|
|
|
|
]);
|
|
|
|
|
2017-07-04 14:05:23 +08:00
|
|
|
$pirep = $this->pirepSvc->create($pirep, []);
|
2018-01-05 09:39:41 +08:00
|
|
|
$this->pirepSvc->saveRoute($pirep);
|
2017-07-04 14:05:23 +08:00
|
|
|
|
|
|
|
/**
|
2017-12-20 10:19:36 +08:00
|
|
|
* Check the initial state info
|
2017-07-04 14:05:23 +08:00
|
|
|
*/
|
2017-12-20 10:19:36 +08:00
|
|
|
$this->assertEquals($pirep->state, PirepState::PENDING);
|
2017-07-04 14:05:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Now set the PIREP state to ACCEPTED
|
|
|
|
*/
|
2017-12-14 00:56:26 +08:00
|
|
|
$new_pirep_count = $pirep->pilot->flights + 1;
|
|
|
|
$original_flight_time = $pirep->pilot->flight_time ;
|
|
|
|
$new_flight_time = $pirep->pilot->flight_time + $pirep->flight_time;
|
|
|
|
|
2017-12-20 10:19:36 +08:00
|
|
|
$this->pirepSvc->changeState($pirep, PirepState::ACCEPTED);
|
2017-12-14 00:56:26 +08:00
|
|
|
$this->assertEquals($new_pirep_count, $pirep->pilot->flights);
|
|
|
|
$this->assertEquals($new_flight_time, $pirep->pilot->flight_time);
|
|
|
|
$this->assertEquals($pirep->arr_airport_id, $pirep->pilot->curr_airport_id);
|
2017-07-04 14:05:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Now go from ACCEPTED to REJECTED
|
|
|
|
*/
|
2017-12-14 00:56:26 +08:00
|
|
|
$new_pirep_count = $pirep->pilot->flights - 1;
|
|
|
|
$new_flight_time = $pirep->pilot->flight_time - $pirep->flight_time;
|
2017-12-20 10:19:36 +08:00
|
|
|
$this->pirepSvc->changeState($pirep, PirepState::REJECTED);
|
2017-12-14 00:56:26 +08:00
|
|
|
$this->assertEquals($new_pirep_count, $pirep->pilot->flights);
|
|
|
|
$this->assertEquals($new_flight_time, $pirep->pilot->flight_time);
|
2017-07-05 02:57:08 +08:00
|
|
|
$this->assertEquals($pirep->arr_airport_id, $pirep->pilot->curr_airport_id);
|
2018-01-02 03:48:02 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the ACARS table
|
|
|
|
*/
|
|
|
|
$saved_route = $this->getAcarsRoute($pirep);
|
|
|
|
$this->assertEquals($route, $saved_route);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recreate the route with new options points. Make sure that the
|
|
|
|
* old route is erased from the ACARS table and then recreated
|
|
|
|
*/
|
|
|
|
$route = $this->createNewRoute();
|
|
|
|
$pirep->route = implode(' ', $route);
|
|
|
|
$pirep->save();
|
|
|
|
|
|
|
|
# this should delete the old route from the acars table
|
|
|
|
$this->pirepSvc->saveRoute($pirep);
|
|
|
|
|
|
|
|
$saved_route = $this->getAcarsRoute($pirep);
|
|
|
|
$this->assertEquals($route, $saved_route);
|
2017-07-05 02:57:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check the stats/ranks, etc have incremented properly
|
|
|
|
*/
|
|
|
|
public function testPilotStatsIncr()
|
|
|
|
{
|
2018-01-03 01:12:53 +08:00
|
|
|
$user = factory(User::class)->create([
|
|
|
|
'airline_id' => 1,
|
|
|
|
'flights' => 0,
|
|
|
|
'flight_time' => 0,
|
|
|
|
'rank_id' => 1,
|
|
|
|
]);
|
2017-12-14 00:56:26 +08:00
|
|
|
|
2017-07-05 02:57:08 +08:00
|
|
|
# Submit two PIREPs
|
2017-12-14 00:56:26 +08:00
|
|
|
$pireps = factory(Pirep::class, 2)->create([
|
2017-12-26 05:19:34 +08:00
|
|
|
'airline_id' => 1,
|
2018-01-03 01:12:53 +08:00
|
|
|
'user_id' => $user->id,
|
2017-12-14 00:56:26 +08:00
|
|
|
# 360min == 6 hours, rank should bump up
|
|
|
|
'flight_time' => 360,
|
|
|
|
]);
|
|
|
|
|
|
|
|
foreach ($pireps as $pirep) {
|
|
|
|
$this->pirepSvc->create($pirep);
|
|
|
|
$this->pirepSvc->accept($pirep);
|
|
|
|
}
|
|
|
|
|
2018-01-03 01:12:53 +08:00
|
|
|
$pilot = User::find($user->id);
|
2017-07-05 02:57:08 +08:00
|
|
|
$last_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();
|
|
|
|
|
2017-12-14 00:56:26 +08:00
|
|
|
# Make sure rank went up
|
2018-01-03 01:12:53 +08:00
|
|
|
$this->assertGreaterThan($user->rank_id, $pilot->rank_id);
|
2017-07-05 02:57:08 +08:00
|
|
|
$this->assertEquals($last_pirep->arr_airport_id, $pilot->curr_airport_id);
|
|
|
|
|
|
|
|
#
|
|
|
|
# Submit another PIREP, adding another 6 hours
|
|
|
|
# it should automatically be accepted
|
|
|
|
#
|
2017-12-14 00:56:26 +08:00
|
|
|
$pirep = factory(Pirep::class)->create([
|
2017-12-26 05:19:34 +08:00
|
|
|
'airline_id' => 1,
|
2018-01-03 01:12:53 +08:00
|
|
|
'user_id' => $user->id,
|
2017-12-14 00:56:26 +08:00
|
|
|
# 120min == 2 hours, currently at 9 hours
|
|
|
|
# Rank bumps up at 10 hours
|
|
|
|
'flight_time' => 120,
|
|
|
|
]);
|
|
|
|
|
|
|
|
# Pilot should be at rank 2, where accept should be automatic
|
|
|
|
$this->pirepSvc->create($pirep);
|
|
|
|
|
|
|
|
$pilot->refresh();
|
2017-07-05 02:57:08 +08:00
|
|
|
$latest_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();
|
|
|
|
|
2017-12-14 00:56:26 +08:00
|
|
|
# Make sure PIREP was auto updated
|
2017-12-20 10:19:36 +08:00
|
|
|
$this->assertEquals(PirepState::ACCEPTED, $latest_pirep->state);
|
2017-12-14 00:56:26 +08:00
|
|
|
|
2017-07-05 02:57:08 +08:00
|
|
|
# Make sure latest PIREP was updated
|
|
|
|
$this->assertNotEquals($last_pirep->id, $latest_pirep->id);
|
2017-06-09 12:00:30 +08:00
|
|
|
}
|
2018-01-03 03:17:22 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find and check for any duplicate PIREPs by a user
|
|
|
|
*/
|
|
|
|
public function testDuplicatePireps()
|
|
|
|
{
|
2018-01-03 04:31:00 +08:00
|
|
|
$pirep = factory(Pirep::class)->create();
|
2018-01-03 03:17:22 +08:00
|
|
|
|
|
|
|
# This should find itself...
|
|
|
|
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
|
|
|
$this->assertNotFalse($dupe_pirep);
|
|
|
|
$this->assertEquals($pirep->id, $dupe_pirep->id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a PIREP outside of the check time interval
|
|
|
|
*/
|
|
|
|
|
|
|
|
$minutes = setting('pireps.duplicate_check_time') + 1;
|
|
|
|
$pirep = factory(Pirep::class)->create([
|
2018-01-03 04:31:00 +08:00
|
|
|
'created_at' => Carbon::now()->subMinutes($minutes)->toDateTimeString()
|
2018-01-03 03:17:22 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
# This should find itself...
|
|
|
|
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
|
|
|
$this->assertFalse($dupe_pirep);
|
|
|
|
}
|
2018-01-04 00:25:55 +08:00
|
|
|
|
|
|
|
public function testCancelViaAPI()
|
|
|
|
{
|
|
|
|
$pirep = factory(App\Models\Pirep::class)->make(['id'=>''])->toArray();
|
|
|
|
$uri = '/api/pireps/prefile';
|
|
|
|
$response = $this->withHeaders($this->apiHeaders())->post($uri, $pirep);
|
|
|
|
$pirep_id = $response->json()['id'];
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
$uri = '/api/pireps/' . $pirep_id . '/acars/position';
|
2018-01-04 00:25:55 +08:00
|
|
|
$acars = factory(App\Models\Acars::class)->make()->toArray();
|
2018-01-05 09:33:23 +08:00
|
|
|
$response = $this->withHeaders($this->apiHeaders())->post($uri, [
|
|
|
|
'positions' => [$acars]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
2018-01-04 00:25:55 +08:00
|
|
|
|
|
|
|
# Cancel it
|
|
|
|
$uri = '/api/pireps/' . $pirep_id . '/cancel';
|
2018-01-05 10:08:22 +08:00
|
|
|
$response = $this->withHeaders($this->apiHeaders())->delete($uri, $acars);
|
2018-01-04 00:25:55 +08:00
|
|
|
$response->assertStatus(200);
|
|
|
|
|
|
|
|
# Should get a 400 when posting an ACARS update
|
2018-01-05 09:33:23 +08:00
|
|
|
$uri = '/api/pireps/' . $pirep_id . '/acars/position';
|
2018-01-04 00:25:55 +08:00
|
|
|
$acars = factory(App\Models\Acars::class)->make()->toArray();
|
|
|
|
$response = $this->withHeaders($this->apiHeaders())->post($uri, $acars);
|
|
|
|
$response->assertStatus(400);
|
|
|
|
}
|
2017-06-09 12:00:30 +08:00
|
|
|
}
|