Files
SkinbaseNova/tests/Feature/ForumBoardPagePerformanceTest.php

89 lines
2.9 KiB
PHP

<?php
use App\Http\Middleware\HandleInertiaRequests;
use App\Models\User;
use cPad\Plugins\Forum\Models\ForumBoard;
use cPad\Plugins\Forum\Models\ForumCategory;
use cPad\Plugins\Forum\Models\ForumPost;
use cPad\Plugins\Forum\Models\ForumTopic;
use Illuminate\Support\Facades\DB;
beforeEach(function (): void {
$this->withoutMiddleware(HandleInertiaRequests::class);
});
it('keeps board page opening-post queries bounded across many topics', function (): void {
$author = User::query()->create([
'username' => 'illustrator',
'username_changed_at' => now()->subDays(120),
'last_username_change_at' => now()->subDays(120),
'onboarding_step' => 'complete',
'name' => 'Illustration Author',
'email' => 'illustration@example.com',
'email_verified_at' => now(),
'password' => 'password',
'is_active' => true,
]);
$category = ForumCategory::query()->create([
'name' => 'Art Query Budget',
'title' => 'Art',
'slug' => 'art-query-budget',
'description' => 'Art discussion',
'is_active' => true,
'position' => 1,
]);
$board = ForumBoard::query()->create([
'category_id' => $category->id,
'title' => 'Illustration',
'slug' => 'illustration-query-budget',
'description' => 'Illustration board',
'is_active' => true,
'position' => 1,
]);
for ($index = 1; $index <= 15; $index++) {
$topic = ForumTopic::query()->create([
'board_id' => $board->id,
'user_id' => $author->id,
'title' => 'Topic ' . $index,
'slug' => 'topic-' . $index,
'replies_count' => 2,
'last_post_at' => now()->subMinutes($index),
]);
ForumPost::query()->create([
'thread_id' => $topic->id,
'topic_id' => $topic->id,
'user_id' => $author->id,
'content' => 'Opening post for topic ' . $index,
'created_at' => now()->subMinutes($index + 30),
'updated_at' => now()->subMinutes($index + 30),
]);
ForumPost::query()->create([
'thread_id' => $topic->id,
'topic_id' => $topic->id,
'user_id' => $author->id,
'content' => 'Reply for topic ' . $index,
'created_at' => now()->subMinutes($index),
'updated_at' => now()->subMinutes($index),
]);
}
$forumPostQueryCount = 0;
DB::listen(function ($query) use (&$forumPostQueryCount): void {
if (preg_match('/\b(from|join)\s+["`\[]?forum_posts\b/i', $query->sql) === 1) {
$forumPostQueryCount++;
}
});
$this->get(route('forum.board.show', ['boardSlug' => $board->slug]))
->assertOk()
->assertSee('Illustration')
->assertSee('Topic 1')
->assertSee('Opening post for topic 1');
expect($forumPostQueryCount)->toBeLessThanOrEqual(3);
});