diff --git a/app/Cron/Hourly/DeletePireps.php b/app/Cron/Hourly/DeletePireps.php index df199e59..40cfdc7f 100644 --- a/app/Cron/Hourly/DeletePireps.php +++ b/app/Cron/Hourly/DeletePireps.php @@ -17,7 +17,7 @@ use Illuminate\Support\Facades\Log; class DeletePireps extends Listener { /** - * Remove expired bids + * Delete old rejected PIREPs * * @param CronHourly $event * diff --git a/tests/CronTest.php b/tests/CronTest.php new file mode 100644 index 00000000..fa0e269e --- /dev/null +++ b/tests/CronTest.php @@ -0,0 +1,152 @@ +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); + } +}