forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessScheduleServiceTest.php
More file actions
95 lines (75 loc) · 2.93 KB
/
ProcessScheduleServiceTest.php
File metadata and controls
95 lines (75 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace Tests\Unit\Services\Schedules;
use Mockery as m;
use Tests\TestCase;
use Cron\CronExpression;
use Cake\Chronos\Chronos;
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();
Chronos::setTestNow(Chronos::now());
$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,
])]));
$this->repository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
$formatted = sprintf('%s %s %s * %s', $model->cron_minute, $model->cron_hour, $model->cron_day_of_month, $model->cron_day_of_week);
$this->repository->shouldReceive('update')->with($model->id, [
'is_processing' => true,
'next_run_at' => Chronos::parse(CronExpression::factory($formatted)->getNextRunDate()->format(Chronos::ATOM)),
]);
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
$this->service->handle($model);
$this->assertTrue(true);
}
public function testScheduleRunTimeCanBeOverridden()
{
$model = factory(Schedule::class)->make();
$model->setRelation('tasks', collect([$task = factory(Task::class)->make([
'sequence_id' => 1,
])]));
$this->repository->shouldReceive('loadTasks')->with($model)->once()->andReturn($model);
$this->repository->shouldReceive('update')->with($model->id, [
'is_processing' => true,
'next_run_at' => Chronos::now()->addSeconds(15),
]);
$this->runnerService->shouldReceive('handle')->with($task)->once()->andReturnNull();
$this->service->setRunTimeOverride(Chronos::now()->addSeconds(15))->handle($model);
$this->assertTrue(true);
}
}