Add extensive controller and model tests
This commit is contained in:
73
tests/Unit/ThreadModelTest.php
Normal file
73
tests/Unit/ThreadModelTest.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Attachment;
|
||||
use App\Models\Forum;
|
||||
use App\Models\Post;
|
||||
use App\Models\Thread;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
it('casts solved flag and exposes relationships', function (): void {
|
||||
$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,
|
||||
]);
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$thread = Thread::create([
|
||||
'forum_id' => $forum->id,
|
||||
'user_id' => $user->id,
|
||||
'title' => 'Thread',
|
||||
'body' => 'Body',
|
||||
'solved' => 1,
|
||||
]);
|
||||
|
||||
$oldPost = Post::create([
|
||||
'thread_id' => $thread->id,
|
||||
'user_id' => $user->id,
|
||||
'body' => 'Old post',
|
||||
'created_at' => Carbon::now()->subDay(),
|
||||
]);
|
||||
|
||||
$newPost = Post::create([
|
||||
'thread_id' => $thread->id,
|
||||
'user_id' => $user->id,
|
||||
'body' => 'New post',
|
||||
'created_at' => Carbon::now(),
|
||||
]);
|
||||
|
||||
$attachment = Attachment::create([
|
||||
'thread_id' => $thread->id,
|
||||
'post_id' => null,
|
||||
'attachment_extension_id' => null,
|
||||
'attachment_group_id' => null,
|
||||
'user_id' => $user->id,
|
||||
'disk' => 'local',
|
||||
'path' => 'attachments/threads/'.$thread->id.'/file.pdf',
|
||||
'original_name' => 'file.pdf',
|
||||
'extension' => 'pdf',
|
||||
'mime_type' => 'application/pdf',
|
||||
'size_bytes' => 10,
|
||||
]);
|
||||
|
||||
$thread->load(['forum', 'user', 'posts', 'attachments', 'latestPost']);
|
||||
|
||||
expect($thread->solved)->toBeTrue();
|
||||
expect($thread->forum->id)->toBe($forum->id);
|
||||
expect($thread->user->id)->toBe($user->id);
|
||||
expect($thread->posts)->toHaveCount(2);
|
||||
expect($thread->attachments->first()->id)->toBe($attachment->id);
|
||||
expect($thread->latestPost->id)->toBe($newPost->id);
|
||||
});
|
||||
Reference in New Issue
Block a user