From 870eefb83d70fef803a3a500818383bbbf46458e Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Tue, 4 Jul 2017 13:57:08 -0500 Subject: [PATCH] #21 Some more fixes for PIREPs --- app/Http/Requests/CreatePirepFieldRequest.php | 5 +- app/Http/Requests/CreatePirepRequest.php | 5 +- app/Http/Requests/UpdatePirepFieldRequest.php | 5 +- app/Http/Requests/UpdatePirepRequest.php | 5 +- app/Models/Pirep.php | 1 + app/Services/PIREPService.php | 2 +- phpunit.xml | 4 +- tests/PIREPTest.php | 66 ++++++++++++++++++- tests/TestCase.php | 43 ++++-------- tests/data/base.yml | 11 ---- 10 files changed, 90 insertions(+), 57 deletions(-) diff --git a/app/Http/Requests/CreatePirepFieldRequest.php b/app/Http/Requests/CreatePirepFieldRequest.php index 014ed037..9900b7b6 100644 --- a/app/Http/Requests/CreatePirepFieldRequest.php +++ b/app/Http/Requests/CreatePirepFieldRequest.php @@ -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 { /** diff --git a/app/Http/Requests/CreatePirepRequest.php b/app/Http/Requests/CreatePirepRequest.php index 831559ec..6f39d0aa 100644 --- a/app/Http/Requests/CreatePirepRequest.php +++ b/app/Http/Requests/CreatePirepRequest.php @@ -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 { /** diff --git a/app/Http/Requests/UpdatePirepFieldRequest.php b/app/Http/Requests/UpdatePirepFieldRequest.php index dd048e2d..38d21c74 100644 --- a/app/Http/Requests/UpdatePirepFieldRequest.php +++ b/app/Http/Requests/UpdatePirepFieldRequest.php @@ -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 { /** diff --git a/app/Http/Requests/UpdatePirepRequest.php b/app/Http/Requests/UpdatePirepRequest.php index 00a1af71..4d161002 100644 --- a/app/Http/Requests/UpdatePirepRequest.php +++ b/app/Http/Requests/UpdatePirepRequest.php @@ -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 { /** diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index 973b1ff0..771b1d5c 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -16,6 +16,7 @@ class Pirep extends Model use SoftDeletes; public $table = 'pireps'; + public $incrementing = false; protected $dates = ['deleted_at']; diff --git a/app/Services/PIREPService.php b/app/Services/PIREPService.php index f66902e5..472dbfc9 100644 --- a/app/Services/PIREPService.php +++ b/app/Services/PIREPService.php @@ -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); diff --git a/phpunit.xml b/phpunit.xml index 01271e96..e7ec9c12 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -27,8 +27,8 @@ - + diff --git a/tests/PIREPTest.php b/tests/PIREPTest.php index 8f69ed6b..1facb8d3 100644 --- a/tests/PIREPTest.php +++ b/tests/PIREPTest.php @@ -1,9 +1,10 @@ 2, 'flight_time' => 21600, # 6 hours 'level' => 320, - 'source' => 0, # manual + 'source' => 0, # manual '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 + ); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index a9ac5efa..d0927742 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,6 +1,8 @@ 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); } } diff --git a/tests/data/base.yml b/tests/data/base.yml index 00e023e5..cbb08e06 100644 --- a/tests/data/base.yml +++ b/tests/data/base.yml @@ -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