112 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2025-01-23 00:19:07 +01:00
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Models\Comment;
use App\Models\Post;
use Tests\TestCase;
class CommentTest extends TestCase
{
public function testApplicationWillCreateCommentOnPost(): void
{
$post = Post::factory()->create();
$data = [
'content' => 'This is test comment',
];
$response = $this->post("/api/v1/post/" . $post->id . "/comments", $data);
$response->assertJsonFragment(
[
'content' => $data['content'],
]
);
$postDb = Comment::find($response->getOriginalContent()->getAttributes()['id']);
$this->assertEquals(
[
'content' => $data['content'],
],
[
'content' => $postDb->content,
]
);
$response->assertStatus(201);
}
public function testApplicationWillListComment(): void
{
$post = Post::factory()->create();
$comment = $post->comments()->create([ // @phpstan-ignore method.notFound
'content' => 'This is test comment',
]);
$comment2 = $post->comments()->create([ // @phpstan-ignore method.notFound
'content' => 'This is test comment #2',
]);
$response = $this->get("/api/v1/post/" . $post->id . "/comments/1");
$response->assertJsonFragment(
[
'content' => $comment->content,
]
);
$response->assertJsonFragment(
[
'content' => $comment2->content,
]
);
$this->assertEquals(2, count($response->decodeResponseJson()));
}
public function testApplicationWillDeleteComment(): void
{
$post = Post::factory()->create();
$comment = $post->comments()->create([ // @phpstan-ignore method.notFound
'content' => 'This is test comment',
]);
$response = $this->delete("/api/v1/post/" . $post->id . "/comment/" . $comment->id);
$response->assertStatus(204);
$comment = Comment::find($comment->id);
$this->assertNull($comment);
}
public function testApplicationWillUpdateComment(): void
{
$post = Post::factory()->create();
$comment = $post->comments()->create([ // @phpstan-ignore method.notFound
'content' => 'This is test comment',
]);
$patchData = [
'content' => 'This is test comment #2',
];
$response = $this->put("/api/v1/post/" . $post->id . "/comment/" . $comment->id, $patchData);
$response->assertJsonFragment(
$patchData
);
$commentDb = Comment::find($comment->id);
$this->assertEquals(
$patchData,
[
'content' => $commentDb->content,
]
);
}
}