phpvms/tests/PIREPTest.php

125 lines
3.8 KiB
PHP
Raw Normal View History

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;
protected $SAMPLE_PIREP
= [
'user_id' => 1,
'flight_id' => 1,
'aircraft_id' => 1,
'dpt_airport_id' => 1,
'arr_airport_id' => 2,
'flight_time' => 21600, # 6 hours
'level' => 320,
2017-07-05 02:57:08 +08:00
'source' => 0, # manual
2017-07-04 14:05:23 +08:00
'notes' => 'just a pilot report',
];
2017-07-05 02:57:08 +08:00
/**
* Add $count number of PIREPs and return a User object
* @param int $count
* @param bool $accept
*
* @return User
*/
protected function addPIREP($count=1, $accept=true): User
{
for($i = 0; $i < $count; $i++) {
$pirep = new Pirep($this->SAMPLE_PIREP);
$pirep = $this->pirepSvc->create($pirep, []);
if($accept) {
$this->pirepSvc->changeStatus($pirep,
2017-07-05 04:43:47 +08:00
config('enums.pirep_status.ACCEPTED'));
2017-07-05 02:57:08 +08:00
}
}
$pilot = User::where('id', $this->SAMPLE_PIREP['user_id'])->first();
return $pilot;
}
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
/**
* @covers \App\Services\PilotService
* @covers \App\Services\PIREPService
*/
2017-07-04 14:05:23 +08:00
public function testAddPirep()
{
$pirep = new Pirep($this->SAMPLE_PIREP);
$pirep->save();
$pirep = $this->pirepSvc->create($pirep, []);
/**
* Check the initial status info
*/
$this->assertEquals($pirep->pilot->flights, 0);
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-07-05 04:43:47 +08:00
$this->pirepSvc->changeStatus($pirep, config('enums.pirep_status.ACCEPTED'));
2017-07-04 14:05:23 +08:00
$this->assertEquals(1, $pirep->pilot->flights);
$this->assertEquals(21600, $pirep->pilot->flight_time);
$this->assertEquals(1, $pirep->pilot->rank_id);
/**
* Now go from ACCEPTED to REJECTED
*/
2017-07-05 04:43:47 +08:00
$this->pirepSvc->changeStatus($pirep, config('enums.pirep_status.REJECTED'));
2017-07-04 14:05:23 +08:00
$this->assertEquals(0, $pirep->pilot->flights);
$this->assertEquals(0, $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
* @covers \App\Services\PilotService
* @covers \App\Services\PIREPService
*/
public function testPilotStatsIncr()
{
# Submit two PIREPs
$pilot = $this->addPIREP(2);
$last_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();
$this->assertEquals(2, $pilot->flights);
$this->assertEquals(43200, $pilot->flight_time);
$this->assertEquals(2, $pilot->rank_id);
$this->assertEquals($last_pirep->arr_airport_id, $pilot->curr_airport_id);
#
# Submit another PIREP, adding another 6 hours
# it should automatically be accepted
#
$pilot = $this->addPIREP(1, false);
$latest_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();
# Make sure latest PIREP was updated
$this->assertNotEquals($last_pirep->id, $latest_pirep->id);
# The PIREP should have been automatically accepted
$this->assertEquals(
2017-07-05 04:43:47 +08:00
config('enums.pirep_status.ACCEPTED'),
2017-07-05 02:57:08 +08:00
$latest_pirep->status
);
2017-06-09 12:00:30 +08:00
}
}