Testing

This package also have some testing utilities built on top of PHPUnit and Laravel's framework assertions.

Assertions

Simple assert that your API route is returning a proper JSON:API response:

$response = $this->getJson('/posts');

$response->assertJsonApi();

at

Assert the resource at position of the collection starting by 0.

use OpenSoutheners\LaravelApiable\Testing\AssertableJsonApi;

$response = $this->getJson('/posts');

$response->assertJsonApi(function (AssertableJsonApi $assert) {
  $assert->at(0)->hasAttribute('title', 'Hello world');
});

atRelation

Assert the related model.

use OpenSoutheners\LaravelApiable\Testing\AssertableJsonApi;

$response = $this->getJson('/posts');

$relatedComment = Comment::find(4);

$response->assertJsonApi(function (AssertableJsonApi $assert) use ($relatedComment) {
  $assert->at(0)->atRelation($relatedComment)->hasAttribute('content', 'Foo bar');
});

hasAttribute

Assert the resource has the specified attribute key and value.

hasNotAttribute

Assert the resource does not has the specified attribute key and value.

hasAttributes

Assert the resource has the specified attributes keys and values.

hasNotAttributes

Assert the resource does not has the specified attributes keys and values.

hasId

Assert the resource has the specified ID (or model key).

hasType

Assert the resource has the specified type.

hasAnyRelationships

Assert that the resource has any relationships with the specified resource type.

Second parameter is for assert that the response includes the relationship data at the included.

hasNotAnyRelationships

Assert that the resource doesn't have any relationships with the specified resource type.

Second parameter is for assert that the response doesn't includes the relationship data at the included.

hasRelationshipWith

Assert that the specific model resource is a relationship with the parent resource.

Second parameter is for assert that the response includes the relationship data at the included.

hasNotRelationshipWith

Assert that the specific model resource is not a relationship with the parent resource.

Second parameter is for assert that the response doesn't includes the relationship data at the included.

isCollection

Assert that the response is a collection (list of resources).

isResource

Last updated