Attempt to fix Fractal object type being null

This commit is contained in:
Matthew Penner 2022-12-14 19:13:00 -07:00
parent 507a802dec
commit 8fff0846a0
No known key found for this signature in database
3 changed files with 47 additions and 36 deletions

View file

@ -6,17 +6,6 @@ use League\Fractal\Serializer\ArraySerializer;
class PterodactylSerializer extends ArraySerializer
{
/**
* Serialize an item.
*/
public function item(?string $resourceKey, array $data): array
{
return [
'object' => $resourceKey,
'attributes' => $data,
];
}
/**
* Serialize a collection.
*/
@ -33,6 +22,17 @@ class PterodactylSerializer extends ArraySerializer
];
}
/**
* Serialize an item.
*/
public function item(?string $resourceKey, array $data): array
{
return [
'object' => $resourceKey,
'attributes' => $data,
];
}
/**
* Serialize a null resource.
*/

View file

@ -3,8 +3,8 @@
namespace Pterodactyl\Extensions\Spatie\Fractalistic;
use League\Fractal\Scope;
use League\Fractal\TransformerAbstract;
use Spatie\Fractal\Fractal as SpatieFractal;
use Pterodactyl\Transformers\Api\Transformer;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use Pterodactyl\Extensions\League\Fractal\Serializers\PterodactylSerializer;
@ -32,11 +32,8 @@ class Fractal extends SpatieFractal
// If the resource name is not set attempt to pull it off the transformer
// itself and set it automatically.
if (
is_null($this->resourceName)
&& $this->transformer instanceof TransformerAbstract
&& method_exists($this->transformer, 'getResourceName')
) {
$class = is_string($this->transformer) ? new $this->transformer() : $this->transformer;
if (is_null($this->resourceName) && $class instanceof Transformer) {
$this->resourceName = $this->transformer->getResourceName();
}

View file

@ -1,21 +1,33 @@
<?php
namespace Pterodactyl\Tests\Integration\Api\Application\Nests;
namespace Pterodactyl\Tests\Integration\Api\Application\Eggs;
use Illuminate\Support\Arr;
use Pterodactyl\Models\Egg;
use Illuminate\Http\Response;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Transformers\Api\Application\EggTransformer;
use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase;
class EggControllerTest extends ApplicationApiIntegrationTestCase
{
private EggRepositoryInterface $repository;
/**
* Setup tests.
*/
public function setUp(): void
{
parent::setUp();
$this->repository = $this->app->make(EggRepositoryInterface::class);
}
/**
* Test that all the eggs belonging to a given nest can be returned.
*/
public function testListAllEggsInNest()
{
$eggs = Egg::query()->where('nest_id', 1)->get();
$eggs = $this->repository->findWhere([['nest_id', '=', 1]]);
$response = $this->getJson('/api/application/nests/' . $eggs->first()->nest_id . '/eggs');
$response->assertStatus(Response::HTTP_OK);
@ -32,7 +44,6 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
'files' => [],
'startup' => ['done'],
'stop',
'logs' => [],
'extends',
],
],
@ -44,12 +55,12 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
$egg = $eggs->where('id', '=', $datum['attributes']['id'])->first();
$expected = json_encode(Arr::sortRecursive($datum['attributes']));
$actual = json_encode(Arr::sortRecursive($this->getTransformer(EggTransformer::class)->transform($egg)));
$actual = json_encode(Arr::sortRecursive((new EggTransformer())->transform($egg)));
$this->assertSame(
$this->assertJsonStringEqualsJsonString(
$expected,
$actual,
'Unable to find JSON fragment: ' . PHP_EOL . PHP_EOL . "[$expected]" . PHP_EOL . PHP_EOL . 'within' . PHP_EOL . PHP_EOL . "[$actual]."
'Unable to find JSON fragment: ' . PHP_EOL . PHP_EOL . "[{$expected}]" . PHP_EOL . PHP_EOL . 'within' . PHP_EOL . PHP_EOL . "[{$actual}]."
);
}
}
@ -59,9 +70,9 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
*/
public function testReturnSingleEgg()
{
$egg = Egg::query()->findOrFail(1);
$egg = $this->repository->find(1);
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs/' . $egg->id);
$response = $this->getJson('/api/application/eggs/' . $egg->id);
$response->assertStatus(Response::HTTP_OK);
$response->assertJsonStructure([
'object',
@ -72,7 +83,7 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
$response->assertJson([
'object' => 'egg',
'attributes' => $this->getTransformer(EggTransformer::class)->transform($egg),
'attributes' => json_decode(json_encode((new EggTransformer())->transform($egg)), true),
], true);
}
@ -81,9 +92,9 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
*/
public function testReturnSingleEggWithRelationships()
{
$egg = Egg::query()->findOrFail(1);
$egg = $this->repository->find(1);
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs/' . $egg->id . '?include=servers,variables,nest');
$response = $this->getJson('/api/application/eggs/' . $egg->id . '?include=servers,variables,nest');
$response->assertStatus(Response::HTTP_OK);
$response->assertJsonStructure([
'object',
@ -102,9 +113,7 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
*/
public function testGetMissingEgg()
{
$egg = Egg::query()->findOrFail(1);
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs/0');
$response = $this->getJson('/api/application/eggs/nil');
$this->assertNotFoundJson($response);
}
@ -114,10 +123,15 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
*/
public function testErrorReturnedIfNoPermission()
{
$egg = Egg::query()->findOrFail(1);
$this->createNewDefaultApiKey($this->getApiUser(), ['r_eggs' => 0]);
$this->markTestSkipped('todo: implement proper admin api key permissions system');
}
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs');
$this->assertAccessDeniedJson($response);
/**
* Test that a nests's existence is not exposed unless an API key has permission
* to access the resource.
*/
public function testResourceIsNotExposedWithoutPermissions()
{
$this->markTestSkipped('todo: implement proper admin api key permissions system');
}
}