Files
panel-pelican-dev/tests/Integration/Jobs/Schedule/RunTaskJobTest.php

174 lines
5.6 KiB
PHP
Raw Permalink Normal View History

2021-05-01 12:24:42 -07:00
<?php
2024-03-12 22:39:16 -04:00
namespace App\Tests\Integration\Jobs\Schedule;
2021-05-01 12:24:42 -07:00
2024-04-18 03:50:20 -04:00
use App\Enums\ServerState;
2024-03-12 22:39:16 -04:00
use App\Jobs\Schedule\RunTaskJob;
2025-09-24 13:34:19 +02:00
use App\Models\Schedule;
use App\Models\Server;
use App\Models\Task;
2025-09-08 08:56:59 +02:00
use App\Repositories\Daemon\DaemonServerRepository;
2024-03-12 22:39:16 -04:00
use App\Tests\Integration\IntegrationTestCase;
2025-09-24 13:34:19 +02:00
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Http\Client\ConnectionException;
2025-09-24 13:34:19 +02:00
use Illuminate\Support\Facades\Bus;
use PHPUnit\Framework\Attributes\DataProvider;
2021-05-01 12:24:42 -07:00
class RunTaskJobTest extends IntegrationTestCase
{
/**
* An inactive job should not be run by the system.
*/
public function test_inactive_job_is_not_run(): void
2021-05-01 12:24:42 -07:00
{
$server = $this->createServerModel();
/** @var Schedule $schedule */
2021-05-01 12:24:42 -07:00
$schedule = Schedule::factory()->create([
'server_id' => $server->id,
'is_processing' => true,
'last_run_at' => null,
'is_active' => false,
]);
/** @var Task $task */
2021-05-01 12:24:42 -07:00
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'is_queued' => true]);
$job = new RunTaskJob($task);
2023-02-23 12:30:16 -07:00
Bus::dispatchSync($job);
2021-05-01 12:24:42 -07:00
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
$this->assertFalse($schedule->is_active);
2023-02-23 12:30:16 -07:00
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
2021-05-01 12:24:42 -07:00
}
public function test_job_with_invalid_action_throws_exception(): void
2021-05-01 12:24:42 -07:00
{
$server = $this->createServerModel();
/** @var Schedule $schedule */
2021-05-01 12:24:42 -07:00
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
/** @var Task $task */
2021-05-01 12:24:42 -07:00
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'action' => 'foobar']);
$job = new RunTaskJob($task);
2023-02-23 12:30:16 -07:00
$this->expectException(\InvalidArgumentException::class);
2021-05-01 12:24:42 -07:00
$this->expectExceptionMessage('Invalid task action provided: foobar');
2023-02-23 12:30:16 -07:00
Bus::dispatchSync($job);
2021-05-01 12:24:42 -07:00
}
#[DataProvider('isManualRunDataProvider')]
public function test_job_is_executed(bool $isManualRun): void
2021-05-01 12:24:42 -07:00
{
$server = $this->createServerModel();
/** @var Schedule $schedule */
2021-05-01 12:24:42 -07:00
$schedule = Schedule::factory()->create([
'server_id' => $server->id,
'is_active' => !$isManualRun,
'is_processing' => true,
'last_run_at' => null,
]);
/** @var Task $task */
2021-05-01 12:24:42 -07:00
$task = Task::factory()->create([
'schedule_id' => $schedule->id,
2025-11-24 14:42:47 +01:00
'action' => 'power',
2021-05-01 12:24:42 -07:00
'payload' => 'start',
'is_queued' => true,
'continue_on_failure' => false,
]);
2025-09-08 08:56:59 +02:00
$mock = \Mockery::mock(DaemonServerRepository::class);
$this->instance(DaemonServerRepository::class, $mock);
2021-05-01 12:24:42 -07:00
2023-02-23 12:30:16 -07:00
$mock->expects('setServer')->with(\Mockery::on(function ($value) use ($server) {
2021-05-01 12:24:42 -07:00
return $value instanceof Server && $value->id === $server->id;
}))->andReturnSelf();
2025-09-08 08:56:59 +02:00
$mock->expects('power')->with('start');
2021-05-01 12:24:42 -07:00
2023-02-23 12:30:16 -07:00
Bus::dispatchSync(new RunTaskJob($task, $isManualRun));
2021-05-01 12:24:42 -07:00
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
2023-02-23 12:30:16 -07:00
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
2021-05-01 12:24:42 -07:00
}
#[DataProvider('isManualRunDataProvider')]
public function test_exception_during_run_is_handled_correctly(bool $continueOnFailure): void
2021-05-01 12:24:42 -07:00
{
$server = $this->createServerModel();
/** @var Schedule $schedule */
2021-05-01 12:24:42 -07:00
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
/** @var Task $task */
2021-05-01 12:24:42 -07:00
$task = Task::factory()->create([
'schedule_id' => $schedule->id,
2025-11-24 14:42:47 +01:00
'action' => 'power',
2021-05-01 12:24:42 -07:00
'payload' => 'start',
'continue_on_failure' => $continueOnFailure,
]);
2025-09-08 08:56:59 +02:00
$mock = \Mockery::mock(DaemonServerRepository::class);
$this->instance(DaemonServerRepository::class, $mock);
2021-05-01 12:24:42 -07:00
2025-09-08 08:56:59 +02:00
$mock->expects('setServer->power')->andThrow(new ConnectionException());
2021-05-01 12:24:42 -07:00
if (!$continueOnFailure) {
$this->expectException(ConnectionException::class);
2021-05-01 12:24:42 -07:00
}
2023-02-23 12:30:16 -07:00
Bus::dispatchSync(new RunTaskJob($task));
2021-05-01 12:24:42 -07:00
if ($continueOnFailure) {
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
2023-02-23 12:30:16 -07:00
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
2021-05-01 12:24:42 -07:00
}
}
/**
* Test that a schedule is not executed if the server is suspended.
*/
public function test_task_is_not_run_if_server_is_suspended(): void
{
$server = $this->createServerModel([
2024-04-18 03:50:20 -04:00
'status' => ServerState::Suspended,
]);
$schedule = Schedule::factory()->for($server)->create([
'last_run_at' => Carbon::now()->subHour(),
]);
$task = Task::factory()->for($schedule)->create([
2025-11-24 14:42:47 +01:00
'action' => 'power',
'payload' => 'start',
]);
2023-02-23 12:30:16 -07:00
Bus::dispatchSync(new RunTaskJob($task));
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
2023-02-23 12:30:16 -07:00
$this->assertTrue(Carbon::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
}
2023-02-23 12:30:16 -07:00
public static function isManualRunDataProvider(): array
2021-05-01 12:24:42 -07:00
{
return [[true], [false]];
}
}