Skip to content

Commit

Permalink
Merge pull request #25 from shopware/clear-cache-always
Browse files Browse the repository at this point in the history
feat: add option to always clear cache after deployment, fixes #21
  • Loading branch information
shyim authored Oct 24, 2024
2 parents 3b1f3b2 + 34b37c7 commit f8ca4b0
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Config/ConfigFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ private static function fillConfig(ProjectConfiguration $projectConfiguration, a
}
}

if (isset($deployment['cache']) && \is_array($deployment['cache'])) {
if (isset($deployment['cache']['always_clear']) && \is_bool($deployment['cache']['always_clear'])) {
$projectConfiguration->alwaysClearCache = $deployment['cache']['always_clear'];
}
}

if (isset($deployment['hooks']) && \is_array($deployment['hooks'])) {
self::fillHooks($projectConfiguration->hooks, $deployment['hooks']);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Config/ProjectConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ProjectConfiguration
*/
public array $oneTimeTasks = [];

public bool $alwaysClearCache = false;

public function __construct()
{
$this->hooks = new ProjectHooks();
Expand Down
29 changes: 29 additions & 0 deletions src/Integration/ClearAlwaysCacheSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Shopware\Deployment\Integration;

use Shopware\Deployment\Config\ProjectConfiguration;
use Shopware\Deployment\Event\PostDeploy;
use Shopware\Deployment\Helper\ProcessHelper;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener(event: PostDeploy::class, method: '__invoke')]
readonly class ClearAlwaysCacheSubscriber
{
public function __construct(
private ProjectConfiguration $projectConfiguration,
private ProcessHelper $processHelper,
) {
}

public function __invoke(PostDeploy $event): void
{
if (!$this->projectConfiguration->alwaysClearCache) {
return;
}

$this->processHelper->console(['cache:pool:clear', 'cache.http', 'cache.object']);
}
}
6 changes: 6 additions & 0 deletions tests/Config/ConfigFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,10 @@ public function testExistingConfigWithStoreCOnfig(): void
$config = ConfigFactory::create(__DIR__ . '/_fixtures/license-domain');
static::assertSame('example.com', $config->store->licenseDomain);
}

public function testExistingConfigWithAlwaysClearCache(): void
{
$config = ConfigFactory::create(__DIR__ . '/_fixtures/always-clear-cache');
static::assertTrue($config->alwaysClearCache);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
deployment:
cache:
always_clear: true
55 changes: 55 additions & 0 deletions tests/Integration/ClearAlwaysCacheSubscriberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Shopware\Deployment\Tests\Integration;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Shopware\Deployment\Config\ProjectConfiguration;
use Shopware\Deployment\Event\PostDeploy;
use Shopware\Deployment\Helper\ProcessHelper;
use Shopware\Deployment\Integration\ClearAlwaysCacheSubscriber;
use Shopware\Deployment\Struct\RunConfiguration;
use Symfony\Component\Console\Output\NullOutput;

#[CoversClass(ClearAlwaysCacheSubscriber::class)]
class ClearAlwaysCacheSubscriberTest extends TestCase
{
private ProjectConfiguration&MockObject $projectConfiguration;
private ProcessHelper&MockObject $processHelper;
private ClearAlwaysCacheSubscriber $subscriber;

protected function setUp(): void
{
$this->projectConfiguration = $this->createMock(ProjectConfiguration::class);
$this->processHelper = $this->createMock(ProcessHelper::class);
$this->subscriber = new ClearAlwaysCacheSubscriber($this->projectConfiguration, $this->processHelper);
}

public function testInvokeWithAlwaysClearCacheEnabled(): void
{
$this->projectConfiguration->alwaysClearCache = true;

$this->processHelper
->expects($this->once())
->method('console')
->with(['cache:pool:clear', 'cache.http', 'cache.object']);

$event = new PostDeploy(new RunConfiguration(), new NullOutput());
$this->subscriber->__invoke($event);
}

public function testInvokeWithAlwaysClearCacheDisabled(): void
{
$this->projectConfiguration->alwaysClearCache = false;

$this->processHelper
->expects($this->never())
->method('console');

$event = new PostDeploy(new RunConfiguration(), new NullOutput());
$this->subscriber->__invoke($event);
}
}

0 comments on commit f8ca4b0

Please sign in to comment.