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 class PterodactylSerializer extends ArraySerializer
{ {
/**
* Serialize an item.
*/
public function item(?string $resourceKey, array $data): array
{
return [
'object' => $resourceKey,
'attributes' => $data,
];
}
/** /**
* Serialize a collection. * 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. * Serialize a null resource.
*/ */

View file

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

View file

@ -1,21 +1,33 @@
<?php <?php
namespace Pterodactyl\Tests\Integration\Api\Application\Nests; namespace Pterodactyl\Tests\Integration\Api\Application\Eggs;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Pterodactyl\Models\Egg;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Transformers\Api\Application\EggTransformer; use Pterodactyl\Transformers\Api\Application\EggTransformer;
use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase; use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase;
class EggControllerTest extends 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. * Test that all the eggs belonging to a given nest can be returned.
*/ */
public function testListAllEggsInNest() 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 = $this->getJson('/api/application/nests/' . $eggs->first()->nest_id . '/eggs');
$response->assertStatus(Response::HTTP_OK); $response->assertStatus(Response::HTTP_OK);
@ -32,7 +44,6 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
'files' => [], 'files' => [],
'startup' => ['done'], 'startup' => ['done'],
'stop', 'stop',
'logs' => [],
'extends', 'extends',
], ],
], ],
@ -44,12 +55,12 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
$egg = $eggs->where('id', '=', $datum['attributes']['id'])->first(); $egg = $eggs->where('id', '=', $datum['attributes']['id'])->first();
$expected = json_encode(Arr::sortRecursive($datum['attributes'])); $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, $expected,
$actual, $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() 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->assertStatus(Response::HTTP_OK);
$response->assertJsonStructure([ $response->assertJsonStructure([
'object', 'object',
@ -72,7 +83,7 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
$response->assertJson([ $response->assertJson([
'object' => 'egg', 'object' => 'egg',
'attributes' => $this->getTransformer(EggTransformer::class)->transform($egg), 'attributes' => json_decode(json_encode((new EggTransformer())->transform($egg)), true),
], true); ], true);
} }
@ -81,9 +92,9 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
*/ */
public function testReturnSingleEggWithRelationships() 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->assertStatus(Response::HTTP_OK);
$response->assertJsonStructure([ $response->assertJsonStructure([
'object', 'object',
@ -102,9 +113,7 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
*/ */
public function testGetMissingEgg() public function testGetMissingEgg()
{ {
$egg = Egg::query()->findOrFail(1); $response = $this->getJson('/api/application/eggs/nil');
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs/0');
$this->assertNotFoundJson($response); $this->assertNotFoundJson($response);
} }
@ -114,10 +123,15 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
*/ */
public function testErrorReturnedIfNoPermission() public function testErrorReturnedIfNoPermission()
{ {
$egg = Egg::query()->findOrFail(1); $this->markTestSkipped('todo: implement proper admin api key permissions system');
$this->createNewDefaultApiKey($this->getApiUser(), ['r_eggs' => 0]); }
$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');
} }
} }