Files
pterodactyl-panel/tests/Integration/Api/Client/ClientApiIntegrationTestCase.php
T
6c0413e39b Start 2.0 development
Co-authored-by: Anthony <anthony@infraly.co>
Co-authored-by: Sky Mulley <daniel.mulley@outlook.com>
2026-10-05 18:10:18 -04:00

83 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Pterodactyl\Tests\Integration\Api\Client;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use InvalidArgumentException;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Backup;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\DatabaseHost;
use Pterodactyl\Models\Location;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Schedule;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Task;
use Pterodactyl\Models\User;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
use Pterodactyl\Tests\Integration\TestResponse;
abstract class ClientApiIntegrationTestCase extends IntegrationTestCase
{
/**
* Cleanup after running tests.
*/
protected function tearDown(): void
{
Database::query()->forceDelete();
DatabaseHost::query()->forceDelete();
Backup::query()->forceDelete();
Server::query()->forceDelete();
Node::query()->forceDelete();
Location::query()->forceDelete();
User::query()->forceDelete();
parent::tearDown();
}
/**
* Override the default createTestResponse from Illuminate so that we can
* just dump 500-level errors to the screen in the tests without having
* to keep re-assigning variables.
*
* @param Response $response
* @param Request $request
* @return \Illuminate\Testing\TestResponse
*/
protected function createTestResponse($response, $request)
{
return TestResponse::fromBaseResponse($response, $request);
}
/**
* Returns a link to the specific resource using the client API.
*/
protected function link(mixed $model, ?string $append = null): string
{
switch ($model::class) {
case Server::class:
$link = "/api/client/servers/$model->uuid";
break;
case Schedule::class:
$link = "/api/client/servers/{$model->server->uuid}/schedules/$model->id";
break;
case Task::class:
$link = "/api/client/servers/{$model->schedule->server->uuid}/schedules/{$model->schedule->id}/tasks/$model->id";
break;
case Allocation::class:
$link = "/api/client/servers/{$model->server->uuid}/network/allocations/$model->id";
break;
case Backup::class:
$link = "/api/client/servers/{$model->server->uuid}/backups/$model->uuid";
break;
default:
throw new InvalidArgumentException(sprintf('Cannot create link for Model of type %s', class_basename($model)));
}
return $link.($append ? '/'.mb_ltrim($append, '/') : '');
}
}