2020-06-27 12:04:41 -07:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2020-06-27 12:04:41 -07:00
|
|
|
namespace Pterodactyl\Tests\Integration\Api\Client;
|
|
|
|
|
|
2026-10-11 14:33:37 -04:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
use Pterodactyl\Models\Allocation;
|
2021-01-19 21:20:55 -08:00
|
|
|
use Pterodactyl\Models\Backup;
|
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;
|
2020-06-27 12:04:41 -07:00
|
|
|
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Pterodactyl\Tests\Integration\TestResponse;
|
2020-06-27 12:04:41 -07:00
|
|
|
|
|
|
|
|
abstract class ClientApiIntegrationTestCase extends IntegrationTestCase
|
|
|
|
|
{
|
2026-10-11 14:33:37 -04:00
|
|
|
use DatabaseTransactions;
|
2020-06-27 12:04:41 -07:00
|
|
|
|
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;
|
2021-07-11 12:15:39 -07:00
|
|
|
case Backup::class:
|
2022-10-14 10:59:20 -06:00
|
|
|
$link = "/api/client/servers/{$model->server->uuid}/backups/$model->uuid";
|
2021-07-11 12:15:39 -07:00
|
|
|
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
|
|
|
}
|
2020-06-27 12:04:41 -07:00
|
|
|
}
|