aiblog/tests/Feature/CommentTest.php
2024-11-17 11:30:01 +08:00

58 lines
No EOL
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Models\Comment;
use App\Models\Post;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class CommentTest extends TestCase
{
use RefreshDatabase;
public function test_user_can_add_comment(): void
{
$user = User::factory()->create();
$post = Post::factory()->create();
$response = $this->actingAs($user)
->postJson("/api/posts/{$post->id}/comments", [
'content' => 'Test Comment',
]);
$response->assertStatus(200)
->assertJson([
'message' => 'Comment added successfully',
]);
$this->assertDatabaseHas('comments', [
'content' => 'Test Comment',
'post_id' => $post->id,
'author_id' => $user->id,
]);
}
public function test_user_can_delete_own_comment(): void
{
$user = User::factory()->create();
$comment = Comment::factory()->create([
'author_id' => $user->id,
]);
$response = $this->actingAs($user)
->deleteJson("/api/comments/{$comment->id}");
$response->assertStatus(200)
->assertJson([
'message' => 'Comment deleted successfully',
]);
$this->assertDatabaseMissing('comments', [
'id' => $comment->id,
]);
}
}