Skip to content

Commit

Permalink
Recheck whether page is cached for queued requests
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurperton committed Dec 17, 2024
1 parent c0318e9 commit f42e417
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
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());
}
}

0 comments on commit f42e417

Please sign in to comment.