Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Static warm: recheck whether page is cached for queued requests #11273

Merged
merged 3 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Console/Commands/StaticWarm.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ private function warm(): void
$queue = config('statamic.static_caching.warm_queue');
$this->line(sprintf('Adding %s requests onto %squeue...', count($requests), $queue ? $queue.' ' : ''));

$jobClass = $this->option('uncached')
? StaticWarmUncachedJob::class
: StaticWarmJob::class;

foreach ($requests as $request) {
StaticWarmJob::dispatch($request, $this->clientConfig())
$jobClass::dispatch($request, $this->clientConfig())
->onConnection($this->queueConnection)
->onQueue($queue);
}
Expand Down
20 changes: 20 additions & 0 deletions src/Console/Commands/StaticWarmUncachedJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Statamic\Console\Commands;

use Illuminate\Http\Request;
use Statamic\StaticCaching\Cacher;

class StaticWarmUncachedJob extends StaticWarmJob
{
public function handle()
{
$cacher = app(Cacher::class);

if ($cacher->hasCachedPage(Request::create($this->request->getUri()))) {
return;
}

parent::handle();
}
}
54 changes: 54 additions & 0 deletions tests/Console/Commands/StaticWarmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Console\Commands\StaticWarmJob;
use Statamic\Console\Commands\StaticWarmUncachedJob;
use Statamic\Facades\Collection;
use Statamic\StaticCaching\Cacher;
use Tests\PreventSavingStacheItemsToDisk;
Expand Down Expand Up @@ -173,6 +174,59 @@ public function it_queues_the_requests()
});
}

#[Test]
public function it_queues_the_request_when_the_uncached_option_is_used()
{
config([
'statamic.static_caching.strategy' => 'half',
'queue.default' => 'redis',
]);

Queue::fake();

$this->artisan('statamic:static:warm', ['--queue' => true, '--uncached' => true])
->expectsOutputToContain('Adding 2 requests')
->assertExitCode(0);

Queue::assertCount(2);

Queue::assertPushed(StaticWarmUncachedJob::class, function ($job) {
return $job->request->getUri()->getPath() === '/about';
});
Queue::assertPushed(StaticWarmUncachedJob::class, function ($job) {
return $job->request->getUri()->getPath() === '/contact';
});
}

#[Test]
public function it_doesnt_queue_the_request_when_the_uncached_option_is_used_and_the_page_is_cached()
{
config([
'statamic.static_caching.strategy' => 'half',
'queue.default' => 'redis',
]);

$mock = Mockery::mock(Cacher::class);
$mock->shouldReceive('hasCachedPage')->times(2)->andReturn(true, false);
$mock->allows('isExcluded')->andReturn(false);
app()->instance(Cacher::class, $mock);

Queue::fake();

$this->artisan('statamic:static:warm', ['--queue' => true, '--uncached' => true])
->expectsOutputToContain('Adding 1 requests')
->assertExitCode(0);

Queue::assertCount(1);

Queue::assertNotPushed(StaticWarmUncachedJob::class, function ($job) {
return $job->request->getUri()->getPath() === '/about';
});
Queue::assertPushed(StaticWarmUncachedJob::class, function ($job) {
return $job->request->getUri()->getPath() === '/contact';
});
}

#[Test, DataProvider('queueConnectionsProvider')]
public function it_queues_the_requests_with_appropriate_queue_and_connection(
$configuredQueue,
Expand Down
53 changes: 53 additions & 0 deletions tests/Console/Commands/StaticWarmUncachedJobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Tests\Console\Commands;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Mockery;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Console\Commands\StaticWarmUncachedJob;
use Statamic\StaticCaching\Cacher;
use Tests\TestCase;

class StaticWarmUncachedJobTest extends TestCase
{
#[Test]
public function it_sends_a_get_request()
{
$mock = new MockHandler([
new Response(200),
]);

$handlerStack = HandlerStack::create($mock);

$job = new StaticWarmUncachedJob(new Request('GET', '/about'), ['handler' => $handlerStack]);

$job->handle();

$this->assertEquals('/about', $mock->getLastRequest()->getUri()->getPath());
}

#[Test]
public function it_does_not_send_a_request_if_the_page_is_cached()
{
$mockCacher = Mockery::mock(Cacher::class);
$mockCacher->shouldReceive('hasCachedPage')->once()->andReturn(true);
$mockCacher->allows('isExcluded')->andReturn(false);
app()->instance(Cacher::class, $mockCacher);

$mock = new MockHandler([
new Response(200),
]);

$handlerStack = HandlerStack::create($mock);

$job = new StaticWarmUncachedJob(new Request('GET', '/about'), ['handler' => $handlerStack]);

$job->handle();

$this->assertNull($mock->getLastRequest());
}
}
Loading