Add extensive controller and model tests
This commit is contained in:
74
tests/Unit/ForumModelTest.php
Normal file
74
tests/Unit/ForumModelTest.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Forum;
|
||||
use App\Models\Post;
|
||||
use App\Models\Thread;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
it('exposes forum relationships and latest helpers', 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' => 2,
|
||||
]);
|
||||
|
||||
$child = Forum::create([
|
||||
'name' => 'Child',
|
||||
'description' => null,
|
||||
'type' => 'forum',
|
||||
'parent_id' => $forum->id,
|
||||
'position' => 1,
|
||||
]);
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$threadOld = Thread::create([
|
||||
'forum_id' => $forum->id,
|
||||
'user_id' => $user->id,
|
||||
'title' => 'Old Thread',
|
||||
'body' => 'Old',
|
||||
'created_at' => Carbon::now()->subDays(2),
|
||||
]);
|
||||
|
||||
$threadNew = Thread::create([
|
||||
'forum_id' => $forum->id,
|
||||
'user_id' => $user->id,
|
||||
'title' => 'New Thread',
|
||||
'body' => 'New',
|
||||
'created_at' => Carbon::now()->subDay(),
|
||||
]);
|
||||
|
||||
$postOld = Post::create([
|
||||
'thread_id' => $threadOld->id,
|
||||
'user_id' => $user->id,
|
||||
'body' => 'Old post',
|
||||
'created_at' => Carbon::now()->subDays(2),
|
||||
]);
|
||||
|
||||
$postNew = Post::create([
|
||||
'thread_id' => $threadNew->id,
|
||||
'user_id' => $user->id,
|
||||
'body' => 'New post',
|
||||
'created_at' => Carbon::now()->subDay(),
|
||||
]);
|
||||
|
||||
$forum->load(['parent', 'children', 'threads', 'posts', 'latestThread', 'latestPost']);
|
||||
|
||||
expect($forum->parent?->id)->toBe($category->id);
|
||||
expect($forum->children->first()->id)->toBe($child->id);
|
||||
expect($forum->threads)->toHaveCount(2);
|
||||
expect($forum->posts)->toHaveCount(2);
|
||||
expect($forum->latestThread?->id)->toBe($threadNew->id);
|
||||
expect($forum->latestPost?->id)->toBe($postNew->id);
|
||||
});
|
||||
Reference in New Issue
Block a user