code
stringlengths
31
1.39M
docstring
stringlengths
23
16.8k
func_name
stringlengths
1
126
language
stringclasses
1 value
repo
stringlengths
7
63
path
stringlengths
7
166
url
stringlengths
50
220
license
stringclasses
7 values
public function testNotAcceptable() { $expected = ['message' => "The requested resource is capable of generating only content not acceptable " . "according to the Accept headers sent in the request." ]; $this->get('/api/v1/posts', ['Accept' => 'application/json']) ->assertStatus(406) ->assertExactJson($expected); }
If we request a content type that is not in our codec configuration, we expect a 406 response.
testNotAcceptable
php
cloudcreativity/laravel-json-api
tests/lib/Integration/ContentNegotiation/DefaultTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/ContentNegotiation/DefaultTest.php
Apache-2.0
public function testCanChangeMediaType1() { app('config')->set('json-api-v1.encoding', [ 'application/json', ]); $this->get('/api/v1/posts', ['Accept' => 'application/json']) ->assertStatus(200) ->assertHeader('Content-Type', 'application/json'); }
The codec configuration can be changed.
testCanChangeMediaType1
php
cloudcreativity/laravel-json-api
tests/lib/Integration/ContentNegotiation/DefaultTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/ContentNegotiation/DefaultTest.php
Apache-2.0
public function testCanChangeMediaType2() { app('config')->set('json-api-v1.encoding', [ 'application/json', ]); $this->get('/api/v1/posts', ['Accept' => 'application/vnd.api+json']) ->assertStatus(406); }
Not including the JSON API media type in our configuration results in a 406 response
testCanChangeMediaType2
php
cloudcreativity/laravel-json-api
tests/lib/Integration/ContentNegotiation/DefaultTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/ContentNegotiation/DefaultTest.php
Apache-2.0
public function testUpdateSyncsRelatedResources(): void { /** @var User $user */ $user = factory(User::class)->create(); $user->roles()->saveMany($existing = factory(Role::class, 2)->create()); $roles = factory(Role::class, 2)->create(); $expected = $roles->merge([$existing[0]]); $data = [ 'type' => 'users', 'id' => (string) $user->getRouteKey(), 'relationships' => [ 'roles' => [ 'data' => $expected->map(function (Role $role) { return ['type' => 'roles', 'id' => (string) $role->getRouteKey()]; })->sortBy('id')->values()->all(), ], ], ]; $response = $this ->jsonApi() ->includePaths('roles') ->withData($data) ->patch(url('/api/v1/users', $user)); $response->assertFetchedOne($data); $this->assertDatabaseCount('role_user', count($expected)); foreach ($expected as $role) { $this->assertDatabaseHas('role_user', [ 'user_id' => $user->getKey(), 'role_id' => $role->getKey(), ]); } }
In this test we keep one existing role, and add two new ones.
testUpdateSyncsRelatedResources
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/BelongsToManyTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/BelongsToManyTest.php
Apache-2.0
public function testInvalidReplace() { $post = factory(Post::class)->create(); $country = factory(Country::class)->create(); $data = ['type' => 'countries', 'id' => (string) $country->getRouteKey()]; $expected = [ 'status' => '422', 'detail' => 'The author field must be a to-one relationship containing users resources.', 'source' => [ 'pointer' => '/data', ], ]; $response = $this ->jsonApi() ->withData($data) ->patch(url('/api/v1/posts', [$post, 'relationships', 'author'])); $response->assertErrorStatus($expected); }
@see https://github.com/cloudcreativity/laravel-json-api/issues/280
testInvalidReplace
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/BelongsToTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/BelongsToTest.php
Apache-2.0
public function test() { /** @var Video $video */ $video = factory(Video::class)->create(); $data = [ 'type' => 'videos', 'id' => $video->getKey(), 'attributes' => [ 'url' => 'http://www.example.com', 'title' => 'My Video', 'description' => 'This is my video.', ], ]; $expected = $data; $expected['attributes']['url'] = $video->url; $response = $this ->jsonApi() ->withData($data) ->patch(url('/api/v1/videos', $video)); $response->assertFetchedOne($expected); }
An adapter must be allowed to 'guard' some fields - i.e. prevent them from being filled to the model. The video adapter in our dummy app is set to guard the `url` field if the model already exists.
test
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/GuardedAttributesTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/GuardedAttributesTest.php
Apache-2.0
private function assertUserIs(Country $country, User $user) { $this->assertUsersAre($country, [$user]); }
@param $country @param $user @return void
assertUserIs
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasManyTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasManyTest.php
Apache-2.0
private function assertUsersAre(Country $country, $users) { $this->assertSame(count($users), $country->users()->count()); /** @var User $user */ foreach ($users as $user) { $this->assertDatabaseHas('users', [ 'id' => $user->getKey(), 'country_id' => $country->getKey(), ]); } }
@param Country $country @param iterable $users @return void
assertUsersAre
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasManyTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasManyTest.php
Apache-2.0
public function testCreateWithNull() { /** @var User $user */ $user = factory(User::class)->make(); $data = [ 'type' => 'users', 'attributes' => [ 'name' => $user->name, 'email' => $user->email, 'password' => 'secret', // @see https://github.com/cloudcreativity/laravel-json-api/issues/262 'passwordConfirmation' => 'secret', ], 'relationships' => [ 'phone' => [ 'data' => null, ], ], ]; $expected = $data; unset($expected['attributes']['password'], $expected['attributes']['passwordConfirmation']); $response = $this ->jsonApi() ->withData($data) ->includePaths('phone') ->post('/api/v1/users'); $id = $response ->assertCreatedWithServerId(url('/api/v1/users'), $expected) ->id(); $this->assertNotNull($refreshed = User::find($id)); $this->assertNull($refreshed->phone); }
We can create a user resource providing `null` as the phone relationship.
testCreateWithNull
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasOneTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasOneTest.php
Apache-2.0
public function testCreatePasswordNotConfirmed(string $field, $value): void { /** @var User $user */ $user = factory(User::class)->make(); $data = ResourceObject::create([ 'type' => 'users', 'attributes' => [ 'name' => $user->name, 'email' => $user->email, 'password' => 'secret', 'passwordConfirmation' => 'secret', ], 'relationships' => [ 'phone' => [ 'data' => null, ], ], ])->replace($field, $value); $expected = [ 'status' => '422', 'source' => [ 'pointer' => '/data/attributes/passwordConfirmation', ], ]; $response = $this ->jsonApi() ->withData($data) ->includePaths('phone') ->post('/api/v1/users'); $response->assertErrorStatus($expected); }
@param string $field @param $value @dataProvider confirmationProvider @see https://github.com/cloudcreativity/laravel-json-api/issues/262
testCreatePasswordNotConfirmed
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasOneTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasOneTest.php
Apache-2.0
public function testCreateWithRelated() { /** @var Phone $phone */ $phone = factory(Phone::class)->create(); /** @var User $user */ $user = factory(User::class)->make(); $data = [ 'type' => 'users', 'attributes' => [ 'name' => $user->name, 'email' => $user->email, 'password' => 'secret', 'passwordConfirmation' => 'secret', ], 'relationships' => [ 'phone' => [ 'data' => [ 'type' => 'phones', 'id' => (string) $phone->getKey(), ], ], ], ]; $expected = $data; unset($expected['attributes']['password'], $expected['attributes']['passwordConfirmation']); $response = $this ->jsonApi() ->withData($data) ->includePaths('phone') ->post('/api/v1/users'); $id = $response ->assertCreatedWithServerId(url('/api/v1/users'), $expected) ->id(); $this->assertDatabaseHas('phones', [ 'id' => $phone->getKey(), 'user_id' => $id, ]); }
We can create a user resource providing a related phone.
testCreateWithRelated
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasOneTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasOneTest.php
Apache-2.0
public function testUpdateReplacesRelationshipWithNull() { /** @var Phone $phone */ $phone = factory(Phone::class)->states('user')->create(); $data = [ 'type' => 'users', 'id' => (string) $phone->user_id, 'relationships' => [ 'phone' => [ 'data' => null, ], ], ]; $response = $this ->jsonApi() ->withData($data) ->includePaths('phone') ->patch(url('/api/v1/users', $phone->user_id)); $response->assertFetchedOne($data); $this->assertDatabaseHas('phones', [ 'id' => $phone->getKey(), 'user_id' => null, ]); }
A user with an existing phone can have the phone replaced with null.
testUpdateReplacesRelationshipWithNull
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasOneTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasOneTest.php
Apache-2.0
public function testUpdateReplacesNullRelationshipWithResource() { /** @var User $user */ $user = factory(User::class)->create(); /** @var Phone $phone */ $phone = factory(Phone::class)->create(); $data = [ 'type' => 'users', 'id' => (string) $user->getKey(), 'relationships' => [ 'phone' => [ 'data' => [ 'type' => 'phones', 'id' => (string) $phone->getKey(), ], ], ], ]; $response = $this ->jsonApi() ->withData($data) ->includePaths('phone') ->patch(url('/api/v1/users', $user)); $response->assertFetchedOne($data); $this->assertDatabaseHas('phones', [ 'id' => $phone->getKey(), 'user_id' => $user->getKey(), ]); }
A user linked to no phone can update their phone to a related resource.
testUpdateReplacesNullRelationshipWithResource
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasOneTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasOneTest.php
Apache-2.0
public function testUpdateChangesRelatedResource() { /** @var Phone $existing */ $existing = factory(Phone::class)->states('user')->create(); /** @var Phone $other */ $other = factory(Phone::class)->create(); $data = [ 'type' => 'users', 'id' => (string) $existing->user_id, 'relationships' => [ 'phone' => [ 'data' => [ 'type' => 'phones', 'id' => (string) $other->getKey(), ], ], ], ]; $response = $this ->jsonApi() ->withData($data) ->includePaths('phone') ->patch(url('/api/v1/users', $existing->user_id)); $response->assertFetchedOne($data); $this->assertDatabaseHas('phones', [ 'id' => $existing->getKey(), 'user_id' => null, ]); $this->assertDatabaseHas('phones', [ 'id' => $other->getKey(), 'user_id' => $existing->user_id, ]); }
A user linked to an existing phone can change to another phone.
testUpdateChangesRelatedResource
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasOneTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasOneTest.php
Apache-2.0
public function testReadRelated() { /** @var Phone $phone */ $phone = factory(Phone::class)->states('user')->create(); /** @var User $user */ $user = $phone->user; $data = [ 'type' => 'phones', 'id' => (string) $phone->getKey(), 'attributes' => [ 'number' => $phone->number, ], 'relationships' => [ 'user' => [ 'data' => [ 'type' => 'users', 'id' => (string) $user->getKey(), ], ], ], ]; $response = $this ->jsonApi() ->includePaths('user') ->get(url('/api/v1/users', [$user, 'phone'])); $response->assertFetchedOne($data); }
Test that we can read the related phone.
testReadRelated
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasOneTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasOneTest.php
Apache-2.0
public function testReadRelationship() { /** @var Phone $phone */ $phone = factory(Phone::class)->states('user')->create(); $response = $this ->jsonApi('phones') ->includePaths('user') ->get(url('/api/v1/users', [$phone->user, 'relationships', 'phone'])); $response ->assertFetchedToOne($phone); }
Test that we can read the resource identifier for the related phone.
testReadRelationship
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasOneTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasOneTest.php
Apache-2.0
public function testReplaceNullRelationshipWithRelatedResource() { /** @var User $user */ $user = factory(User::class)->create(); /** @var Phone $phone */ $phone = factory(Phone::class)->create(); $data = ['type' => 'phones', 'id' => (string) $phone->getRouteKey()]; $response = $this ->jsonApi('phones') ->withData($data) ->patch(url('/api/v1/users', [$user, 'relationships', 'phone'])); $response ->assertStatus(204); $this->assertDatabaseHas('phones', [ 'id' => $phone->getKey(), 'user_id' => $user->getKey(), ]); }
Test that we can replace a null relationship with a related resource.
testReplaceNullRelationshipWithRelatedResource
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasOneTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasOneTest.php
Apache-2.0
public function testReplaceRelationshipWithNull() { /** @var Phone $phone */ $phone = factory(Phone::class)->states('user')->create(); /** @var Phone $other */ $other = factory(Phone::class)->states('user')->create(); $response = $this ->jsonApi('phones') ->withData(null) ->patch(url('/api/v1/users', [$phone->user, 'relationships', 'phone'])); $response ->assertStatus(204); $this->assertDatabaseHas('phones', [ 'id' => $phone->getKey(), 'user_id' => null, ]); /** The other phone must be unaffected. */ $this->assertDatabaseHas('phones', [ 'id' => $other->getKey(), 'user_id' => $other->user_id, ]); }
Test that we can clear the related phone relationship.
testReplaceRelationshipWithNull
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasOneTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasOneTest.php
Apache-2.0
public function testReplaceRelationshipWithDifferentResource() { /** @var Phone $existing */ $existing = factory(Phone::class)->states('user')->create(); /** @var Phone $other */ $other = factory(Phone::class)->create(); $data = ['type' => 'phones', 'id' => (string) $other->getRouteKey()]; $response = $this ->jsonApi('phones') ->withData($data) ->patch(url('/api/v1/users', [$existing->user, 'relationships', 'phone'])); $response ->assertStatus(204); $this->assertDatabaseHas('phones', [ 'id' => $existing->getKey(), 'user_id' => null, ]); $this->assertDatabaseHas('phones', [ 'id' => $other->getKey(), 'user_id' => $existing->user_id, ]); }
Test that we can replace a related resource with a different one.
testReplaceRelationshipWithDifferentResource
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasOneTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasOneTest.php
Apache-2.0
public function testReadRelated(): void { $this->checkSupported(); $supplier = factory(Supplier::class)->create(); $user = factory(User::class)->create(['supplier_id' => $supplier->getKey()]); $history = factory(History::class)->create(['user_id' => $user->getKey()]); $data = [ 'type' => 'histories', 'id' => (string) $history->getRouteKey(), 'attributes' => [ 'detail' => $history->detail, ], 'relationships' => [ 'user' => [ 'data' => [ 'type' => 'users', 'id' => (string) $user->getRouteKey(), ], ], ], ]; $response = $this ->jsonApi() ->includePaths('user') ->get(url('/api/v1/suppliers', [$supplier, 'user-history'])); $response ->assertFetchedOne($data); }
Test that we can read the related phone.
testReadRelated
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasOneThroughTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasOneThroughTest.php
Apache-2.0
private function checkSupported(): void { if (!class_exists(HasOneThrough::class)) { $this->markTestSkipped('Eloquent has-one-through not supported.'); } }
@return void @todo remove when minimum Laravel version is 5.8.
checkSupported
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/HasOneThroughTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/HasOneThroughTest.php
Apache-2.0
public function testReadRelated() { $model = factory(Post::class)->create(); $comments = factory(Comment::class, 2)->create([ 'commentable_type' => Post::class, 'commentable_id' => $model->getKey(), ]); /** This comment should not appear in the results... */ factory(Comment::class)->states('post')->create(); $response = $this ->jsonApi('comments') ->get(url('/api/v1/posts', [$model, 'comments'])); $response ->assertFetchedMany($comments); }
Test that we can read the related comments.
testReadRelated
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/MorphManyTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/MorphManyTest.php
Apache-2.0
public function testReadRelationship() { $model = factory(Post::class)->create(); $comments = factory(Comment::class, 2)->create([ 'commentable_type' => Post::class, 'commentable_id' => $model->getKey(), ]); /** This comment should not appear in the results... */ factory(Comment::class)->states('post')->create(); $response = $this ->jsonApi('comments') ->get(url('/api/v1/posts', [$model, 'relationships', 'comments'])); $response ->assertFetchedToMany($comments); }
Test that we can read the resource identifiers for the related comments.
testReadRelationship
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/MorphManyTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/MorphManyTest.php
Apache-2.0
private function assertCommentIs(Post $post, Comment $comment) { $this->assertCommentsAre($post, [$comment]); }
@param $post @param $comment @return void
assertCommentIs
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/MorphManyTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/MorphManyTest.php
Apache-2.0
private function assertCommentsAre(Post $post, $comments) { $this->assertSame(count($comments), $post->comments()->count()); /** @var Comment $comment */ foreach ($comments as $comment) { $this->assertDatabaseHas('comments', [ $comment->getKeyName() => $comment->getKey(), 'commentable_type' => Post::class, 'commentable_id' => $post->getKey(), ]); } }
@param Post $post @param iterable $comments @return void
assertCommentsAre
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/MorphManyTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/MorphManyTest.php
Apache-2.0
public function testAddToRelationshipDoesNotCreateDuplicates() { $post = factory(Post::class)->create(); $existing = factory(Tag::class, 2)->create(); $post->tags()->sync($existing); $add = factory(Tag::class, 2)->create(); $data = $add->merge($existing)->map(function (Tag $tag) { return ['type' => 'tags', 'id' => $tag->getRouteKey()]; })->all(); $response = $this ->jsonApi('tags') ->withData($data) ->post(url('/api/v1/posts', [$post, 'relationships', 'tags'])); $response ->assertStatus(204); $this->assertTagsAre($post, $existing->merge($add)); }
From the spec: > If a client makes a POST request to a URL from a relationship link, > the server MUST add the specified members to the relationship unless > they are already present. If a given type and id is already in the > relationship, the server MUST NOT add it again.
testAddToRelationshipDoesNotCreateDuplicates
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/MorphToManyTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/MorphToManyTest.php
Apache-2.0
public function testRemoveWithIdsThatAreNotRelated() { $post = factory(Post::class)->create(); $tags = factory(Tag::class, 2)->create(); $post->tags()->sync($tags); $data = factory(Tag::class, 2)->create()->map(function (Tag $tag) { return ['type' => 'tags', 'id' => $tag->getRouteKey()]; })->all(); $response = $this ->jsonApi('tags') ->withData($data) ->delete(url('/api/v1/posts', [$post, 'relationships', 'tags'])); $response ->assertStatus(204); $this->assertTagsAre($post, $tags); }
From the spec: > If all of the specified resources are able to be removed from, > or are already missing from, the relationship then the server > MUST return a successful response.
testRemoveWithIdsThatAreNotRelated
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/MorphToManyTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/MorphToManyTest.php
Apache-2.0
private function assertTagIs(Post $post, Tag $tag) { $this->assertTagsAre($post, [$tag]); }
@param $post @param $tag @return void
assertTagIs
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/MorphToManyTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/MorphToManyTest.php
Apache-2.0
private function assertTagsAre(Post $post, $tags) { $this->assertSame(count($tags), $post->tags()->count()); /** @var Tag $tag */ foreach ($tags as $tag) { $this->assertDatabaseHas('taggables', [ 'taggable_type' => Post::class, 'taggable_id' => $post->getKey(), 'tag_id' => $tag->getKey(), ]); } }
@param Post $post @param iterable $tags @return void
assertTagsAre
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/MorphToManyTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/MorphToManyTest.php
Apache-2.0
private function assertTaggablesAre(Tag $tag, $posts, $videos) { $this->assertSame( count($posts) + count($videos), \DB::table('taggables')->where('tag_id', $tag->getKey())->count(), 'Unexpected number of taggables.' ); $this->assertSame(count($posts), $tag->posts()->count(), 'Unexpected number of posts.'); $this->assertSame(count($videos), $tag->videos()->count(), 'Unexpected number of videos.'); /** @var Post $post */ foreach ($posts as $post) { $this->assertDatabaseHas('taggables', [ 'taggable_type' => Post::class, 'taggable_id' => $post->getKey(), 'tag_id' => $tag->getKey(), ]); } /** @var Video $video */ foreach ($videos as $video) { $this->assertDatabaseHas('taggables', [ 'taggable_type' => Video::class, 'taggable_id' => $video->getKey(), 'tag_id' => $tag->getKey(), ]); } }
@param Tag $tag @param iterable $posts @param iterable $videos @return void
assertTaggablesAre
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/PolymorphicHasManyTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/PolymorphicHasManyTest.php
Apache-2.0
public function testUnrecognisedFilter() { $response = $this ->jsonApi('posts') ->filter(['foo' => 'bar', 'slug' => 'my-first-post']) ->get('/api/v1/posts'); $response ->assertStatus(400); }
As the posts adapter uses the `FiltersModel` trait we need to check how it handles unrecognised parameters.
testUnrecognisedFilter
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testSearchById() { $models = factory(Post::class, 2)->create(); // this model should not be in the search results $this->createPost(); $ids = $models->map(fn($model) => $model->getRouteKey()); $response = $this ->jsonApi('posts') ->filter(['id' => $ids]) ->get('/api/v1/posts'); $response->assertFetchedMany($models); }
Test that we can search posts for specific ids
testSearchById
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testCreateWithoutRequiredMember() { $model = factory(Post::class)->make(); $data = [ 'type' => 'posts', 'attributes' => [ 'title' => $model->title, 'content' => $model->content, ], 'relationships' => [ 'author' => [ 'data' => [ 'type' => 'users', 'id' => (string) $model->author_id, ], ], ], ]; $response = $this ->jsonApi() ->withData($data) ->post('/api/v1/posts'); $response->assertErrorStatus([ 'status' => '422', 'detail' => 'The slug field is required.', 'source' => [ 'pointer' => '/data', ], ]); }
@see https://github.com/cloudcreativity/laravel-json-api/issues/255
testCreateWithoutRequiredMember
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testRead() { $retrieved = 0; Post::retrieved(function () use (&$retrieved) { $retrieved++; }); $model = $this->createPost(); $model->tags()->create(['name' => 'Important']); $response = $this ->withoutExceptionHandling() ->jsonApi() ->get(url('/api/v1/posts', $model)); $response->assertFetchedOneExact( $this->serialize($model) ); $this->assertSame(1, $retrieved, 'retrieved once'); }
Test the read resource route. @see https://github.com/cloudcreativity/laravel-json-api/issues/256 we only expect to see the model retrieved once.
testRead
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testReadSoftDeleted() { $post = factory(Post::class)->create(['deleted_at' => Carbon::now()]); $response = $this ->jsonApi() ->get(url('/api/v1/posts', $post)); $response->assertFetchedOneExact( $this->serialize($post) ); }
We must be able to read soft deleted models.
testReadSoftDeleted
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testReadWithInclude() { $model = $this->createPost(); $tag = $model->tags()->create(['name' => 'Important']); $expected = $this->serialize($model); $expected['relationships']['author']['data'] = [ 'type' => 'users', 'id' => (string) $model->author_id, ]; $expected['relationships']['tags']['data'] = [ ['type' => 'tags', 'id' => $tag->uuid], ]; $expected['relationships']['comments']['data'] = []; $response = $this ->jsonApi() ->includePaths('author', 'tags', 'comments') ->get(url('/api/v1/posts', $model)); $response ->assertFetchedOne($expected) ->assertIsIncluded('users', $model->author) ->assertIsIncluded('tags', $tag); }
Test reading a resource with included resources. We expect the relationships data identifiers to be serialized in the response so that the compound document has full resource linkage, in accordance with the spec.
testReadWithInclude
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testReadWithEmptyInclude(): void { $post = factory(Post::class)->create(); $response = $this ->withoutExceptionHandling() ->jsonApi() ->get("api/v1/posts/{$post->getRouteKey()}?include="); $response->assertFetchedOne($this->serialize($post)); }
@see https://github.com/cloudcreativity/laravel-json-api/issues/518
testReadWithEmptyInclude
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testReadWithInvalidInclude() { $post = $this->createPost(); $response = $this ->jsonApi() ->includePaths('author', 'foo') ->get(url('/api/v1/posts', $post)); $response->assertError(400, [ 'status' => '400', 'detail' => 'Include path foo is not allowed.', 'source' => ['parameter' => 'include'], ]); }
@see https://github.com/cloudcreativity/laravel-json-api/issues/194
testReadWithInvalidInclude
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testReadWithDashCaseRelationLinks(): void { $comment = factory(Comment::class)->create(); $self = 'http://localhost/api/v1/comments/' . $comment->getRouteKey(); $expected = [ 'type' => 'comments', 'id' => (string) $comment->getRouteKey(), 'attributes' => [ 'content' => $comment->content, 'createdAt' => $comment->created_at->toJSON(), 'updatedAt' => $comment->updated_at->toJSON(), ], 'relationships' => [ 'commentable' => [ 'links' => [ 'self' => "{$self}/relationships/commentable", 'related' => "{$self}/commentable", ], ], 'createdBy' => [ 'links' => [ 'self' => "{$self}/relationships/created-by", 'related' => "{$self}/created-by", ], ], ], 'links' => [ 'self' => $self, ], ]; $response = $this ->actingAs($comment->user) ->jsonApi() ->expects('comments') ->get($self); $response->assertFetchedOneExact($expected); }
When using camel-case JSON API fields, we may want the relationship URLs to use dash-case for the field name.
testReadWithDashCaseRelationLinks
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testResourceNotFound() { $response = $this ->jsonApi() ->get('/api/v1/posts/xyz'); $response->assertStatus(404); }
Test that the resource can not be found.
testResourceNotFound
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testUpdateRefreshes() { $post = $this->createPost(); Post::saving(function (Post $saved) { $saved->tags; // causes the model to cache the tags relationship. }); /** @var Tag $tag */ $tag = factory(Tag::class)->create(); $data = [ 'type' => 'posts', 'id' => (string) $post->getRouteKey(), 'relationships' => [ 'tags' => [ 'data' => [ ['type' => 'tags', 'id' => $tag->uuid], ], ], ], ]; $response = $this ->jsonApi() ->withData($data) ->includePaths('tags') ->patch(url('/api/v1/posts', $post)); $response->assertFetchedOne($data); $this->assertDatabaseHas('taggables', [ 'taggable_type' => Post::class, 'taggable_id' => $post->getKey(), 'tag_id' => $tag->getKey(), ]); }
Issue 125. If the model caches any of its relationships prior to a hydrator being invoked, any changes to that relationship will not be serialized when the schema serializes the model. @see https://github.com/cloudcreativity/laravel-json-api/issues/125
testUpdateRefreshes
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testUpdateWithUnrecognisedRelationship() { $post = factory(Post::class)->create(); $data = [ 'type' => 'posts', 'id' => (string) $post->getRouteKey(), 'attributes' => [ 'title' => 'Hello World', ], 'relationships' => [ 'edited-by' => [ 'data' => [ 'type' => 'users', 'id' => (string) $post->author_id, ], ], ], ]; $response = $this ->jsonApi() ->withData($data) ->patch(url('/api/v1/posts', $post)); $response->assertStatus(200); $this->assertDatabaseHas('posts', [ 'id' => $post->getKey(), 'title' => 'Hello World', ]); }
Test that if a client sends a relation that does not exist, it is ignored rather than causing an internal server error.
testUpdateWithUnrecognisedRelationship
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testUpdateWithRelationshipAsAttribute() { $post = factory(Post::class)->create(); $data = [ 'type' => 'posts', 'id' => (string) $post->getRouteKey(), 'attributes' => [ 'title' => 'Hello World', 'author' => 'foobar', ], ]; $response = $this ->jsonApi() ->withData($data) ->patch(url('/api/v1/posts', $post)); $response->assertStatus(200); $this->assertDatabaseHas('posts', [ 'id' => $post->getKey(), 'title' => 'Hello World', 'author_id' => $post->author_id, ]); }
The client sends an unexpected attribute with the same name as a relationship.
testUpdateWithRelationshipAsAttribute
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testTrimsStrings() { $model = $this->createPost(); $data = [ 'type' => 'posts', 'id' => (string) $model->getRouteKey(), 'attributes' => [ 'content' => ' Hello world. ', ], ]; $expected = $data; $expected['attributes']['content'] = 'Hello world.'; $response = $this ->jsonApi() ->withData($data) ->patch(url('/api/v1/posts', $model)); $response->assertFetchedOne($expected); $this->assertDatabaseHas('posts', [ 'id' => $model->getKey(), 'content' => 'Hello world.', ]); }
Laravel conversion middleware e.g. trim strings, works. @see https://github.com/cloudcreativity/laravel-json-api/issues/201
testTrimsStrings
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testUpdateAndSoftDelete() { $post = factory(Post::class)->create(); $data = [ 'type' => 'posts', 'id' => (string) $post->getRouteKey(), 'attributes' => [ 'deletedAt' => (new Carbon('2018-01-01 12:00:00'))->toJSON(), 'title' => 'My Post Is Soft Deleted', ], ]; $response = $this ->jsonApi() ->withData($data) ->patch(url('/api/v1/posts', $post)); $response->assertFetchedOne($data); $this->assertDatabaseHas('posts', [ $post->getKeyName() => $post->getKey(), 'title' => 'My Post Is Soft Deleted', ]); }
Test that we can update attributes at the same time as soft deleting.
testUpdateAndSoftDelete
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testUpdateAndRestore() { Event::fake(); $post = factory(Post::class)->create(['deleted_at' => '2018-01-01 12:00:00']); $data = [ 'type' => 'posts', 'id' => (string) $post->getRouteKey(), 'attributes' => [ 'deletedAt' => null, 'title' => 'My Post Is Restored', ], ]; $response = $this ->jsonApi() ->withData($data) ->patch(url('/api/v1/posts', $post)); $response->assertFetchedOne($data); $this->assertDatabaseHas('posts', [ $post->getKeyName() => $post->getKey(), 'deleted_at' => null, 'title' => 'My Post Is Restored', ]); Event::assertDispatched("eloquent.restored: " . Post::class, function ($name, $actual) use ($post) { return $post->is($actual); }); }
Test that we can update attributes at the same time as restoring the model.
testUpdateAndRestore
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testCannotDeletePostHasComments() { $post = factory(Comment::class)->states('post')->create()->commentable; $expected = [ 'title' => 'Not Deletable', 'status' => '422', 'detail' => 'Cannot delete a post with comments.', ]; $response = $this ->jsonApi() ->delete(url('/api/v1/posts', $post)); $response->assertExactErrorStatus($expected); }
Test that the delete request is logically validated.
testCannotDeletePostHasComments
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
private function createPost($create = true) { $builder = factory(Post::class); return $create ? $builder->create() : $builder->make(); }
Just a helper method so that we get a type-hinted model back... @param bool $create @return Post
createPost
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
private function serialize(Post $post) { $self = url('/api/v1/posts', [$post]); return [ 'type' => 'posts', 'id' => (string) $post->getRouteKey(), 'attributes' => [ 'content' => $post->content, 'createdAt' => $post->created_at->toJSON(), 'deletedAt' => optional($post->deleted_at)->toJSON(), 'published' => optional($post->published_at)->toJSON(), 'slug' => $post->slug, 'title' => $post->title, 'updatedAt' => $post->updated_at->toJSON(), ], 'relationships' => [ 'author' => [ 'links' => [ 'self' => "$self/relationships/author", 'related' => "$self/author", ], ], 'comments' => [ 'links' => [ 'self' => "$self/relationships/comments", 'related' => "$self/comments", ], 'meta' => [ 'count' => $post->comments()->count(), ], ], 'image' => [ 'links' => [ 'self' => "$self/relationships/image", 'related' => "$self/image", ], ], 'tags' => [ 'links' => [ 'self' => "$self/relationships/tags", 'related' => "$self/tags", ], ], ], 'links' => [ 'self' => $self, ], ]; }
Get the posts resource that we expect in server responses. @param Post $post @return array
serialize
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Eloquent/ResourceTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Eloquent/ResourceTest.php
Apache-2.0
public function testSearching() { $response = $this ->jsonApi() ->get('/api/v1/posts'); $response->assertStatus(200); $this->assertHooksInvoked('searching', 'searched'); }
A search must invoke the `searching` hook.
testSearching
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/HooksTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/HooksTest.php
Apache-2.0
public function testCreate() { $post = factory(Post::class)->make(); $data = [ 'type' => 'posts', 'attributes' => [ 'title' => $post->title, 'slug' => $post->slug, 'content' => $post->content, ], 'relationships' => [ 'author' => [ 'data' => [ 'type' => 'users', 'id' => (string) $post->author_id, ], ], ], ]; $response = $this ->jsonApi() ->withData($data) ->post('/api/v1/posts'); $response->assertStatus(201); $this->assertHooksInvoked('saving', 'creating', 'created', 'saved'); }
A successful create must invoke the following hooks: - saving - creating - saved - created
testCreate
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/HooksTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/HooksTest.php
Apache-2.0
public function testRead() { $post = factory(Post::class)->create(); $response = $this ->jsonApi() ->get(url('/api/v1/posts', $post)); $response->assertStatus(200); $this->assertHooksInvoked('reading', 'did-read'); }
A successful read must dispatch the `reading` hook.
testRead
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/HooksTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/HooksTest.php
Apache-2.0
public function testUpdate() { $post = factory(Post::class)->create(); $data = [ 'type' => 'posts', 'id' => (string) $post->getRouteKey(), 'attributes' => [ 'title' => 'My First Post', ], ]; $response = $this ->jsonApi() ->withData($data) ->patch(url('/api/v1/posts', $post)); $response->assertStatus(200); $this->assertHooksInvoked('saving', 'updating', 'updated', 'saved'); }
A successful update must invoke the following hooks: - saving - updating - saved - updated
testUpdate
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/HooksTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/HooksTest.php
Apache-2.0
public function testDelete() { $post = factory(Post::class)->create(); $response = $this ->jsonApi() ->delete(url('/api/v1/posts', $post)); $response->assertStatus(204); $this->assertHooksInvoked('deleting', 'deleted'); }
A successful delete must invoke the following hooks: - deleting - deleted
testDelete
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/HooksTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/HooksTest.php
Apache-2.0
public function reading(Post $record, FetchResource $request) { event(new TestEvent('reading', $record, $request)); }
@param Post $record @param FetchResource $request
reading
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function didRead(?Post $record, FetchResource $request) { event(new TestEvent('did-read', $record, $request)); }
@param Post|null $record @param FetchResource $request
didRead
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function saving($record, $request) { if (!$request instanceof CreateResource && !$request instanceof UpdateResource) { Assert::fail('Invalid request class.'); } event(new TestEvent('saving', $record, $request)); }
@param $record @param CreateResource|UpdateResource $request
saving
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function updating(Post $record, UpdateResource $request) { event(new TestEvent('updating', $record, $request)); }
@param Post $record @param UpdateResource $request
updating
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function saved(Post $record, $request) { if (!$request instanceof CreateResource && !$request instanceof UpdateResource) { Assert::fail('Invalid request class.'); } event(new TestEvent('saved', $record, $request)); }
@param Post $record @param CreateResource|UpdateResource $request
saved
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function created(Post $record, CreateResource $request) { event(new TestEvent('created', $record, $request)); }
@param Post $record @param CreateResource $request
created
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function updated(Post $record, UpdateResource $request) { event(new TestEvent('updated', $record, $request)); }
@param Post $record @param UpdateResource $request
updated
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function deleting(Post $record, DeleteResource $request) { event(new TestEvent('deleting', $record, $request)); }
@param Post $record @param DeleteResource $request
deleting
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function deleted(Post $record, DeleteResource $request) { event(new TestEvent('deleted', $record->getKey(), $request)); }
@param Post $record @param DeleteResource $request
deleted
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function didReadAuthor(Post $record, ?User $user, $request) { if (!$request instanceof FetchRelated && !$request instanceof FetchRelationship) { Assert::fail('Invalid request class.'); } event(new TestEvent('did-read-author', $record, $request, $user)); }
@param Post $record @param User|null $user @param $request
didReadAuthor
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function didReadRelationship(Post $record, $related, $request) { if (!$request instanceof FetchRelated && !$request instanceof FetchRelationship) { Assert::fail('Invalid request class.'); } event(new TestEvent('did-read-relationship', $record, $request, $related)); }
@param Post $record @param mixed|null $related @param $request
didReadRelationship
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function replacing(Post $record, UpdateRelationship $request) { event(new TestEvent('replacing', $record, $request)); }
@param Post $record @param UpdateRelationship $request
replacing
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function replacingAuthor(Post $record, UpdateRelationship $request) { event(new TestEvent('replacing-author', $record, $request)); }
@param Post $record @param UpdateRelationship $request
replacingAuthor
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function replaced(Post $record, UpdateRelationship $request) { event(new TestEvent('replaced', $record, $request)); }
@param Post $record @param UpdateRelationship $request
replaced
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function replacedAuthor(Post $record, UpdateRelationship $request) { event(new TestEvent('replaced-author', $record, $request)); }
@param Post $record @param UpdateRelationship $request
replacedAuthor
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function adding(Post $record, UpdateRelationship $request) { event(new TestEvent('adding', $record, $request)); }
@param Post $record @param UpdateRelationship $request
adding
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function addingTags(Post $record, UpdateRelationship $request) { event(new TestEvent('adding-tags', $record, $request)); }
@param Post $record @param UpdateRelationship $request
addingTags
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function addedTags(Post $record, UpdateRelationship $request) { event(new TestEvent('added-tags', $record, $request)); }
@param Post $record @param UpdateRelationship $request
addedTags
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function added(Post $record, UpdateRelationship $request) { event(new TestEvent('added', $record, $request)); }
@param Post $record @param UpdateRelationship $request
added
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function removing(Post $record, UpdateRelationship $request) { event(new TestEvent('removing', $record, $request)); }
@param Post $record @param UpdateRelationship $request
removing
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function removingTags(Post $record, UpdateRelationship $request) { event(new TestEvent('removing-tags', $record, $request)); }
@param Post $record @param UpdateRelationship $request
removingTags
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function removedTags(Post $record, UpdateRelationship $request) { event(new TestEvent('removed-tags', $record, $request)); }
@param Post $record @param UpdateRelationship $request
removedTags
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function removed(Post $record, UpdateRelationship $request) { event(new TestEvent('removed', $record, $request)); }
@param Post $record @param UpdateRelationship $request
removed
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestController.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestController.php
Apache-2.0
public function __construct( $hook, $record, ?ValidatedRequest $request = null, $related = null ) { $this->hook = $hook; $this->record = $record; $this->request = $request; $this->related = $related; }
ResourceEvent constructor. @param string $hook @param mixed|null $record @param ValidatedRequest|null $request @param mixed|null $related
__construct
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Http/Controllers/TestEvent.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Http/Controllers/TestEvent.php
Apache-2.0
public function testCreate($hook, array $unexpected) { $post = factory(Post::class)->make(); $data = [ 'type' => 'posts', 'attributes' => [ 'title' => $post->title, 'slug' => $post->slug, 'content' => $post->content, ], 'relationships' => [ 'author' => [ 'data' => [ 'type' => 'users', 'id' => (string) $post->author_id, ], ], ], ]; $this->withResponse($hook, $unexpected); $response = $this ->jsonApi() ->withData($data) ->post('/api/v1/posts'); $response->assertStatus(202); }
@param $hook @param array $unexpected @dataProvider createProvider
testCreate
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Issue154/IssueTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Issue154/IssueTest.php
Apache-2.0
public function testUpdate($hook, array $unexpected) { $post = factory(Post::class)->create(); $data = [ 'type' => 'posts', 'id' => (string) $post->getKey(), 'attributes' => [ 'title' => 'My First Post', ], ]; $this->withResponse($hook, $unexpected); $response = $this ->jsonApi() ->withData($data) ->patch(url('/api/v1/posts', $post)); $response->assertStatus(202); }
@param $hook @param array $unexpected @dataProvider updateProvider
testUpdate
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Issue154/IssueTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Issue154/IssueTest.php
Apache-2.0
public function testDelete($hook, array $unexpected) { $post = factory(Post::class)->create(); $this->withResponse($hook, $unexpected); $response = $this ->jsonApi() ->delete(url('/api/v1/posts', $post)); $response->assertStatus(202); }
@param $hook @param array $unexpected @dataProvider deleteProvider
testDelete
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Issue154/IssueTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Issue154/IssueTest.php
Apache-2.0
private function withResponse($hook, array $unexpected = []) { $this->app->instance('DummyApp\Http\Controllers\PostsController', $controller = new Controller()); $controller->responses[$hook] = response('', 202); $controller->unexpected = $unexpected; return $this; }
@param $hook @param array $unexpected hooks that must not be invoked. @return $this
withResponse
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Issue154/IssueTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Issue154/IssueTest.php
Apache-2.0
protected function creating(Post $post, ResourceObject $resource): void { $error = Error::fromArray([ 'title' => 'The language you want to use is not active', 'status' => Response::HTTP_UNPROCESSABLE_ENTITY, ]); throw new JsonApiException($error); }
@param Post $post @param ResourceObject $resource @throws JsonApiException
creating
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Issue566/Adapter.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Issue566/Adapter.php
Apache-2.0
public function test() { factory(Post::class)->create(); $response = $this ->jsonApi() ->page(['number' => 1, 'size' => 5]) ->get('/api/v1/posts'); $response ->assertStatus(500); $response->assertExactJson([ 'errors' => [], ]); }
If an exception is thrown while rendering resources within a page, the page meta must not leak into the error response. @see https://github.com/cloudcreativity/laravel-json-api/issues/67
test
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Issue67/IssueTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Issue67/IssueTest.php
Apache-2.0
public function testBeforeDoesNotExist() { $response = $this ->actingAsUser() ->jsonApi('comments') ->page(['before' => '999']) ->get('/api/v1/comments'); $response ->assertStatus(500); }
If the before key does not exist, we expect the cursor builder to throw an exception which would constitute an internal server error. Applications should validate the id before passing it to the cursor.
testBeforeDoesNotExist
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Pagination/CursorPagingTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Pagination/CursorPagingTest.php
Apache-2.0
public function testAfterDoesNotExist() { $response = $this ->actingAsUser() ->jsonApi('comments') ->page(['after' => '999']) ->get('/api/v1/comments'); $response ->assertStatus(500); }
If the after key does not exist, we expect the cursor builder to throw an exception which would constitute an internal server error. Applications should validate the id before passing it to the cursor.
testAfterDoesNotExist
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Pagination/CursorPagingTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Pagination/CursorPagingTest.php
Apache-2.0
public function testBeforeAndAfter() { /** @var Collection $comments */ $comments = factory(Comment::class, 6)->create([ 'created_at' => function () { return $this->faker->dateTime(); } ])->sortByDesc('created_at')->values(); $page = [ 'limit' => '3', 'before' => $comments->get(5)->getRouteKey(), 'after' => $comments->get(1)->getRouteKey(), ]; $expected = collect([ $comments->get(2), $comments->get(3), $comments->get(4), ]); $meta = [ 'page' => [ 'per-page' => 3, 'from' => (string) $expected->first()->getRouteKey(), 'to' => (string) $expected->last()->getRouteKey(), 'has-more' => true, ], ]; $response = $this ->actingAsUser() ->jsonApi('comments') ->page($page) ->get('/api/v1/comments'); $response ->assertFetchedMany($expected) ->assertExactMeta($meta) ->assertExactLinks($this->createLinks(3, $expected->first(), $expected->last())); }
If we supply both the before and after ids, only the before should be used.
testBeforeAndAfter
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Pagination/CursorPagingTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Pagination/CursorPagingTest.php
Apache-2.0
public function testSameColumnAndIdentifier() { $this->strategy->withColumn('id'); /** @var Collection $comments */ $comments = factory(Comment::class, 6)->create([ 'created_at' => function () { return $this->faker->dateTime(); } ])->sortByDesc('id')->values(); $page = [ 'limit' => '3', 'before' => $comments->get(4)->getRouteKey(), 'after' => $comments->get(1)->getRouteKey(), ]; $expected = collect([ $comments->get(1), $comments->get(2), $comments->get(3), ]); $meta = [ 'page' => [ 'per-page' => 3, 'from' => (string) $expected->first()->getRouteKey(), 'to' => (string) $expected->last()->getRouteKey(), 'has-more' => true, ], ]; $response = $this ->actingAsUser() ->jsonApi('comments') ->page($page) ->get('/api/v1/comments'); $response ->assertFetchedMany($expected) ->assertExactMeta($meta) ->assertExactLinks($this->createLinks(3, $expected->first(), $expected->last())); }
Test use of the cursor paginator where the pagination column is identical to the identifier column.
testSameColumnAndIdentifier
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Pagination/CursorPagingTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Pagination/CursorPagingTest.php
Apache-2.0
public function testCustomMeta() { $this->strategy->withMetaKey('cursor')->withUnderscoredMetaKeys(); /** @var Collection $comments */ $comments = factory(Comment::class, 6)->create([ 'created_at' => function () { return $this->faker->dateTime(); } ])->sortByDesc('created_at')->values(); $page = [ 'limit' => '3', 'before' => $comments->get(4)->getRouteKey(), ]; $expected = collect([ $comments->get(1), $comments->get(2), $comments->get(3), ]); $meta = [ 'cursor' => [ 'per_page' => 3, 'from' => (string) $expected->first()->getRouteKey(), 'to' => (string) $expected->last()->getRouteKey(), 'has_more' => true, ], ]; $response = $this ->actingAsUser() ->jsonApi('comments') ->page($page) ->get('/api/v1/comments'); $response ->assertFetchedMany($expected) ->assertExactMeta($meta); }
Test that we can customise the meta nesting and underscore meta keys.
testCustomMeta
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Pagination/CursorPagingTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Pagination/CursorPagingTest.php
Apache-2.0
public function testColumn() { $this->strategy->withColumn('updated_at'); /** @var Collection $comments */ $comments = factory(Comment::class, 6)->create([ 'updated_at' => function () { return $this->faker->dateTime(); } ])->sortByDesc('updated_at')->values(); $page = [ 'limit' => '3', 'before' => $comments->get(4)->getRouteKey(), ]; $expected = collect([ $comments->get(1), $comments->get(2), $comments->get(3), ]); $meta = [ 'page' => [ 'per-page' => 3, 'from' => (string) $expected->first()->getRouteKey(), 'to' => (string) $expected->last()->getRouteKey(), 'has-more' => true, ], ]; $response = $this ->actingAsUser() ->jsonApi('comments') ->page($page) ->get('/api/v1/comments'); $response ->assertFetchedMany($expected) ->assertExactMeta($meta); }
Test that we can change the column on which we paginate.
testColumn
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Pagination/CursorPagingTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Pagination/CursorPagingTest.php
Apache-2.0
public function testDefaultPagination() { $posts = factory(Post::class, 4)->create(); $meta = [ 'page' => [ 'current-page' => 1, 'per-page' => 10, 'from' => 1, 'to' => 4, 'total' => 4, 'last-page' => 1, ], ]; $response = $this ->jsonApi('posts') ->get('/api/v1/posts'); $response->assertFetchedMany($posts)->assertExactMeta($meta); }
An adapter's default pagination is used if no pagination parameters are sent. @see https://github.com/cloudcreativity/laravel-json-api/issues/131
testDefaultPagination
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Pagination/StandardPagingTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Pagination/StandardPagingTest.php
Apache-2.0
public function testNoPages() { $meta = [ 'page' => [ 'current-page' => 1, 'per-page' => 3, 'from' => null, 'to' => null, 'total' => 0, 'last-page' => 1, ], ]; $links = [ 'first' => $first = $this->buildLink( '/api/v1/posts', ['page' => ['number' => 1, 'size' => 3]] ), 'last' => $first, ]; $response = $this ->jsonApi('posts') ->page(['number' => 1, 'size' => 3]) ->get('/api/v1/posts'); $response ->assertFetchedNone() ->assertExactMeta($meta) ->assertExactLinks($links); }
If the search does not match any models, then there are no pages.
testNoPages
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Pagination/StandardPagingTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Pagination/StandardPagingTest.php
Apache-2.0
public function testDeterministicOrder() { $first = factory(Video::class)->create([ 'created_at' => Carbon::now()->subWeek(), ]); $f = factory(Video::class)->create([ 'uuid' => 'f3b3bea3-dca0-4ef9-b06c-43583a7e6118', 'created_at' => Carbon::now()->subHour(), ]); $d = factory(Video::class)->create([ 'uuid' => 'd215f35c-feb7-4cc5-9631-61742f00d0b2', 'created_at' => $f->created_at, ]); $c = factory(Video::class)->create([ 'uuid' => 'cbe17134-d7e2-4509-ba2c-3b3b5e3b2cbe', 'created_at' => $f->created_at, ]); $response = $this ->jsonApi('videos') ->page(['number' => '1', 'size' => '3']) ->sort('createdAt') ->get('/api/v1/videos'); $response->assertFetchedManyInOrder([$first, $c, $d]); }
If we are sorting by a column that might not be unique, we expect the page to always be returned in a particular order i.e. by the key column. @see https://github.com/cloudcreativity/laravel-json-api/issues/313
testDeterministicOrder
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Pagination/StandardPagingTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Pagination/StandardPagingTest.php
Apache-2.0
protected function buildLink($path, array $params) { return 'http://localhost' . $path . '?' . http_build_query($params); }
@param $path @param array $params @return string
buildLink
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Pagination/TestCase.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Pagination/TestCase.php
Apache-2.0
public function testCreateWithClientGeneratedId() { $data = [ 'type' => 'downloads', 'id' => '85f3cb08-5c5c-4e41-ae92-57097d28a0b8', 'attributes' => [ 'category' => 'my-posts', ], ]; $response = $this ->jsonApi('downloads') ->withData($data) ->post('/api/v1/downloads'); $response->assertAcceptedWithId('http://localhost/api/v1/downloads/queue-jobs', [ 'type' => 'queue-jobs', 'attributes' => [ 'resourceType' => 'downloads', 'timeout' => 60, 'timeoutAt' => null, 'tries' => null, ], ]); $job = $this->assertDispatchedCreate(); $this->assertSame($data['id'], $job->resourceId(), 'resource id'); $this->assertNotSame($data['id'], $job->clientJob->getKey()); $this->assertDatabaseHas('json_api_client_jobs', [ 'uuid' => $job->clientJob->getKey(), 'created_at' => '2018-10-23 12:00:00', 'updated_at' => '2018-10-23 12:00:00', 'api' => 'v1', 'resource_type' => 'downloads', 'resource_id' => $data['id'], 'timeout' => 60, 'timeout_at' => null, 'tries' => null, ]); }
If we are asynchronously creating a resource with a client generated id, that id needs to be stored on the client job.
testCreateWithClientGeneratedId
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Queue/ClientDispatchTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Queue/ClientDispatchTest.php
Apache-2.0
public function testReadNotPending() { $job = factory(ClientJob::class)->states('success', 'with_download')->create(); $expected = $this->serialize($job); $response = $this ->jsonApi('queue-jobs') ->get($expected['links']['self']); $response ->assertStatus(303) ->assertHeader('Location', url('/api/v1/downloads', [$job->resource_id])) ->assertHeader('Content-Type', 'application/vnd.api+json'); $this->assertEmpty($response->getContent(), 'content is empty.'); }
When job process is done, the request SHOULD return a status 303 See other with a link in Location header. The spec recommendation shows a response with a Content-Type header as `application/vnd.api+json` but no content in the response body.
testReadNotPending
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Queue/QueueJobsTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Queue/QueueJobsTest.php
Apache-2.0
public function testReadNotPendingCannotSeeOther() { $job = factory(ClientJob::class)->states('success')->create(); $expected = $this->serialize($job); $response = $this ->jsonApi('queue-jobs') ->get($expected['links']['self']); $response ->assertFetchedOneExact($expected) ->assertHeaderMissing('Location'); }
If the asynchronous process does not have a location, a See Other response cannot be returned. In this scenario, we expect the job to be serialized.
testReadNotPendingCannotSeeOther
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Queue/QueueJobsTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Queue/QueueJobsTest.php
Apache-2.0
public function testReadFailed() { $job = factory(ClientJob::class)->states('failed', 'with_download')->create(); $expected = $this->serialize($job); $response = $this ->jsonApi('queue-jobs') ->get($expected['links']['self']); $response ->assertFetchedOneExact($expected) ->assertHeaderMissing('Location'); }
If the async process fails, we do not expect it to return a See Other even if it has a resource id. This is because otherwise there is no way for the client to know that it failed.
testReadFailed
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Queue/QueueJobsTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Queue/QueueJobsTest.php
Apache-2.0
private function serialize(ClientJob $job): array { return [ 'type' => 'queue-jobs', 'id' => (string) $job->getRouteKey(), 'attributes' => [ 'attempts' => $job->attempts, 'createdAt' => $job->created_at->toJSON(), 'completedAt' => optional($job->completed_at)->toJSON(), 'failed' => $job->failed, 'resourceType' => 'downloads', 'timeout' => $job->timeout, 'timeoutAt' => optional($job->timeout_at)->toJSON(), 'tries' => $job->tries, 'updatedAt' => $job->updated_at->toJSON(), ], 'links' => [ 'self' => url('/api/v1', [$job->resource_type, 'queue-jobs', $job]), ], ]; }
Get the expected resource object for a client job model. @param ClientJob $job @return array
serialize
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Queue/QueueJobsTest.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Queue/QueueJobsTest.php
Apache-2.0
public function handle(): ?Download { if ($this->ex) { throw new \LogicException('Boom.'); } if ($this->model) { $this->model->delete(); $this->didComplete(); return null; } $download = factory(Download::class)->create(); $this->didCreate($download); return $download; }
Execute the job. @return Download|null @throws \Exception
handle
php
cloudcreativity/laravel-json-api
tests/lib/Integration/Queue/TestJob.php
https://github.com/cloudcreativity/laravel-json-api/blob/master/tests/lib/Integration/Queue/TestJob.php
Apache-2.0