2017-09-27 03:16:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Schedules;
|
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Tests\TestCase;
|
|
|
|
use Cron\CronExpression;
|
|
|
|
use Pterodactyl\Models\Task;
|
|
|
|
use Pterodactyl\Models\Schedule;
|
2018-09-03 21:04:25 +00:00
|
|
|
use Illuminate\Contracts\Bus\Dispatcher;
|
|
|
|
use Pterodactyl\Jobs\Schedule\RunTaskJob;
|
2017-09-27 03:16:26 +00:00
|
|
|
use Pterodactyl\Services\Schedules\ProcessScheduleService;
|
2018-09-03 21:04:25 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
|
2017-09-27 03:16:26 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
|
|
|
|
|
|
|
|
class ProcessScheduleServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2018-09-03 21:04:25 +00:00
|
|
|
* @var \Illuminate\Contracts\Bus\Dispatcher|\Mockery\Mock
|
2017-09-27 03:16:26 +00:00
|
|
|
*/
|
2018-09-03 21:04:25 +00:00
|
|
|
private $dispatcher;
|
2017-09-27 03:16:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface|\Mockery\Mock
|
|
|
|
*/
|
2018-09-03 21:04:25 +00:00
|
|
|
private $scheduleRepository;
|
2017-09-27 03:16:26 +00:00
|
|
|
|
|
|
|
/**
|
2018-09-03 21:04:25 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface|\Mockery\Mock
|
2017-09-27 03:16:26 +00:00
|
|
|
*/
|
2018-09-03 21:04:25 +00:00
|
|
|
private $taskRepository;
|
2017-09-27 03:16:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-09-27 03:16:26 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2018-09-01 04:00:13 +00:00
|
|
|
|
2018-09-03 21:04:25 +00:00
|
|
|
$this->dispatcher = m::mock(Dispatcher::class);
|
|
|
|
$this->scheduleRepository = m::mock(ScheduleRepositoryInterface::class);
|
|
|
|
$this->taskRepository = m::mock(TaskRepositoryInterface::class);
|
2017-09-27 03:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a schedule can be updated and first task set to run.
|
|
|
|
*/
|
|
|
|
public function testScheduleIsUpdatedAndRun()
|
|
|
|
{
|
|
|
|
$model = factory(Schedule::class)->make();
|
|
|
|
$model->setRelation('tasks', collect([$task = factory(Task::class)->make([
|
|
|
|
'sequence_id' => 1,
|
|
|
|
])]));
|
|
|
|
|
2018-09-03 21:04:25 +00:00
|
|
|
$this->scheduleRepository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
|
2017-09-27 03:16:26 +00:00
|
|
|
|
2018-05-13 04:41:56 +00:00
|
|
|
$formatted = sprintf('%s %s %s * %s', $model->cron_minute, $model->cron_hour, $model->cron_day_of_month, $model->cron_day_of_week);
|
2018-09-03 21:04:25 +00:00
|
|
|
$this->scheduleRepository->shouldReceive('update')->with($model->id, [
|
2017-09-27 03:16:26 +00:00
|
|
|
'is_processing' => true,
|
2018-09-03 21:32:33 +00:00
|
|
|
'next_run_at' => CronExpression::factory($formatted)->getNextRunDate(),
|
2017-09-27 03:16:26 +00:00
|
|
|
]);
|
|
|
|
|
2018-09-03 21:04:25 +00:00
|
|
|
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => true])->once();
|
|
|
|
|
|
|
|
$this->dispatcher->shouldReceive('dispatch')->with(m::on(function ($class) use ($model, $task) {
|
|
|
|
$this->assertInstanceOf(RunTaskJob::class, $class);
|
|
|
|
$this->assertSame($task->time_offset, $class->delay);
|
|
|
|
$this->assertSame($task->id, $class->task);
|
|
|
|
$this->assertSame($model->id, $class->schedule);
|
2017-09-27 03:16:26 +00:00
|
|
|
|
2018-09-03 21:04:25 +00:00
|
|
|
return true;
|
|
|
|
}))->once();
|
|
|
|
|
|
|
|
$this->getService()->handle($model);
|
2017-09-27 03:16:26 +00:00
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
|
|
|
|
2018-09-03 21:32:33 +00:00
|
|
|
/**
|
|
|
|
* Return an instance of the service for testing purposes.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Schedules\ProcessScheduleService
|
|
|
|
*/
|
2018-09-03 21:04:25 +00:00
|
|
|
private function getService(): ProcessScheduleService
|
|
|
|
{
|
|
|
|
return new ProcessScheduleService($this->dispatcher, $this->scheduleRepository, $this->taskRepository);
|
|
|
|
}
|
2017-09-27 03:16:26 +00:00
|
|
|
}
|