Add extensive controller and model tests
This commit is contained in:
92
tests/Feature/PostThankControllerTest.php
Normal file
92
tests/Feature/PostThankControllerTest.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Forum;
|
||||
use App\Models\Post;
|
||||
use App\Models\PostThank;
|
||||
use App\Models\Thread;
|
||||
use App\Models\User;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
function makeThanksThread(): Thread
|
||||
{
|
||||
$category = Forum::create([
|
||||
'name' => 'Category',
|
||||
'description' => null,
|
||||
'type' => 'category',
|
||||
'parent_id' => null,
|
||||
'position' => 1,
|
||||
]);
|
||||
|
||||
$forum = Forum::create([
|
||||
'name' => 'Forum',
|
||||
'description' => null,
|
||||
'type' => 'forum',
|
||||
'parent_id' => $category->id,
|
||||
'position' => 1,
|
||||
]);
|
||||
|
||||
return Thread::create([
|
||||
'forum_id' => $forum->id,
|
||||
'user_id' => null,
|
||||
'title' => 'Thanks Thread',
|
||||
'body' => 'Thread Body',
|
||||
]);
|
||||
}
|
||||
|
||||
it('lists thanks given by a user', function (): void {
|
||||
$thread = makeThanksThread();
|
||||
$author = User::factory()->create(['name' => 'Author']);
|
||||
$thanker = User::factory()->create(['name' => 'ThanksGiver']);
|
||||
|
||||
$post = Post::create([
|
||||
'thread_id' => $thread->id,
|
||||
'user_id' => $author->id,
|
||||
'body' => 'Helpful post',
|
||||
]);
|
||||
|
||||
$thank = PostThank::create([
|
||||
'post_id' => $post->id,
|
||||
'user_id' => $thanker->id,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($thanker);
|
||||
$response = $this->getJson("/api/user/{$thanker->id}/thanks/given");
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonFragment([
|
||||
'id' => $thank->id,
|
||||
'post_id' => $post->id,
|
||||
'thread_id' => $thread->id,
|
||||
'thread_title' => 'Thanks Thread',
|
||||
'post_author_name' => 'Author',
|
||||
]);
|
||||
});
|
||||
|
||||
it('lists thanks received for a user', function (): void {
|
||||
$thread = makeThanksThread();
|
||||
$author = User::factory()->create(['name' => 'Author']);
|
||||
$thanker = User::factory()->create(['name' => 'ThanksGiver']);
|
||||
|
||||
$post = Post::create([
|
||||
'thread_id' => $thread->id,
|
||||
'user_id' => $author->id,
|
||||
'body' => 'Helpful post',
|
||||
]);
|
||||
|
||||
$thank = PostThank::create([
|
||||
'post_id' => $post->id,
|
||||
'user_id' => $thanker->id,
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($author);
|
||||
$response = $this->getJson("/api/user/{$author->id}/thanks/received");
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertJsonFragment([
|
||||
'id' => $thank->id,
|
||||
'post_id' => $post->id,
|
||||
'thread_id' => $thread->id,
|
||||
'thread_title' => 'Thanks Thread',
|
||||
'thanker_name' => 'ThanksGiver',
|
||||
]);
|
||||
});
|
||||
Reference in New Issue
Block a user