Files
laralog/tests/Feature/PageCacheTest.php
T
goukiandCursor ae47447062
Build and Deploy / verify (push) Successful in 40s
Build and Deploy / deploy (push) Successful in 36s
feat: restore sablog browse modes and clean imported HTML
Keep the public site readable after import (nbsp, highlighter, wrapping code, compact pager) and finish the admin extras already in this tree: sticky levels, preview, page cache, and live llms.txt.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-18 21:33:02 +00:00

85 lines
2.2 KiB
PHP

<?php
namespace Tests\Feature;
use App\Blog\Support\PageCacheMiddleware;
use App\Models\Category;
use App\Models\Post;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PageCacheTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seed();
$user = User::create(['name' => '作者', 'email' => 'w@t.com', 'password' => bcrypt('x')]);
$cat = Category::create(['name' => '技术', 'slug' => 'tech']);
Post::create([
'user_id' => $user->id,
'category_id' => $cat->id,
'title' => '缓存测试',
'slug' => 'cache-test',
'content' => '内容',
'content_format' => 'markdown',
'status' => 'published',
'published_at' => now(),
]);
}
public function test_anonymous_page_is_cached(): void
{
PageCacheMiddleware::flush();
$this->get('/')->assertOk();
$this->assertSame(1, PageCacheMiddleware::count());
// 第二次命中缓存
$this->get('/')->assertOk();
$this->assertSame(1, PageCacheMiddleware::count());
$this->get('/?view=full')->assertOk();
$this->assertSame(2, PageCacheMiddleware::count());
}
public function test_authenticated_user_not_cached(): void
{
PageCacheMiddleware::flush();
$user = User::where('email', 'admin@laralog.test')->first();
$this->actingAs($user)->get('/')->assertOk();
$this->assertSame(0, PageCacheMiddleware::count());
}
public function test_track_view_increments_without_caching(): void
{
PageCacheMiddleware::flush();
$post = Post::where('slug', 'cache-test')->first();
$views = $post->views;
$this->get($post->url)->assertOk();
$this->get('/track-view/'.$post->id)->assertStatus(204);
$post->refresh();
$this->assertSame($views + 1, $post->views);
}
public function test_flush_clears_pages(): void
{
$this->get('/')->assertOk();
$this->assertSame(1, PageCacheMiddleware::count());
PageCacheMiddleware::flush();
$this->assertSame(0, PageCacheMiddleware::count());
}
}