Skip to content
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
4 changes: 2 additions & 2 deletions src/StaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ private function handleInvalidateElementCaches(InvalidateElementCachesEvent $eve
private function handleRegisterCacheOptions(RegisterCacheOptionsEvent $event): void
{
$event->options[] = [
'key' => 'craft-cloud-caches',
'label' => Craft::t('app', 'Craft Cloud caches'),
'key' => 'craft-cloud-static-cache',
'label' => Craft::t('app', 'Craft Cloud static cache'),
'action' => [$this, 'purgeAll'],
];
Comment thread
timkelty marked this conversation as resolved.
}
Expand Down
47 changes: 47 additions & 0 deletions src/cli/controllers/StaticCacheController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,47 @@

use craft\cloud\Module;
use craft\console\Controller;
use yii\console\Exception;
use yii\console\ExitCode;
Comment thread
timkelty marked this conversation as resolved.

class StaticCacheController extends Controller
{
public function actionPurgeAll(): int
{
$this->do('Purging static cache', function() {
$module = Module::getInstance();
$environmentId = $this->environmentId();
$module->getStaticCache()->purgeTags(
"$environmentId:uri",
"$environmentId:cdn",
);
});

return ExitCode::OK;
}

public function actionPurgeCdn(): int
{
$this->do('Purging CDN static cache', function() {
$module = Module::getInstance();
$environmentId = $this->environmentId();
$module->getStaticCache()->purgeTags("$environmentId:cdn");
});

return ExitCode::OK;
}

public function actionPurgeOrigin(): int
{
$this->do('Purging origin static cache', function() {
$module = Module::getInstance();
$environmentId = $this->environmentId();
$module->getStaticCache()->purgeTags("$environmentId:uri");
});

return ExitCode::OK;
}

public function actionPurgePrefixes(string ...$prefixes): int
{
$this->do('Purging prefixes', function() use ($prefixes) {
Expand All @@ -25,4 +62,14 @@ public function actionPurgeTags(string ...$tags): int

return ExitCode::OK;
}

private function environmentId(): string
{
$environmentId = Module::getInstance()->getConfig()->environmentId;
if (!$environmentId) {
throw new Exception('Static cache purges require an environment ID.');
}

return $environmentId;
}
}
91 changes: 91 additions & 0 deletions tests/unit/StaticCacheControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace craft\cloud\tests\unit;

use Codeception\Test\Unit;
use Craft;
use craft\cloud\cli\controllers\StaticCacheController;
use craft\cloud\Module;
use craft\cloud\StaticCache;
use craft\cloud\StaticCacheTag;

class StaticCacheControllerTest extends Unit
{
private ?Module $previousModule = null;
private TestStaticCache $staticCache;

protected function _before(): void
{
parent::_before();

$this->previousModule = Module::getInstance();
$module = new Module('cloud');
$module->getConfig()->environmentId = '123-environment-id';
$this->staticCache = new TestStaticCache();
$module->set('staticCache', $this->staticCache);
Module::setInstance($module);
}

protected function _after(): void
{
Module::setInstance($this->previousModule);

parent::_after();
}

public function testPurgeActions(): void
{
$controller = new StaticCacheController('static-cache', Craft::$app);

$controller->actionPurgeAll();
$controller->actionPurgeOrigin();
$controller->actionPurgeCdn();

$this->assertSame([
['123-environment-id:uri', '123-environment-id:cdn'],
['123-environment-id:uri'],
['123-environment-id:cdn'],
], $this->staticCache->purges);
}

public function testPurgeFailurePropagates(): void
{
$this->staticCache->fail = true;
$this->expectException(\RuntimeException::class);

(new StaticCacheController('static-cache', Craft::$app))->actionPurgeAll();
}
Comment thread
timkelty marked this conversation as resolved.

public function testPurgeRequiresEnvironmentId(): void
{
Module::getInstance()->getConfig()->environmentId = null;
$this->expectException(\yii\console\Exception::class);
$this->expectExceptionMessage('Static cache purges require an environment ID.');

(new StaticCacheController('static-cache', Craft::$app))->actionPurgeAll();
Comment thread
timkelty marked this conversation as resolved.
}

public function testPurgeRequiresNonEmptyEnvironmentId(): void
{
Module::getInstance()->getConfig()->environmentId = '';
$this->expectException(\yii\console\Exception::class);
$this->expectExceptionMessage('Static cache purges require an environment ID.');

(new StaticCacheController('static-cache', Craft::$app))->actionPurgeAll();
Comment thread
timkelty marked this conversation as resolved.
}
}

class TestStaticCache extends StaticCache
{
public array $purges = [];
public bool $fail = false;

public function purgeTags(string|StaticCacheTag ...$tags): void
{
if ($this->fail) {
throw new \RuntimeException('Purge failed.');
}

$this->purges[] = $tags;
}
}
Loading