-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
404 additions
and
12 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
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
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
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
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
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,48 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\CommentStoreRequest; | ||
use App\Http\Requests\CommentUpdateRequest; | ||
use App\Http\Resources\CommentCollection; | ||
use App\Http\Resources\CommentResource; | ||
use App\Models\Comment; | ||
use App\Models\Post; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
|
||
class CommentController extends Controller | ||
{ | ||
public function index(Request $request, Post $post): CommentCollection | ||
{ | ||
$comments = $post->comments()->get(); | ||
|
||
return new CommentCollection($comments); | ||
} | ||
|
||
public function store(CommentStoreRequest $request, Post $post): CommentResource | ||
{ | ||
$comment = $post->comments()->create($request->validated()); | ||
|
||
return new CommentResource($comment); | ||
} | ||
|
||
public function show(Request $request, Post $post, Comment $comment): CommentResource | ||
{ | ||
return new CommentResource($comment); | ||
} | ||
|
||
public function update(CommentUpdateRequest $request, Post $post, Comment $comment): CommentResource | ||
{ | ||
$comment->update($request->validated()); | ||
|
||
return new CommentResource($comment); | ||
} | ||
|
||
public function destroy(Request $request, Post $post, Comment $comment): Response | ||
{ | ||
$comment->delete(); | ||
|
||
return response()->noContent(); | ||
} | ||
} |
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,16 @@ | ||
models: | ||
Post: | ||
title: string | ||
body: text | ||
relationships: | ||
hasMany: comment | ||
belongsTo: user | ||
Comment: | ||
body: text | ||
relationships: | ||
belongsTo: post, user | ||
controllers: | ||
Comment: | ||
resource: api | ||
meta: | ||
parent: post |
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,5 @@ | ||
controllers: | ||
Comment: | ||
resource: web | ||
meta: | ||
parent: post |
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,3 @@ | ||
|
||
|
||
Route::resource('posts/{post}/comments', App\Http\Controllers\CommentController::class); |
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,107 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Http\Controllers; | ||
|
||
use App\Models\Comment; | ||
use App\Models\Post; | ||
use App\Models\User; | ||
use function Pest\Faker\fake; | ||
use function Pest\Laravel\assertModelMissing; | ||
use function Pest\Laravel\delete; | ||
use function Pest\Laravel\get; | ||
use function Pest\Laravel\post; | ||
use function Pest\Laravel\put; | ||
|
||
test('index behaves as expected', function (): void { | ||
$post = Post::factory()->create(); | ||
$comments = Comment::factory()->count(3)->create(); | ||
|
||
$response = get(route('comments.index', ['post' => $post])); | ||
|
||
$response->assertOk(); | ||
$response->assertJsonStructure([]); | ||
}); | ||
|
||
|
||
test('store uses form request validation') | ||
->assertActionUsesFormRequest( | ||
\App\Http\Controllers\CommentController::class, | ||
'store', | ||
\App\Http\Requests\CommentStoreRequest::class | ||
); | ||
|
||
test('store saves', function (): void { | ||
$post = Post::factory()->create(); | ||
$body = fake()->text(); | ||
$user = User::factory()->create(); | ||
|
||
$response = post(route('comments.store', ['post' => $post]), [ | ||
'body' => $body, | ||
'post_id' => $post->id, | ||
'user_id' => $user->id, | ||
]); | ||
|
||
$comments = Comment::query() | ||
->where('body', $body) | ||
->where('post_id', $post->id) | ||
->where('user_id', $user->id) | ||
->get(); | ||
expect($comments)->toHaveCount(1); | ||
$comment = $comments->first(); | ||
|
||
$response->assertCreated(); | ||
$response->assertJsonStructure([]); | ||
}); | ||
|
||
|
||
test('show behaves as expected', function (): void { | ||
$comment = Comment::factory()->create(); | ||
$post = Post::factory()->create(); | ||
|
||
$response = get(route('comments.show', ['post' => $post, 'comment' => $comment])); | ||
|
||
$response->assertOk(); | ||
$response->assertJsonStructure([]); | ||
}); | ||
|
||
|
||
test('update uses form request validation') | ||
->assertActionUsesFormRequest( | ||
\App\Http\Controllers\CommentController::class, | ||
'update', | ||
\App\Http\Requests\CommentUpdateRequest::class | ||
); | ||
|
||
test('update behaves as expected', function (): void { | ||
$comment = Comment::factory()->create(); | ||
$post = Post::factory()->create(); | ||
$body = fake()->text(); | ||
$user = User::factory()->create(); | ||
|
||
$response = put(route('comments.update', ['post' => $post, 'comment' => $comment]), [ | ||
'body' => $body, | ||
'post_id' => $post->id, | ||
'user_id' => $user->id, | ||
]); | ||
|
||
$comment->refresh(); | ||
|
||
$response->assertOk(); | ||
$response->assertJsonStructure([]); | ||
|
||
expect($body)->toEqual($comment->body); | ||
expect($post->id)->toEqual($comment->post_id); | ||
expect($user->id)->toEqual($comment->user_id); | ||
}); | ||
|
||
|
||
test('destroy deletes and responds with', function (): void { | ||
$comment = Comment::factory()->create(); | ||
$post = Post::factory()->create(); | ||
|
||
$response = delete(route('comments.destroy', ['post' => $post, 'comment' => $comment])); | ||
|
||
$response->assertNoContent(); | ||
|
||
assertModelMissing($comment); | ||
}); |
Oops, something went wrong.