Tooling

Checking resources

Catch a resource that cannot be described before a client does, in CI and in your tests.

jsonapi:check

Terminal
php artisan jsonapi:check                  # every resource of definitions.paths (the app directory by default)
php artisan jsonapi:check PostResource     # some resources
php artisan jsonapi:check --strict         # warnings fail too: use it in CI
php artisan jsonapi:check --revision=Labels

It compiles the definition of every resource at every revision and prints its findings ([error] [Labels] code (subject): …). It fails on an error, and on a warning with --strict.

What it finds, among others:

  • a plain value or a member without name in a hook, a hook throwing on the probe (an if on the model);
  • a relationship whose resource cannot be guessed;
  • an attribute or relationship named id or type;
  • a self route of #[JsonApiRoutes] that does not exist;
  • two relationships loading the same relation with different constraints.

Every code is in the diagnostics reference.

In your tests

tests/Feature/ResourcesTest.php
use Ark4ne\JsonApi\Testing\ResourceAssertions;

public function test_the_post_resource_is_well_described(): void
{
    ResourceAssertions::assertValid(PostResource::class, strict: true);
}
AssertionChecks
assertValid($class, strict: false, revision: null)the definition, as jsonapi:check does, at the head or at a revision
assertValidAtEveryRevision($class)every revision
definitionAt($class, $revision)returns the definition of a revision, for snapshot tests

ResourceAssertions needs PHPUnit, which the application already has in its dev dependencies.