2018-03-18 01:55:50 +08:00
|
|
|
<?php
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
|
|
|
use App\Models\Award;
|
|
|
|
use App\Models\Pirep;
|
|
|
|
use App\Models\User;
|
2018-03-18 01:55:50 +08:00
|
|
|
use App\Models\UserAward;
|
2019-08-05 20:27:53 +08:00
|
|
|
use App\Services\AwardService;
|
|
|
|
use App\Services\PirepService;
|
2020-10-23 04:25:52 +08:00
|
|
|
use Modules\Awards\Awards\FlightRouteAwards;
|
2020-05-23 23:43:29 +08:00
|
|
|
use Modules\Awards\Awards\PilotFlightAwards;
|
2018-03-18 01:55:50 +08:00
|
|
|
|
|
|
|
class AwardsTest extends TestCase
|
|
|
|
{
|
2020-02-29 05:06:45 +08:00
|
|
|
/** @var AwardService */
|
2018-08-27 00:40:04 +08:00
|
|
|
private $awardSvc;
|
2020-02-29 05:06:45 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
private $pirepSvc;
|
2018-03-18 01:55:50 +08:00
|
|
|
|
2019-05-12 23:50:38 +08:00
|
|
|
public function setUp(): void
|
2018-03-18 01:55:50 +08:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2019-08-05 20:27:53 +08:00
|
|
|
$this->addData('base');
|
|
|
|
$this->addData('fleet');
|
|
|
|
$this->awardSvc = app(AwardService::class);
|
|
|
|
$this->pirepSvc = app(PirepService::class);
|
2018-03-18 01:55:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure the awards classes are returned
|
|
|
|
*/
|
|
|
|
public function testGetAwardsClasses()
|
|
|
|
{
|
|
|
|
$classes = $this->awardSvc->findAllAwardClasses();
|
2020-10-13 06:54:42 +08:00
|
|
|
$this->assertGreaterThanOrEqual(2, $classes);
|
2018-03-18 01:55:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test to make sure that the award is actually given out
|
|
|
|
*/
|
|
|
|
public function testAwardsGiven()
|
|
|
|
{
|
|
|
|
// Create one award that's given out with one flight
|
2022-03-14 23:45:18 +08:00
|
|
|
$award = Award::factory()->create([
|
2020-05-23 23:43:29 +08:00
|
|
|
'ref_model' => PilotFlightAwards::class,
|
2018-04-02 03:32:01 +08:00
|
|
|
'ref_model_params' => 1,
|
2018-03-18 01:55:50 +08:00
|
|
|
]);
|
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
$user = User::factory()->create([
|
2018-03-18 01:55:50 +08:00
|
|
|
'flights' => 0,
|
|
|
|
]);
|
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
$pirep = Pirep::factory()->create([
|
2018-03-18 01:55:50 +08:00
|
|
|
'airline_id' => $user->airline->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'user_id' => $user->id,
|
2018-03-18 01:55:50 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->pirepSvc->create($pirep);
|
|
|
|
$this->pirepSvc->accept($pirep);
|
|
|
|
|
|
|
|
$w = [
|
2018-08-27 00:40:04 +08:00
|
|
|
'user_id' => $user->id,
|
2018-03-18 01:55:50 +08:00
|
|
|
'award_id' => $award->id,
|
|
|
|
];
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Make sure only one is awarded
|
2018-03-18 01:55:50 +08:00
|
|
|
$this->assertEquals(1, UserAward::where($w)->count(['id']));
|
|
|
|
|
|
|
|
$found_award = UserAward::where($w)->first();
|
|
|
|
$this->assertNotNull($found_award);
|
|
|
|
}
|
2020-10-23 04:25:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the flight route
|
|
|
|
*/
|
|
|
|
public function testFlightRouteAward()
|
|
|
|
{
|
|
|
|
/** @var \App\Models\User $user */
|
2022-03-14 23:45:18 +08:00
|
|
|
$user = User::factory()->create([
|
2020-10-23 04:25:52 +08:00
|
|
|
'flights' => 0,
|
|
|
|
]);
|
|
|
|
|
|
|
|
/** @var \App\Models\Award $award */
|
2022-03-14 23:45:18 +08:00
|
|
|
$award = Award::factory()->create([
|
2020-10-23 04:25:52 +08:00
|
|
|
'ref_model' => FlightRouteAwards::class,
|
|
|
|
'ref_model_params' => 1,
|
|
|
|
]);
|
|
|
|
|
|
|
|
/** @var Pirep $pirep */
|
2022-03-14 23:45:18 +08:00
|
|
|
$pirep = Pirep::factory()->create([
|
2020-10-23 04:25:52 +08:00
|
|
|
'airline_id' => $user->airline->id,
|
|
|
|
'user_id' => $user->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$flightAward = new FlightRouteAwards($award, $user);
|
|
|
|
|
|
|
|
// Test no last PIREP for the user
|
|
|
|
$this->assertFalse($flightAward->check(''));
|
|
|
|
|
|
|
|
// Reinit award, add a last user PIREP id
|
|
|
|
$user->last_pirep_id = $pirep->id;
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$flightAward = new FlightRouteAwards($award, $user);
|
|
|
|
$validStrs = [
|
|
|
|
$pirep->dpt_airport_id.':'.$pirep->arr_airport_id,
|
|
|
|
$pirep->dpt_airport_id.':'.$pirep->arr_airport_id.' ',
|
|
|
|
$pirep->dpt_airport_id.':'.$pirep->arr_airport_id.':',
|
|
|
|
strtolower($pirep->dpt_airport_id).':'.strtolower($pirep->arr_airport_id),
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($validStrs as $str) {
|
|
|
|
$this->assertTrue($flightAward->check($str));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check error conditions
|
|
|
|
$errStrs = [
|
|
|
|
'',
|
|
|
|
' ',
|
|
|
|
':',
|
|
|
|
'ABCD:EDFSDF',
|
|
|
|
$pirep->dpt_airport_id.':',
|
|
|
|
':'.$pirep->arr_airport_id,
|
|
|
|
':'.$pirep->arr_airport_id.':',
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($errStrs as $err) {
|
|
|
|
$this->assertFalse($flightAward->check($err));
|
|
|
|
}
|
|
|
|
}
|
2018-03-18 01:55:50 +08:00
|
|
|
}
|