Skip to content

Make JSON:API server configurable using Attributes #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Core/Attributes/ServeSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace LaravelJsonApi\Core\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class ServeSchema
{
/**
* @param array|class-string $schema
*/
public function __construct(
public array|string $schema,
) {}
}
11 changes: 10 additions & 1 deletion src/Core/Server/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
use LaravelJsonApi\Contracts\Schema\Container as SchemaContainerContract;
use LaravelJsonApi\Contracts\Server\Server as ServerContract;
use LaravelJsonApi\Contracts\Store\Store as StoreContract;
use LaravelJsonApi\Core\Attributes\ServeSchema;
use LaravelJsonApi\Core\Document\JsonApi;
use LaravelJsonApi\Core\Resources\Container as ResourceContainer;
use LaravelJsonApi\Core\Resources\Factory as ResourceFactory;
use LaravelJsonApi\Core\Schema\Container as SchemaContainer;
use LaravelJsonApi\Core\Store\Store;
use LaravelJsonApi\Core\Support\AppResolver;
use LogicException;
use ReflectionAttribute;
use ReflectionClass;

abstract class Server implements ServerContract
{
Expand Down Expand Up @@ -65,7 +68,13 @@ abstract class Server implements ServerContract
*
* @return array
*/
abstract protected function allSchemas(): array;
protected function allSchemas(): array
{
return (new Collection((new ReflectionClass(static::class))->getAttributes(ServeSchema::class)))
->map(fn (ReflectionAttribute $attribute): array => $attribute->getArguments())
->flatten()
->all();
}

/**
* Server constructor.
Expand Down
90 changes: 90 additions & 0 deletions tests/Unit/Server/ServerWithAttributesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/*
* Copyright 2024 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

declare(strict_types=1);

namespace LaravelJsonApi\Core\Tests\Unit\Server;

use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Contracts\Foundation\Application;
use LaravelJsonApi\Core\Server\ServerRepository;
use LaravelJsonApi\Core\Support\AppResolver;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class ServerWithAttributesTest extends TestCase
{
/**
* @var MockObject&Application
*/
private Application&MockObject $app;

/**
* @var MockObject&ConfigRepository
*/
private ConfigRepository&MockObject $config;

/**
* @var AppResolver
*/
private AppResolver $resolver;

/**
* @var ServerRepository
*/
private ServerRepository $repository;

/**
* @return void
*/
protected function setUp(): void
{
parent::setUp();

$this->app = $this->createMock(Application::class);
$this->config = $this->createMock(ConfigRepository::class);
$this->resolver = new AppResolver(fn() => $this->app);
$this->repository = new ServerRepository($this->resolver);
}

/**
* @return void
*/
public function test(): void
{
$expected = $this->willMakeServer($name = 'v1');
$actual = $this->repository->server($name);

$this->assertInstanceOf(TestServerWithServeSchemaAttribute::class, $actual);
$this->assertEquals($expected, $actual);
$this->assertTrue($actual->schemas()->existsForModel(TestSchema::$model));
}


/**
* @param string $name
* @param string $class
* @return TestServer
*/
private function willMakeServer(string $name, string $class = TestServerWithServeSchemaAttribute::class): TestServerWithServeSchemaAttribute
{
$this->app
->method('make')
->with(ConfigRepository::class)
->willReturn($this->config);

$this->config
->expects($this->once())
->method('get')
->with("jsonapi.servers.{$name}")
->willReturn($class);

return new TestServerWithServeSchemaAttribute($this->resolver, $name);
}
}
20 changes: 20 additions & 0 deletions tests/Unit/Server/TestSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace LaravelJsonApi\Core\Tests\Unit\Server;

use LaravelJsonApi\Core\Schema\Schema;

class TestSchema extends Schema
{
public static string $model = 'test';

/**
* Get the resource fields.
*/
public function fields(): array
{
return [];
}
}
27 changes: 27 additions & 0 deletions tests/Unit/Server/TestServerWithServeSchemaAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/*
* Copyright 2024 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

declare(strict_types=1);

namespace LaravelJsonApi\Core\Tests\Unit\Server;

use LaravelJsonApi\Core\Attributes\ServeSchema;
use LaravelJsonApi\Core\Server\Server as ServerContract;

#[ServeSchema(TestSchema::class)]
class TestServerWithServeSchemaAttribute extends ServerContract
{
/**
* @return void
*/
public function serving(): void
{
// no-op
}
}