2017-09-27 03:16:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Schedules;
|
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Tests\TestCase;
|
|
|
|
use Cron\CronExpression;
|
2018-09-01 04:00:13 +00:00
|
|
|
use Cake\Chronos\Chronos;
|
2017-09-27 03:16:26 +00:00
|
|
|
use Pterodactyl\Models\Task;
|
|
|
|
use Pterodactyl\Models\Schedule;
|
|
|
|
use Pterodactyl\Services\Schedules\Tasks\RunTaskService;
|
|
|
|
use Pterodactyl\Services\Schedules\ProcessScheduleService;
|
|
|
|
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
|
|
|
|
|
|
|
|
class ProcessScheduleServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Cron\CronExpression|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
protected $cron;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Schedules\Tasks\RunTaskService|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
protected $runnerService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Schedules\ProcessScheduleService
|
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
2018-09-01 04:00:13 +00:00
|
|
|
|
|
|
|
Chronos::setTestNow(Chronos::now());
|
2017-09-27 03:16:26 +00:00
|
|
|
|
|
|
|
$this->repository = m::mock(ScheduleRepositoryInterface::class);
|
|
|
|
$this->runnerService = m::mock(RunTaskService::class);
|
|
|
|
|
|
|
|
$this->service = new ProcessScheduleService($this->runnerService, $this->repository);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-01-09 03:43:10 +00:00
|
|
|
$this->repository->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);
|
2017-09-27 03:16:26 +00:00
|
|
|
$this->repository->shouldReceive('update')->with($model->id, [
|
|
|
|
'is_processing' => true,
|
2018-09-01 04:12:10 +00:00
|
|
|
'next_run_at' => Chronos::parse(CronExpression::factory($formatted)->getNextRunDate()->format(Chronos::ATOM)),
|
2017-09-27 03:16:26 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
|
|
|
|
|
2018-01-09 03:43:10 +00:00
|
|
|
$this->service->handle($model);
|
2017-09-27 03:16:26 +00:00
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
|
|
|
|
2018-01-09 03:43:10 +00:00
|
|
|
public function testScheduleRunTimeCanBeOverridden()
|
2017-09-27 03:16:26 +00:00
|
|
|
{
|
|
|
|
$model = factory(Schedule::class)->make();
|
|
|
|
$model->setRelation('tasks', collect([$task = factory(Task::class)->make([
|
|
|
|
'sequence_id' => 1,
|
|
|
|
])]));
|
|
|
|
|
2018-01-09 03:43:10 +00:00
|
|
|
$this->repository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
|
2017-09-27 03:16:26 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('update')->with($model->id, [
|
|
|
|
'is_processing' => true,
|
2018-09-01 04:12:10 +00:00
|
|
|
'next_run_at' => Chronos::now()->addSeconds(15),
|
2017-09-27 03:16:26 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
|
|
|
|
|
2018-09-01 04:00:13 +00:00
|
|
|
$this->service->setRunTimeOverride(Chronos::now()->addSeconds(15))->handle($model);
|
2017-09-27 03:16:26 +00:00
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
|
|
|
}
|