Files
pterodactyl-panel/tests/Integration/Api/Client/ClientApiIntegrationTestCase.php
T

83 lines
2.7 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Tests\Integration\Api\Client;
2026-10-05 18:10:18 -04:00
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use InvalidArgumentException;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Backup;
2021-01-23 12:09:16 -08:00
use Pterodactyl\Models\Database;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\DatabaseHost;
use Pterodactyl\Models\Location;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Node;
2020-06-28 14:41:22 -07:00
use Pterodactyl\Models\Schedule;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Task;
use Pterodactyl\Models\User;
use Pterodactyl\Tests\Integration\IntegrationTestCase;
2026-10-05 18:10:18 -04:00
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();
}
2021-01-19 20:11:00 -08:00
/**
* 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.
*
2026-10-05 18:10:18 -04:00
* @param Response $response
* @param Request $request
2021-01-19 20:11:00 -08:00
* @return \Illuminate\Testing\TestResponse
*/
2024-10-21 19:18:20 -06:00
protected function createTestResponse($response, $request)
2021-01-19 20:11:00 -08:00
{
2024-10-21 19:18:20 -06:00
return TestResponse::fromBaseResponse($response, $request);
2021-01-19 20:11:00 -08:00
}
2020-06-28 14:41:22 -07:00
/**
* Returns a link to the specific resource using the client API.
*/
2024-10-21 19:18:20 -06:00
protected function link(mixed $model, ?string $append = null): string
2020-06-28 14:41:22 -07:00
{
2026-10-05 18:10:18 -04:00
switch ($model::class) {
2020-06-28 14:41:22 -07:00
case Server::class:
2022-10-14 10:59:20 -06:00
$link = "/api/client/servers/$model->uuid";
2020-06-28 14:41:22 -07:00
break;
case Schedule::class:
2022-10-14 10:59:20 -06:00
$link = "/api/client/servers/{$model->server->uuid}/schedules/$model->id";
2020-06-28 14:41:22 -07:00
break;
case Task::class:
2022-10-14 10:59:20 -06:00
$link = "/api/client/servers/{$model->schedule->server->uuid}/schedules/{$model->schedule->id}/tasks/$model->id";
2020-06-28 14:41:22 -07:00
break;
2020-07-10 21:17:28 -07:00
case Allocation::class:
2022-10-14 10:59:20 -06:00
$link = "/api/client/servers/{$model->server->uuid}/network/allocations/$model->id";
2020-07-10 21:17:28 -07:00
break;
case Backup::class:
2022-10-14 10:59:20 -06:00
$link = "/api/client/servers/{$model->server->uuid}/backups/$model->uuid";
break;
default:
2026-10-05 18:10:18 -04:00
throw new InvalidArgumentException(sprintf('Cannot create link for Model of type %s', class_basename($model)));
2020-06-28 14:41:22 -07:00
}
2026-10-05 18:10:18 -04:00
return $link.($append ? '/'.mb_ltrim($append, '/') : '');
2020-06-28 13:50:07 -07:00
}
}