88use Cake \Chronos \Chronos ;
99use Pterodactyl \Models \Task ;
1010use Pterodactyl \Models \Schedule ;
11- use Pterodactyl \Services \Schedules \Tasks \RunTaskService ;
11+ use Illuminate \Contracts \Bus \Dispatcher ;
12+ use Pterodactyl \Jobs \Schedule \RunTaskJob ;
1213use Pterodactyl \Services \Schedules \ProcessScheduleService ;
14+ use Pterodactyl \Contracts \Repository \TaskRepositoryInterface ;
1315use Pterodactyl \Contracts \Repository \ScheduleRepositoryInterface ;
1416
1517class ProcessScheduleServiceTest extends TestCase
1618{
1719 /**
18- * @var \Cron\CronExpression |\Mockery\Mock
20+ * @var \Illuminate\Contracts\Bus\Dispatcher |\Mockery\Mock
1921 */
20- protected $ cron ;
22+ private $ dispatcher ;
2123
2224 /**
2325 * @var \Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface|\Mockery\Mock
2426 */
25- protected $ repository ;
27+ private $ scheduleRepository ;
2628
2729 /**
28- * @var \Pterodactyl\Services\Schedules\Tasks\RunTaskService |\Mockery\Mock
30+ * @var \Pterodactyl\Contracts\Repository\TaskRepositoryInterface |\Mockery\Mock
2931 */
30- protected $ runnerService ;
31-
32- /**
33- * @var \Pterodactyl\Services\Schedules\ProcessScheduleService
34- */
35- protected $ service ;
32+ private $ taskRepository ;
3633
3734 /**
3835 * Setup tests.
@@ -42,11 +39,9 @@ public function setUp()
4239 parent ::setUp ();
4340
4441 Chronos::setTestNow (Chronos::now ());
45-
46- $ this ->repository = m::mock (ScheduleRepositoryInterface::class);
47- $ this ->runnerService = m::mock (RunTaskService::class);
48-
49- $ this ->service = new ProcessScheduleService ($ this ->runnerService , $ this ->repository );
42+ $ this ->dispatcher = m::mock (Dispatcher::class);
43+ $ this ->scheduleRepository = m::mock (ScheduleRepositoryInterface::class);
44+ $ this ->taskRepository = m::mock (TaskRepositoryInterface::class);
5045 }
5146
5247 /**
@@ -59,17 +54,26 @@ public function testScheduleIsUpdatedAndRun()
5954 'sequence_id ' => 1 ,
6055 ])]));
6156
62- $ this ->repository ->shouldReceive ('loadTasks ' )->with ($ model )->once ()->andReturn ($ model );
57+ $ this ->scheduleRepository ->shouldReceive ('loadTasks ' )->with ($ model )->once ()->andReturn ($ model );
6358
6459 $ formatted = sprintf ('%s %s %s * %s ' , $ model ->cron_minute , $ model ->cron_hour , $ model ->cron_day_of_month , $ model ->cron_day_of_week );
65- $ this ->repository ->shouldReceive ('update ' )->with ($ model ->id , [
60+ $ this ->scheduleRepository ->shouldReceive ('update ' )->with ($ model ->id , [
6661 'is_processing ' => true ,
6762 'next_run_at ' => Chronos::parse (CronExpression::factory ($ formatted )->getNextRunDate ()->format (Chronos::ATOM )),
6863 ]);
6964
70- $ this ->runnerService ->shouldReceive ('handle ' )->with ($ task )->once ()->andReturnNull ();
65+ $ this ->taskRepository ->shouldReceive ('update ' )->with ($ task ->id , ['is_queued ' => true ])->once ();
66+
67+ $ this ->dispatcher ->shouldReceive ('dispatch ' )->with (m::on (function ($ class ) use ($ model , $ task ) {
68+ $ this ->assertInstanceOf (RunTaskJob::class, $ class );
69+ $ this ->assertSame ($ task ->time_offset , $ class ->delay );
70+ $ this ->assertSame ($ task ->id , $ class ->task );
71+ $ this ->assertSame ($ model ->id , $ class ->schedule );
7172
72- $ this ->service ->handle ($ model );
73+ return true ;
74+ }))->once ();
75+
76+ $ this ->getService ()->handle ($ model );
7377 $ this ->assertTrue (true );
7478 }
7579
@@ -80,16 +84,30 @@ public function testScheduleRunTimeCanBeOverridden()
8084 'sequence_id ' => 1 ,
8185 ])]));
8286
83- $ this ->repository ->shouldReceive ('loadTasks ' )->with ($ model )->once ()->andReturn ($ model );
87+ $ this ->scheduleRepository ->shouldReceive ('loadTasks ' )->with ($ model )->once ()->andReturn ($ model );
8488
85- $ this ->repository ->shouldReceive ('update ' )->with ($ model ->id , [
89+ $ this ->scheduleRepository ->shouldReceive ('update ' )->with ($ model ->id , [
8690 'is_processing ' => true ,
8791 'next_run_at ' => Chronos::now ()->addSeconds (15 ),
8892 ]);
8993
90- $ this ->runnerService ->shouldReceive ('handle ' )->with ($ task )->once ()->andReturnNull ();
94+ $ this ->taskRepository ->shouldReceive ('update ' )->with ($ task ->id , ['is_queued ' => true ])->once ();
95+
96+ $ this ->dispatcher ->shouldReceive ('dispatch ' )->with (m::on (function ($ class ) use ($ model , $ task ) {
97+ $ this ->assertInstanceOf (RunTaskJob::class, $ class );
98+ $ this ->assertSame ($ task ->time_offset , $ class ->delay );
99+ $ this ->assertSame ($ task ->id , $ class ->task );
100+ $ this ->assertSame ($ model ->id , $ class ->schedule );
101+
102+ return true ;
103+ }))->once ();
91104
92- $ this ->service ->setRunTimeOverride (Chronos::now ()->addSeconds (15 ))->handle ($ model );
105+ $ this ->getService () ->setRunTimeOverride (Chronos::now ()->addSeconds (15 ))->handle ($ model );
93106 $ this ->assertTrue (true );
94107 }
108+
109+ private function getService (): ProcessScheduleService
110+ {
111+ return new ProcessScheduleService ($ this ->dispatcher , $ this ->scheduleRepository , $ this ->taskRepository );
112+ }
95113}
0 commit comments