Files
panel/tests/Integration/Api/Client/ClientApiIntegrationTestCase.php

72 lines
2.6 KiB
PHP
Raw Normal View History

<?php
2024-03-12 22:39:16 -04:00
namespace App\Tests\Integration\Api\Client;
2025-09-24 13:34:19 +02:00
use App\Models\Allocation;
2024-03-12 22:39:16 -04:00
use App\Models\Backup;
use App\Models\Schedule;
2025-09-24 13:34:19 +02:00
use App\Models\Server;
use App\Models\Task;
2024-03-12 22:39:16 -04:00
use App\Tests\Integration\IntegrationTestCase;
use App\Transformers\Api\Client\BaseClientTransformer;
2025-09-24 13:34:19 +02:00
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
abstract class ClientApiIntegrationTestCase extends IntegrationTestCase
{
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.
2024-03-22 21:34:19 -04:00
protected function createTestResponse($response, $request): \Illuminate\Testing\TestResponse
2021-01-19 20:11:00 -08:00
{
return TestResponse::fromBaseResponse($response);
}
2020-06-28 14:41:22 -07:00
/**
* Returns a link to the specific resource using the client API.
*/
protected function link(mixed $model, ?string $append = null): string
2020-06-28 14:41:22 -07:00
{
switch (get_class($model)) {
case Server::class:
$link = "/api/client/servers/$model->uuid";
2020-06-28 14:41:22 -07:00
break;
case Schedule::class:
$link = "/api/client/servers/{$model->server->uuid}/schedules/$model->id";
2020-06-28 14:41:22 -07:00
break;
case Task::class:
$link = "/api/client/servers/{$model->schedule->server->uuid}/schedules/{$model->schedule->id}/tasks/$model->id";
2020-06-28 14:41:22 -07:00
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:
2023-02-23 12:30:16 -07:00
throw new \InvalidArgumentException(sprintf('Cannot create link for Model of type %s', class_basename($model)));
2020-06-28 14:41:22 -07:00
}
return $link . ($append ? '/' . ltrim($append, '/') : '');
2020-06-28 14:41:22 -07:00
}
2020-06-28 13:50:07 -07: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.
*/
protected function assertJsonTransformedWith(array $data, Model $model): void
2020-06-28 13:50:07 -07:00
{
2023-02-23 12:30:16 -07:00
$reflect = new \ReflectionClass($model);
2024-03-12 22:39:16 -04:00
$transformer = sprintf('\\App\\Transformers\\Api\\Client\\%sTransformer', $reflect->getShortName());
2020-06-28 13:50:07 -07:00
2021-01-23 12:33:34 -08:00
$transformer = new $transformer();
2020-06-28 13:50:07 -07:00
$this->assertInstanceOf(BaseClientTransformer::class, $transformer);
$this->assertSame(
$transformer->transform($model),
Collection::make($data)->except(['relationships'])->toArray()
);
}
}