Files
speedBB/tests/Unit/ThreadModelTest.php
tracer 160430e128
All checks were successful
CI/CD Pipeline / test (push) Successful in 10s
CI/CD Pipeline / deploy (push) Successful in 25s
Add extensive controller and model tests
2026-02-07 22:14:42 +01:00

74 lines
2.0 KiB
PHP

<?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);
});