Skip to content

Commit

Permalink
Merge branch 'release/1.1.6'
Browse files Browse the repository at this point in the history
aronnebrivio committed Aug 19, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 6512af6 + d5dfd0c commit f070490
Showing 5 changed files with 546 additions and 434 deletions.
28 changes: 15 additions & 13 deletions app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
@@ -12,38 +12,34 @@
class CommentController extends BaseController
{
/**
* @param Request $request
* @param Request $request
* @param int|string $postId
*
* @throws \Illuminate\Validation\ValidationException
*
* @return mixed
*/
public function getAll(Request $request)
public function getAll(Request $request, $postId)
{
$this->validate($request, [
'post_id' => 'required|integer|min:1',
]);

return Comment::where('post_id', $request->all()['post_id'])
return Comment::where('post_id', $postId)
->orderBy('created_at', 'desc')
->get();
}

/**
* @param Request $request
* @param Request $request
* @param int|string $postId
*
* @throws \Illuminate\Validation\ValidationException
*
* @return Comment
*/
public function new(Request $request)
public function new(Request $request, $postId)
{
$this->validate($request, [
'text' => 'required',
'post_id' => 'required',
]);

$postId = $request->all()['post_id'];
Post::findOrFail($postId);

$comment = new Comment();
@@ -58,13 +54,16 @@ public function new(Request $request)
/**
* @param Request $request
* @param int|string $id
* @param int|string $postId
*
* @throws \Illuminate\Validation\ValidationException
*
* @return Comment
*/
public function update(Request $request, $id)
public function update(Request $request, $postId, $id)
{
Post::findOrFail($postId);

$comment = Comment::findOrFail($id);

Gate::authorize('update', $comment);
@@ -77,15 +76,18 @@ public function update(Request $request, $id)

/**
* @param int|string $id
* @param int|string $postId
*
* @throws \Exception
*
* @return array
*
* @psalm-return array<empty, empty>
*/
public function delete($id): array
public function delete($postId, $id)
{
Post::findOrFail($postId);

$comment = Comment::findOrFail($id);

Gate::authorize('delete', $comment);
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
"lumen",
"api"
],
"version": "1.1.5",
"version": "1.1.6",
"license": "MIT",
"type": "project",
"require": {
897 changes: 499 additions & 398 deletions composer.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -28,23 +28,23 @@
$router->group(['prefix' => 'posts'], function () use ($router) {
$router->get('', PostController::class . '@getAll');
$router->get('{id}', PostController::class . '@get');
});

$router->group(['prefix' => 'comments'], function () use ($router) {
$router->get('', CommentController::class . '@getAll');
$router->group(['prefix' => '{postId}/comments'], function () use ($router) {
$router->get('', CommentController::class . '@getAll');
});
});

$router->group(['middleware' => 'auth'], function () use ($router) {
$router->group(['prefix' => 'posts'], function () use ($router) {
$router->put('{id}', PostController::class . '@update');
$router->delete('{id}', PostController::class . '@delete');
$router->post('', PostController::class . '@new');
});

$router->group(['prefix' => 'comments'], function () use ($router) {
$router->put('{id}', CommentController::class . '@update');
$router->delete('{id}', CommentController::class . '@delete');
$router->post('', CommentController::class . '@new');
$router->group(['prefix' => '{postId}/comments'], function () use ($router) {
$router->put('{id}', CommentController::class . '@update');
$router->delete('{id}', CommentController::class . '@delete');
$router->post('', CommentController::class . '@new');
});
});

$router->group(['prefix' => 'users'], function () use ($router) {
37 changes: 23 additions & 14 deletions tests/CommentTest.php
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
*/
class CommentTest extends TestCase
{
public function testGetCommentsByPostId()
public function testGetComments()
{
$user = User::factory()->create();
$post = Post::factory()->create([
@@ -24,7 +24,7 @@ public function testGetCommentsByPostId()

$result = Comment::find($comment->id);

$this->json('GET', 'comments', ['post_id' => $post->id])
$this->json('GET', 'posts/' . $post->id . '/comments')
->seeStatusCode(200)
->seeJsonEquals([$result->toArray()]);
}
@@ -46,16 +46,19 @@ public function testCommentEdit()
]);
$newText = Str::random(300);

$this->put('comments/' . $comment->id, ['text' => $newText])
$this->put('posts/' . $post->id . '/comments/' . $comment->id, ['text' => $newText])
->seeStatusCode(401);

$this->notSeeInDatabase('comments', ['id' => $comment->id, 'text' => $newText]);

$this->actingAs($user);
$this->put('comments/' . $comment->id, ['text' => $newText])
$this->put('posts/' . ($post->id + 1) . '/comments/' . $comment->id, ['text' => $newText])
->seeStatusCode(404);

$this->put('posts/' . $post->id . '/comments/' . $comment->id, ['text' => $newText])
->seeStatusCode(200);

$this->put('comments/' . ($comment->id + 2), ['text' => $newText])
$this->put('posts/' . $post->id . '/comments/' . ($comment->id + 2), ['text' => $newText])
->seeStatusCode(404);

$this->seeInDatabase('comments', ['id' => $comment->id, 'text' => $newText]);
@@ -65,7 +68,7 @@ public function testCommentEdit()
'user_id' => $user2->id,
'post_id' => $post->id,
]);
$this->put('comments/' . $comment2->id, ['text' => $newText])
$this->put('posts/' . $post->id . '/comments/' . $comment2->id, ['text' => $newText])
->seeStatusCode(401);
}

@@ -85,19 +88,22 @@ public function testCommentDelete()
'post_id' => $post->id,
]);

$this->delete('comments/' . $comment->id)
$this->delete('posts/' . $post->id . '/comments/' . $comment->id)
->seeStatusCode(401);
$this->seeInDatabase('comments', ['id' => $comment->id]);

$this->actingAs($user);
$this->delete('comments/' . $comment->id)
$this->delete('posts/' . ($post->id + 1) . '/comments/' . $comment->id)
->seeStatusCode(404);

$this->delete('posts/' . $post->id . '/comments/' . $comment->id)
->seeStatusCode(200);
$this->notSeeInDatabase('comments', ['id' => $comment->id]);

$this->delete('comments/' . 1)
$this->delete('posts/' . $post->id . '/comments/' . 1)
->seeStatusCode(404);

$this->delete('comments/' . $comment2->id)
$this->delete('posts/' . $post->id . '/comments/' . $comment2->id)
->seeStatusCode(401);
}

@@ -109,12 +115,15 @@ public function testCommentNew()
]);
$sampleText = Str::random(300);

$this->post('comments', ['post_id' => $post->id, 'text' => $sampleText])
$this->post('posts/' . $post->id . '/comments', ['text' => $sampleText])
->seeStatusCode(401);
$this->notSeeInDatabase('comments', ['user_id' => $user->id, 'post_id' => $post->id, 'text' => $sampleText]);

$this->actingAs($user);
$this->post('comments', ['post_id' => $post->id, 'text' => $sampleText])
$this->post('posts/' . ($post->id + 1) . '/comments', ['text' => $sampleText])
->seeStatusCode(404);

$this->post('posts/' . $post->id . '/comments', ['text' => $sampleText])
->seeStatusCode(200);
$this->seeInDatabase('comments', ['user_id' => $user->id, 'post_id' => $post->id, 'text' => $sampleText]);
}
@@ -144,11 +153,11 @@ public function testCommentNewValidation()
'user_id' => $user->id,
]);

$this->post('comments', ['post_id' => $post->id])
$this->post('posts/' . $post->id . '/comments', [])
->seeStatusCode(422);
$this->notSeeInDatabase('comments', ['user_id' => $user->id, 'post_id' => $post->id]);

$this->post('comments', ['post_id' => $post->id, 'text' => 'txt'])
$this->post('posts/' . $post->id . '/comments', ['text' => 'txt'])
->seeStatusCode(200);
$this->seeInDatabase('comments', ['user_id' => $user->id, 'text' => 'txt', 'post_id' => $post->id]);
}

0 comments on commit f070490

Please sign in to comment.