-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retrieving Posts Using a Post Collection Resource
- Loading branch information
1 parent
810c3da
commit a4e4572
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
|
||
class PostCollection extends ResourceCollection | ||
{ | ||
/** | ||
* Transform the resource collection into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'data' => $this->collection, | ||
'links' => [ | ||
'self' => url('/posts') | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
/** @var Factory $factory */ | ||
|
||
use App\Post; | ||
use App\User; | ||
use Faker\Generator as Faker; | ||
use Illuminate\Database\Eloquent\Factory; | ||
|
||
$factory->define(Post::class, function (Faker $faker) { | ||
return [ | ||
'user_id' => factory(User::class), | ||
'body' => $faker->sentence | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Post; | ||
use App\User; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Http\Response; | ||
use Tests\TestCase; | ||
|
||
class RetrievePostsTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
/** @test */ | ||
public function a_user_can_retrieve_posts() | ||
{ | ||
$this->withoutExceptionHandling(); | ||
$this->actingAs($user = factory(User::class)->create(), 'api'); | ||
$posts = factory(Post::class, 2)->create(); | ||
|
||
|
||
$response = $this->get('/api/posts'); | ||
$response->assertStatus(Response::HTTP_OK) | ||
->assertJson([ | ||
'data' => [ | ||
[ | ||
'data' => | ||
[ | ||
'type' => 'posts', | ||
'post_id' => $posts->first()->id, | ||
'attributes' => [ | ||
'body' => $posts->first()->body | ||
] | ||
] | ||
], | ||
[ | ||
'data' => | ||
[ | ||
'type' => 'posts', | ||
'post_id' => $posts->last()->id, | ||
'attributes' => [ | ||
'body' => $posts->last()->body | ||
] | ||
] | ||
] | ||
|
||
], | ||
'links' => [ | ||
'self' => url('/posts') | ||
], | ||
]); | ||
|
||
} | ||
} |