diff --git a/src/Console/Commands/StaticWarm.php b/src/Console/Commands/StaticWarm.php index 03e33da0b5..3510e3ad26 100644 --- a/src/Console/Commands/StaticWarm.php +++ b/src/Console/Commands/StaticWarm.php @@ -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); } diff --git a/src/Console/Commands/StaticWarmUncachedJob.php b/src/Console/Commands/StaticWarmUncachedJob.php new file mode 100644 index 0000000000..11c0c246d6 --- /dev/null +++ b/src/Console/Commands/StaticWarmUncachedJob.php @@ -0,0 +1,20 @@ +hasCachedPage(Request::create($this->request->getUri()))) { + return; + } + + parent::handle(); + } +} diff --git a/tests/Console/Commands/StaticWarmTest.php b/tests/Console/Commands/StaticWarmTest.php index bcb7f5fdd8..70b5ac0cc0 100644 --- a/tests/Console/Commands/StaticWarmTest.php +++ b/tests/Console/Commands/StaticWarmTest.php @@ -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; @@ -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, diff --git a/tests/Console/Commands/StaticWarmUncachedJobTest.php b/tests/Console/Commands/StaticWarmUncachedJobTest.php new file mode 100644 index 0000000000..5b5459a53b --- /dev/null +++ b/tests/Console/Commands/StaticWarmUncachedJobTest.php @@ -0,0 +1,53 @@ + $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()); + } +}