Add tests for the cron jobs (#1141)

This commit is contained in:
Nabeel S 2021-04-14 08:37:48 -04:00 committed by GitHub
parent d18925f473
commit bffc4f911c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 153 additions and 1 deletions

View File

@ -17,7 +17,7 @@ use Illuminate\Support\Facades\Log;
class DeletePireps extends Listener
{
/**
* Remove expired bids
* Delete old rejected PIREPs
*
* @param CronHourly $event
*

152
tests/CronTest.php Normal file
View File

@ -0,0 +1,152 @@
<?php
namespace Tests;
use App\Cron\Hourly\DeletePireps;
use App\Cron\Hourly\RemoveExpiredLiveFlights;
use App\Events\CronHourly;
use App\Models\Enums\PirepState;
use App\Models\Pirep;
use App\Models\User;
use Carbon\Carbon;
class CronTest extends TestCase
{
/**
* Create a new sample PIREP
*
* @param $subtractTime
*
* @return Pirep
*/
protected static function getPirep($subtractTime): Pirep
{
/** @var User $user */
$user = factory(User::class)->create();
/** @var Pirep $pirep */
return factory(Pirep::class)->create([
'user_id' => $user->id,
'state' => PirepState::IN_PROGRESS,
'updated_at' => Carbon::now('UTC')->subHours($subtractTime),
]);
}
/**
* @throws \Exception
*/
public function testExpiredFlightNotBeingRemoved()
{
$this->updateSetting('acars.live_time', 0);
$pirep = $this->getPirep(2);
/** @var RemoveExpiredLiveFlights $eventListener */
$eventListener = app(RemoveExpiredLiveFlights::class);
$eventListener->handle(new CronHourly());
$found_pirep = Pirep::find($pirep->id);
$this->assertNotNull($found_pirep);
}
/**
* Delete flights that are more than X hours old and still in progress (no updates)
*
* @throws \Exception
*/
public function testExpiredFlightShouldNotBeRemoved()
{
$this->updateSetting('acars.live_time', 3);
$pirep = $this->getPirep(2);
/** @var RemoveExpiredLiveFlights $eventListener */
$eventListener = app(RemoveExpiredLiveFlights::class);
$eventListener->handle(new CronHourly());
$found_pirep = Pirep::find($pirep->id);
$this->assertNotNull($found_pirep);
}
/**
* Delete flights that are more than X hours old and still in progress (no updates)
*
* @throws \Exception
*/
public function testExpiredFlightShouldBeRemoved()
{
$this->updateSetting('acars.live_time', 3);
$pirep = $this->getPirep(4);
/** @var RemoveExpiredLiveFlights $eventListener */
$eventListener = app(RemoveExpiredLiveFlights::class);
$eventListener->handle(new CronHourly());
$found_pirep = Pirep::find($pirep->id);
$this->assertNull($found_pirep);
}
/**
* Delete flights that are more than X hours old and still in progress (no updates)
*
* @throws \Exception
*/
public function testCompletedFlightsShouldNotBeDeleted()
{
$this->updateSetting('acars.live_time', 3);
$pirep = $this->getPirep(4);
// Make sure the state is accepted
$pirep->state = PirepState::ACCEPTED;
$pirep->save();
/** @var RemoveExpiredLiveFlights $eventListener */
$eventListener = app(RemoveExpiredLiveFlights::class);
$eventListener->handle(new CronHourly());
$found_pirep = Pirep::find($pirep->id);
$this->assertNotNull($found_pirep);
}
/**
* Delete flights that are more than X hours old and have been rejected
*
* @throws \Exception
*/
public function testDeleteRejectedPireps()
{
$this->updateSetting('pireps.delete_rejected_hours', 3);
$pirep = $this->getPirep(4);
// Make sure the state is accepted
$pirep->state = PirepState::REJECTED;
$pirep->save();
/** @var DeletePireps $eventListener */
$eventListener = app(DeletePireps::class);
$eventListener->handle(new CronHourly());
$found_pirep = Pirep::find($pirep->id);
$this->assertNotNull($found_pirep);
}
/**
* Delete flights that are more than X hours old and have been cancelled
*
* @throws \Exception
*/
public function testDeleteCancelledPireps()
{
$this->updateSetting('pireps.delete_cancelled_hours', 3);
$pirep = $this->getPirep(4);
// Make sure the state is accepted
$pirep->state = PirepState::CANCELLED;
$pirep->save();
/** @var DeletePireps $eventListener */
$eventListener = app(DeletePireps::class);
$eventListener->handle(new CronHourly());
$found_pirep = Pirep::find($pirep->id);
$this->assertNotNull($found_pirep);
}
}