Cache Schema::hasTable lookups on artwork JSON.

Artwork detail was paying repeated information_schema hits per request. Keep public detail edge-case behavior covered by regression tests.
This commit is contained in:
2026-08-29 12:25:37 +02:00
parent b9beb8f5c5
commit bfcbfec357
2 changed files with 96 additions and 13 deletions
@@ -0,0 +1,75 @@
<?php
use App\Models\Artwork;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
uses(RefreshDatabase::class);
it('keeps the artwork detail edge-case responses intact', function (): void {
$artwork = Artwork::factory()->create([
'title' => 'M18B2 Artwork',
'slug' => 'm18b2-artwork',
]);
$this->get(route('art.show', ['id' => $artwork->id, 'slug' => 'm18b2-artwork']))
->assertOk();
$this->get(route('art.show', ['id' => $artwork->id, 'slug' => 'wrong-slug']))
->assertRedirect(route('art.show', ['id' => $artwork->id, 'slug' => 'm18b2-artwork']))
->assertHeader('Location', route('art.show', ['id' => $artwork->id, 'slug' => 'm18b2-artwork']));
$deleted = Artwork::factory()->create();
$deleted->delete();
$this->get(route('art.show', ['id' => $deleted->id, 'slug' => $deleted->slug]))
->assertStatus(410);
$private = Artwork::factory()->private()->create();
$this->get(route('art.show', ['id' => $private->id, 'slug' => $private->slug]))
->assertForbidden();
$this->get(route('art.show', ['id' => 999999, 'slug' => 'missing']))
->assertNotFound();
});
it('performs one primary artwork lookup on the public detail path', function (): void {
$artwork = Artwork::factory()->create([
'slug' => 'm18b2-query-regression',
]);
$primaryLookups = 0;
DB::listen(function ($query) use (&$primaryLookups): void {
$sql = strtolower(preg_replace('/\s+/', ' ', $query->sql));
if (str_contains($sql, 'from "artworks"')
&& str_contains($sql, 'where "id" = ?')
&& str_contains($sql, 'limit 1')) {
$primaryLookups++;
}
});
$this->get(route('art.show', ['id' => $artwork->id, 'slug' => $artwork->slug]))
->assertOk();
expect($primaryLookups)->toBe(1);
});
it('keeps the authenticated artwork detail query count bounded', function (): void {
$artwork = Artwork::factory()->create([
'slug' => 'm18b2-auth-query-regression',
]);
$viewer = User::factory()->create();
$queryCount = 0;
DB::listen(function () use (&$queryCount): void {
$queryCount++;
});
$this->actingAs($viewer)
->get(route('art.show', ['id' => $artwork->id, 'slug' => $artwork->slug]))
->assertOk();
expect($queryCount)->toBeLessThanOrEqual(50);
});