#21 Some more fixes for PIREPs

This commit is contained in:
Nabeel Shahzad 2017-07-04 13:57:08 -05:00
parent 70dbfbcd3f
commit 870eefb83d
10 changed files with 90 additions and 57 deletions

View File

@ -2,10 +2,11 @@
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\Models\PirepField;
use Illuminate\Foundation\Http\FormRequest;
class CreatePirepFieldRequest extends Request
class CreatePirepFieldRequest extends FormRequest
{
/**

View File

@ -2,10 +2,11 @@
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\Models\Pirep;
use Illuminate\Foundation\Http\FormRequest;
class CreatePirepRequest extends Request
class CreatePirepRequest extends FormRequest
{
/**

View File

@ -2,10 +2,11 @@
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\Models\PirepField;
use Illuminate\Foundation\Http\FormRequest;
class UpdatePirepFieldRequest extends Request
class UpdatePirepFieldRequest extends FormRequest
{
/**

View File

@ -2,10 +2,11 @@
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\Models\Pirep;
use Illuminate\Foundation\Http\FormRequest;
class UpdatePirepRequest extends Request
class UpdatePirepRequest extends FormRequest
{
/**

View File

@ -16,6 +16,7 @@ class Pirep extends Model
use SoftDeletes;
public $table = 'pireps';
public $incrementing = false;
protected $dates = ['deleted_at'];

View File

@ -113,8 +113,8 @@ class PIREPService extends BaseService
return $pirep;
}
$pilot = $pirep->pilot;
$ft = $pirep->flight_time;
$pilot = $pirep->pilot;
$this->pilotSvc->adjustFlightHours($pilot, $ft);
$this->pilotSvc->adjustFlightCount($pilot, +1);

View File

@ -27,8 +27,8 @@
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<ini name="error_reporting" value="E_ALL" />
<!--<ini name="error_reporting" value="E_ALL" />
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
<ini name="display_startup_errors" value="On" />-->
</php>
</phpunit>

View File

@ -1,9 +1,10 @@
<?php
use App\Models\User;
use App\Models\Pirep;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class PIREPTest extends TestCase
{
@ -24,14 +25,39 @@ class PIREPTest extends TestCase
'notes' => 'just a pilot report',
];
/**
* 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,
VMSEnums::$pirep_status['ACCEPTED']);
}
}
$pilot = User::where('id', $this->SAMPLE_PIREP['user_id'])->first();
return $pilot;
}
public function setUp()
{
parent::setUp(); // TODO: Change the autogenerated stub
$this->addData('base');
$this->pirepSvc = app('App\Services\PIREPService');
}
/**
* @covers \App\Services\PilotService
* @covers \App\Services\PIREPService
*/
public function testAddPirep()
{
$pirep = new Pirep($this->SAMPLE_PIREP);
@ -60,5 +86,39 @@ class PIREPTest extends TestCase
$this->assertEquals(0, $pirep->pilot->flights);
$this->assertEquals(0, $pirep->pilot->flight_time);
$this->assertEquals(1, $pirep->pilot->rank_id);
$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(
VMSEnums::$pirep_status['ACCEPTED'],
$latest_pirep->status
);
}
}

View File

@ -1,6 +1,8 @@
<?php
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;
abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
{
@ -24,10 +26,11 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
public function setUp() {
parent::setUp();
$this->reset_db();
/*
Artisan::call('migrate');
Artisan::call('db:seed');
*/
Mail::fake();
#Artisan::call('migrate');
#Artisan::call('db:seed');
}
/**
@ -48,34 +51,10 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
return $app->make('App\Repositories\\' . $repo_name);
}
public function readYaml($file)
{
return Yaml::parse(file_get_contents(base_path('tests/data/' . $file . '.yml')));
}
public function addData($file)
{
$time_fields = ['created_at', 'updated_at'];
$curr_time = Carbon::now('UTC')->format('Y-m-d H:i:s');
$yml = $this->readYaml($file);
foreach ($yml as $table => $rows) {
foreach ($rows as $row) {
# encrypt any password fields
if (array_key_exists('password', $row)) {
$row['password'] = bcrypt($row['password']);
}
# if any time fields are == to "now", then insert the right time
foreach ($time_fields as $tf) {
if (array_key_exists($tf, $row) && $row[$tf] === 'now') {
$row[$tf] = $curr_time;
}
}
DB::table($table)->insert($row);
}
}
$svc = app('\App\Services\DatabaseService');
$file_path = base_path('tests/data/' . $file . '.yml');
$svc->seed_from_yaml_file($file_path);
}
}

View File

@ -135,14 +135,3 @@ flights:
dpt_airport_id: 1
arr_airport_id: 2
route: KAUS KJFK
#pireps:
# - user_id: 1
# flight_id: 1
# aircraft_id: 1
# dpt_airport_id: 1
# arr_airport_id: 2
# flight_time: 21600 # 6 hours
# level: 320
# status: -1
# notes: just a pilot report