Lance Pioch f8ad9a1805
Use PestPHP (#962)
* Install Pest

* Don’t use bootstrap file anymore

* Fix comment

* Think this is needed

* Reset this

* Switch dataproviders to attributes

* Fix these

* Support in memory databases

* Fix this migration

* Switch this back for now

* Add missing import

* Truncate and reseed database

* These are replaced now

* Switch ci to use pest
2025-01-30 16:39:17 -05:00

174 lines
5.7 KiB
PHP

<?php
namespace App\Tests\Integration\Jobs\Schedule;
use App\Enums\ServerState;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use App\Models\Task;
use App\Models\Server;
use App\Models\Schedule;
use Illuminate\Support\Facades\Bus;
use App\Jobs\Schedule\RunTaskJob;
use App\Tests\Integration\IntegrationTestCase;
use App\Repositories\Daemon\DaemonPowerRepository;
use Illuminate\Http\Client\ConnectionException;
use PHPUnit\Framework\Attributes\DataProvider;
class RunTaskJobTest extends IntegrationTestCase
{
/**
* An inactive job should not be run by the system.
*/
public function testInactiveJobIsNotRun(): void
{
$server = $this->createServerModel();
/** @var \App\Models\Schedule $schedule */
$schedule = Schedule::factory()->create([
'server_id' => $server->id,
'is_processing' => true,
'last_run_at' => null,
'is_active' => false,
]);
/** @var \App\Models\Task $task */
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'is_queued' => true]);
$job = new RunTaskJob($task);
Bus::dispatchSync($job);
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
$this->assertFalse($schedule->is_active);
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
}
public function testJobWithInvalidActionThrowsException(): void
{
$server = $this->createServerModel();
/** @var \App\Models\Schedule $schedule */
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
/** @var \App\Models\Task $task */
$task = Task::factory()->create(['schedule_id' => $schedule->id, 'action' => 'foobar']);
$job = new RunTaskJob($task);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid task action provided: foobar');
Bus::dispatchSync($job);
}
#[DataProvider('isManualRunDataProvider')]
public function testJobIsExecuted(bool $isManualRun): void
{
$server = $this->createServerModel();
/** @var \App\Models\Schedule $schedule */
$schedule = Schedule::factory()->create([
'server_id' => $server->id,
'is_active' => !$isManualRun,
'is_processing' => true,
'last_run_at' => null,
]);
/** @var \App\Models\Task $task */
$task = Task::factory()->create([
'schedule_id' => $schedule->id,
'action' => Task::ACTION_POWER,
'payload' => 'start',
'is_queued' => true,
'continue_on_failure' => false,
]);
$mock = \Mockery::mock(DaemonPowerRepository::class);
$this->instance(DaemonPowerRepository::class, $mock);
$mock->expects('setServer')->with(\Mockery::on(function ($value) use ($server) {
return $value instanceof Server && $value->id === $server->id;
}))->andReturnSelf();
$mock->expects('send')->with('start');
Bus::dispatchSync(new RunTaskJob($task, $isManualRun));
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
}
#[DataProvider('isManualRunDataProvider')]
public function testExceptionDuringRunIsHandledCorrectly(bool $continueOnFailure): void
{
$server = $this->createServerModel();
/** @var \App\Models\Schedule $schedule */
$schedule = Schedule::factory()->create(['server_id' => $server->id]);
/** @var \App\Models\Task $task */
$task = Task::factory()->create([
'schedule_id' => $schedule->id,
'action' => Task::ACTION_POWER,
'payload' => 'start',
'continue_on_failure' => $continueOnFailure,
]);
$mock = \Mockery::mock(DaemonPowerRepository::class);
$this->instance(DaemonPowerRepository::class, $mock);
$mock->expects('setServer->send')->andThrow(new ConnectionException());
if (!$continueOnFailure) {
$this->expectException(ConnectionException::class);
}
Bus::dispatchSync(new RunTaskJob($task));
if ($continueOnFailure) {
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
$this->assertTrue(CarbonImmutable::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
}
}
/**
* Test that a schedule is not executed if the server is suspended.
*/
public function testTaskIsNotRunIfServerIsSuspended(): void
{
$server = $this->createServerModel([
'status' => ServerState::Suspended,
]);
$schedule = Schedule::factory()->for($server)->create([
'last_run_at' => Carbon::now()->subHour(),
]);
$task = Task::factory()->for($schedule)->create([
'action' => Task::ACTION_POWER,
'payload' => 'start',
]);
Bus::dispatchSync(new RunTaskJob($task));
$task->refresh();
$schedule->refresh();
$this->assertFalse($task->is_queued);
$this->assertFalse($schedule->is_processing);
$this->assertTrue(Carbon::now()->isSameAs(\DateTimeInterface::ATOM, $schedule->last_run_at));
}
public static function isManualRunDataProvider(): array
{
return [[true], [false]];
}
}