Call the AwardListener in the proper UserStatsChanged event #155

This commit is contained in:
Nabeel Shahzad 2018-03-17 12:55:50 -05:00
parent 3c39aeee43
commit 67ef6872af
6 changed files with 90 additions and 12 deletions

View File

@ -0,0 +1,13 @@
<?php
use Faker\Generator as Faker;
$factory->define(App\Models\Award::class, function (Faker $faker) {
return [
'id' => null,
'name' => $faker->name,
'description' => $faker->text(10),
'ref_class' => null,
'ref_class_params' => null,
];
});

View File

@ -29,6 +29,16 @@ class CreateAwardsTable extends Migration
$table->index(['ref_class']); $table->index(['ref_class']);
}); });
Schema::create('user_awards', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id');
$table->unsignedInteger('award_id');
$table->timestamps();
$table->index(['user_id', 'award_id']);
});
} }
/** /**
@ -39,5 +49,6 @@ class CreateAwardsTable extends Migration
public function down() public function down()
{ {
Schema::dropIfExists('awards'); Schema::dropIfExists('awards');
Schema::dropIfExists('user_awards');
} }
} }

View File

@ -2,7 +2,7 @@
namespace App\Listeners; namespace App\Listeners;
use App\Events\UserStateChanged; use App\Events\UserStatsChanged;
use App\Models\Award; use App\Models\Award;
/** /**
@ -13,9 +13,9 @@ class AwardListener
{ {
/** /**
* Call all of the awards * Call all of the awards
* @param UserStateChanged $event * @param UserStatsChanged $event
*/ */
public function handle(UserStateChanged $event) public function handle(UserStatsChanged $event)
{ {
$awards = Award::all(); $awards = Award::all();
foreach($awards as $award) { foreach($awards as $award) {

View File

@ -6,7 +6,7 @@ use App\Events\CronMonthly;
use App\Events\CronNightly; use App\Events\CronNightly;
use App\Events\CronWeekly; use App\Events\CronWeekly;
use App\Events\Expenses; use App\Events\Expenses;
use App\Events\UserStateChanged; use App\Events\UserStatsChanged;
use App\Listeners\AwardListener; use App\Listeners\AwardListener;
use App\Listeners\Cron\Nightly\RecalculateBalances; use App\Listeners\Cron\Nightly\RecalculateBalances;
use App\Listeners\ExpenseListener; use App\Listeners\ExpenseListener;
@ -17,11 +17,6 @@ use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvi
class EventServiceProvider extends ServiceProvider class EventServiceProvider extends ServiceProvider
{ {
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [ protected $listen = [
Expenses::class => [ Expenses::class => [
@ -41,7 +36,7 @@ class EventServiceProvider extends ServiceProvider
\App\Listeners\Cron\Monthly\ApplyExpenses::class \App\Listeners\Cron\Monthly\ApplyExpenses::class
], ],
UserStateChanged::class => [ UserStatsChanged::class => [
AwardListener::class, AwardListener::class,
], ],
]; ];

View File

@ -285,8 +285,6 @@ class PirepService extends BaseService
$pirep->save(); $pirep->save();
$pirep->refresh(); $pirep->refresh();
$this->setPilotState($pilot, $pirep);
Log::info('PIREP ' . $pirep->id . ' state change to ACCEPTED'); Log::info('PIREP ' . $pirep->id . ' state change to ACCEPTED');
# Update the aircraft # Update the aircraft
@ -300,6 +298,7 @@ class PirepService extends BaseService
# Any ancillary tasks before an event is dispatched # Any ancillary tasks before an event is dispatched
$this->removeBid($pirep); $this->removeBid($pirep);
$this->setPilotState($pilot, $pirep);
event(new PirepAccepted($pirep)); event(new PirepAccepted($pirep));
return $pirep; return $pirep;

60
tests/AwardsTest.php Normal file
View File

@ -0,0 +1,60 @@
<?php
use App\Models\UserAward;
class AwardsTest extends TestCase
{
private $awardSvc,
$pirepSvc;
public function setUp()
{
parent::setUp();
$this->awardSvc = app(\App\Services\AwardService::class);
$this->pirepSvc = app(\App\Services\PirepService::class);
}
/**
* Make sure the awards classes are returned
*/
public function testGetAwardsClasses()
{
$classes = $this->awardSvc->findAllAwardClasses();
$this->assertCount(2, $classes);
}
/**
* Test to make sure that the award is actually given out
*/
public function testAwardsGiven()
{
// Create one award that's given out with one flight
$award = factory(App\Models\Award::class)->create([
'ref_class' => App\Awards\PilotFlightAwards::class,
'ref_class_params' => 1,
]);
$user = factory(App\Models\User::class)->create([
'flights' => 0,
]);
$pirep = factory(App\Models\Pirep::class)->create([
'airline_id' => $user->airline->id,
'user_id' => $user->id,
]);
$this->pirepSvc->create($pirep);
$this->pirepSvc->accept($pirep);
$w = [
'user_id' => $user->id,
'award_id' => $award->id,
];
# Make sure only one is awarded
$this->assertEquals(1, UserAward::where($w)->count(['id']));
$found_award = UserAward::where($w)->first();
$this->assertNotNull($found_award);
}
}