2017-06-09 12:00:30 +08:00
|
|
|
<?php
|
|
|
|
|
2017-07-05 02:57:08 +08:00
|
|
|
use App\Models\User;
|
2017-07-04 14:05:23 +08:00
|
|
|
use App\Models\Pirep;
|
|
|
|
|
2017-06-09 12:00:30 +08:00
|
|
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
2017-07-05 02:57:08 +08:00
|
|
|
|
2017-06-09 12:00:30 +08:00
|
|
|
|
|
|
|
class PIREPTest extends TestCase
|
|
|
|
{
|
2017-07-04 14:05:23 +08:00
|
|
|
use WithoutMiddleware;
|
2017-07-05 02:09:44 +08:00
|
|
|
#use DatabaseMigrations;
|
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');
|
|
|
|
}
|
|
|
|
|
2017-07-05 02:57:08 +08:00
|
|
|
/**
|
|
|
|
*/
|
2017-07-04 14:05:23 +08:00
|
|
|
public function testAddPirep()
|
|
|
|
{
|
2017-12-14 00:56:26 +08:00
|
|
|
$pirep = factory(App\Models\Pirep::class)->create();
|
2017-07-04 14:05:23 +08:00
|
|
|
$pirep = $this->pirepSvc->create($pirep, []);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the initial status info
|
|
|
|
*/
|
2017-07-05 04:43:47 +08:00
|
|
|
$this->assertEquals($pirep->status, config('enums.pirep_status.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;
|
|
|
|
|
|
|
|
$this->pirepSvc->changeStatus($pirep, '1');
|
|
|
|
$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);
|
|
|
|
#$this->assertEquals(1, $pirep->pilot->rank_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-07-05 04:43:47 +08:00
|
|
|
$this->pirepSvc->changeStatus($pirep, config('enums.pirep_status.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);
|
|
|
|
//$this->assertEquals(1, $pirep->pilot->rank_id);
|
2017-07-05 02:57:08 +08:00
|
|
|
$this->assertEquals($pirep->arr_airport_id, $pirep->pilot->curr_airport_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check the stats/ranks, etc have incremented properly
|
|
|
|
*/
|
|
|
|
public function testPilotStatsIncr()
|
|
|
|
{
|
2017-12-14 00:56:26 +08:00
|
|
|
$original_pilot = User::find(1);
|
|
|
|
|
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([
|
|
|
|
'user_id' => 1,
|
|
|
|
# 360min == 6 hours, rank should bump up
|
|
|
|
'flight_time' => 360,
|
|
|
|
]);
|
|
|
|
|
|
|
|
foreach ($pireps as $pirep) {
|
|
|
|
$this->pirepSvc->create($pirep);
|
|
|
|
$this->pirepSvc->accept($pirep);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pilot = User::find(1);
|
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
|
|
|
|
$this->assertGreaterThan($original_pilot->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([
|
|
|
|
'user_id' => 1,
|
|
|
|
# 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
|
|
|
|
$this->assertEquals(config('enums.pirep_status.ACCEPTED'), $latest_pirep->status);
|
|
|
|
|
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
|
|
|
}
|
2017-07-11 07:54:51 +08:00
|
|
|
|
|
|
|
public function testPirepFinances()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2017-06-09 12:00:30 +08:00
|
|
|
}
|