2020-06-27 19:04:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Api\Client;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Node;
|
2020-06-28 21:41:22 +00:00
|
|
|
use Pterodactyl\Models\Task;
|
2020-06-27 19:04:41 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Pterodactyl\Models\Model;
|
2021-01-20 05:20:55 +00:00
|
|
|
use Pterodactyl\Models\Backup;
|
2021-01-23 20:09:16 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Models\Database;
|
2020-06-27 19:04:41 +00:00
|
|
|
use Pterodactyl\Models\Location;
|
2020-06-28 21:41:22 +00:00
|
|
|
use Pterodactyl\Models\Schedule;
|
2020-06-28 20:50:07 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2020-07-11 04:17:28 +00:00
|
|
|
use Pterodactyl\Models\Allocation;
|
2021-01-20 05:20:55 +00:00
|
|
|
use Pterodactyl\Models\DatabaseHost;
|
2022-12-15 02:53:07 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Transformer;
|
2021-01-20 04:11:00 +00:00
|
|
|
use Pterodactyl\Tests\Integration\TestResponse;
|
2020-06-27 19:04:41 +00:00
|
|
|
use Pterodactyl\Tests\Integration\IntegrationTestCase;
|
2022-10-14 16:59:20 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model as EloquentModel;
|
2020-06-27 19:04:41 +00:00
|
|
|
|
|
|
|
abstract class ClientApiIntegrationTestCase extends IntegrationTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Cleanup after running tests.
|
|
|
|
*/
|
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
2021-01-20 05:20:55 +00:00
|
|
|
Database::query()->forceDelete();
|
|
|
|
DatabaseHost::query()->forceDelete();
|
|
|
|
Backup::query()->forceDelete();
|
2020-06-27 19:04:41 +00:00
|
|
|
Server::query()->forceDelete();
|
|
|
|
Node::query()->forceDelete();
|
|
|
|
Location::query()->forceDelete();
|
|
|
|
User::query()->forceDelete();
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2021-01-20 04:11:00 +00: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.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Response $response
|
2021-01-23 20:33:34 +00:00
|
|
|
*
|
2021-01-20 04:11:00 +00:00
|
|
|
* @return \Illuminate\Testing\TestResponse
|
|
|
|
*/
|
|
|
|
protected function createTestResponse($response)
|
|
|
|
{
|
|
|
|
return TestResponse::fromBaseResponse($response);
|
|
|
|
}
|
|
|
|
|
2020-06-28 21:41:22 +00:00
|
|
|
/**
|
|
|
|
* Returns a link to the specific resource using the client API.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function link(mixed $model, string $append = null): string
|
2020-06-28 21:41:22 +00:00
|
|
|
{
|
|
|
|
switch (get_class($model)) {
|
|
|
|
case Server::class:
|
2022-10-14 16:59:20 +00:00
|
|
|
$link = "/api/client/servers/$model->uuid";
|
2020-06-28 21:41:22 +00:00
|
|
|
break;
|
|
|
|
case Schedule::class:
|
2022-10-14 16:59:20 +00:00
|
|
|
$link = "/api/client/servers/{$model->server->uuid}/schedules/$model->id";
|
2020-06-28 21:41:22 +00:00
|
|
|
break;
|
|
|
|
case Task::class:
|
2022-10-14 16:59:20 +00:00
|
|
|
$link = "/api/client/servers/{$model->schedule->server->uuid}/schedules/{$model->schedule->id}/tasks/$model->id";
|
2020-06-28 21:41:22 +00:00
|
|
|
break;
|
2020-07-11 04:17:28 +00:00
|
|
|
case Allocation::class:
|
2022-10-14 16:59:20 +00:00
|
|
|
$link = "/api/client/servers/{$model->server->uuid}/network/allocations/$model->id";
|
2020-07-11 04:17:28 +00:00
|
|
|
break;
|
2021-07-11 19:15:39 +00:00
|
|
|
case Backup::class:
|
2022-10-14 16:59:20 +00:00
|
|
|
$link = "/api/client/servers/{$model->server->uuid}/backups/$model->uuid";
|
2021-07-11 19:15:39 +00:00
|
|
|
break;
|
|
|
|
default:
|
2022-11-29 17:53:59 +00:00
|
|
|
throw new \InvalidArgumentException(sprintf('Cannot create link for Model of type %s', class_basename($model)));
|
2020-06-28 21:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $link . ($append ? '/' . ltrim($append, '/') : '');
|
|
|
|
}
|
|
|
|
|
2020-06-28 20:50:07 +00:00
|
|
|
/**
|
|
|
|
* Asserts that the data passed through matches the output of the data from the transformer. This
|
|
|
|
* will remove the "relationships" key when performing the comparison.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
protected function assertJsonTransformedWith(array $data, Model|EloquentModel $model)
|
2020-06-28 20:50:07 +00:00
|
|
|
{
|
2022-11-29 17:53:59 +00:00
|
|
|
$reflect = new \ReflectionClass($model);
|
2020-06-28 20:50:07 +00:00
|
|
|
$transformer = sprintf('\\Pterodactyl\\Transformers\\Api\\Client\\%sTransformer', $reflect->getShortName());
|
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
$transformer = new $transformer();
|
2022-12-15 02:53:07 +00:00
|
|
|
$this->assertInstanceOf(Transformer::class, $transformer);
|
2020-06-28 20:50:07 +00:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$transformer->transform($model),
|
|
|
|
Collection::make($data)->except(['relationships'])->toArray()
|
|
|
|
);
|
|
|
|
}
|
2020-06-27 19:04:41 +00:00
|
|
|
}
|