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

59 lines
No EOL
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Models\Post;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PostTest extends TestCase
{
use RefreshDatabase;
public function test_user_can_view_posts(): void
{
$user = User::factory()->create();
$posts = Post::factory(5)->create();
$response = $this->actingAs($user)
->getJson('/api/posts');
$response->assertStatus(200)
->assertJsonStructure([
'posts' => [
'*' => [
'id',
'title',
'summary',
'author',
],
],
'pagination',
]);
}
public function test_author_can_create_post(): void
{
$user = User::factory()->create(['role' => 'author']);
$response = $this->actingAs($user)
->postJson('/api/posts', [
'title' => 'Test Post',
'content' => 'Test Content',
'summary' => 'Test Summary',
]);
$response->assertStatus(200)
->assertJson([
'message' => 'Post created successfully',
]);
$this->assertDatabaseHas('posts', [
'title' => 'Test Post',
'author_id' => $user->id,
]);
}
}