From a54fe9b91d37be8ef63d9121e9d909dceed9fd6c Mon Sep 17 00:00:00 2001 From: ashraf Date: Wed, 28 Feb 2024 10:25:34 +0100 Subject: [PATCH] OXDEV-7768 Remove Template cache service --- CHANGELOG-8.0.md | 1 + .../Cache/Command/ClearCacheCommand.php | 14 ++-- .../Cache/ShopTemplateCacheService.php | 4 +- .../Templating/Cache/TemplateCacheService.php | 33 -------- .../Cache/TemplateCacheServiceInterface.php | 16 ---- .../Framework/Templating/services.yaml | 6 -- .../Theme/Command/ThemeActivateCommand.php | 6 +- .../Transition/Utility/BasicContext.php | 8 -- .../Utility/BasicContextInterface.php | 5 -- .../Cache/ShopTemplateCacheServiceTest.php | 53 +++--------- .../Cache/TemplateCacheServiceTest.php | 84 ------------------- tests/Unit/Internal/BasicContextStub.php | 7 -- .../Cache/Command/ClearCacheCommandTest.php | 8 +- .../Cache/ShopTemplateCacheServiceTest.php | 26 +++++- 14 files changed, 52 insertions(+), 219 deletions(-) delete mode 100644 source/Internal/Framework/Templating/Cache/TemplateCacheService.php delete mode 100644 source/Internal/Framework/Templating/Cache/TemplateCacheServiceInterface.php delete mode 100644 tests/Integration/Internal/Framework/Templating/Cache/TemplateCacheServiceTest.php diff --git a/CHANGELOG-8.0.md b/CHANGELOG-8.0.md index dd13de40daa..0fb5a47521e 100644 --- a/CHANGELOG-8.0.md +++ b/CHANGELOG-8.0.md @@ -15,3 +15,4 @@ - Remove deprecated global function \makeReadable() - Redundant `TemplateFileResolverInterface` functionality - Smarty templates support +- Remove deprecated `TemplateCacheService` implementation diff --git a/source/Internal/Framework/Cache/Command/ClearCacheCommand.php b/source/Internal/Framework/Cache/Command/ClearCacheCommand.php index f694169505f..cbaaf40a13f 100644 --- a/source/Internal/Framework/Cache/Command/ClearCacheCommand.php +++ b/source/Internal/Framework/Cache/Command/ClearCacheCommand.php @@ -6,7 +6,7 @@ use OxidEsales\EshopCommunity\Internal\Framework\DIContainer\Service\ContainerCacheInterface; use OxidEsales\EshopCommunity\Internal\Framework\Module\Cache\ModuleCacheServiceInterface; -use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\TemplateCacheServiceInterface; +use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface; use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface; use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface; use Symfony\Component\Console\Command\Command; @@ -16,11 +16,11 @@ class ClearCacheCommand extends Command { public function __construct( - private ShopAdapterInterface $shopAdapter, - private TemplateCacheServiceInterface $templateCacheService, - private ContainerCacheInterface $containerCache, - private ModuleCacheServiceInterface $moduleCacheService, - private ContextInterface $context + private readonly ShopAdapterInterface $shopAdapter, + private readonly ShopTemplateCacheServiceInterface $shopTemplateCacheService, + private readonly ContainerCacheInterface $containerCache, + private readonly ModuleCacheServiceInterface $moduleCacheService, + private readonly ContextInterface $context ) { parent::__construct(); } @@ -32,7 +32,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { - $this->templateCacheService->invalidateTemplateCache(); + $this->shopTemplateCacheService->invalidateAllShopsCache(); $this->shopAdapter->invalidateModulesCache(); foreach ($this->context->getAllShopIds() as $shopId) { diff --git a/source/Internal/Framework/Templating/Cache/ShopTemplateCacheService.php b/source/Internal/Framework/Templating/Cache/ShopTemplateCacheService.php index a4840dcff56..f280814b627 100644 --- a/source/Internal/Framework/Templating/Cache/ShopTemplateCacheService.php +++ b/source/Internal/Framework/Templating/Cache/ShopTemplateCacheService.php @@ -16,8 +16,8 @@ class ShopTemplateCacheService implements ShopTemplateCacheServiceInterface { public function __construct( - private ContextInterface $context, - private Filesystem $filesystem + private readonly ContextInterface $context, + private readonly Filesystem $filesystem ) { } diff --git a/source/Internal/Framework/Templating/Cache/TemplateCacheService.php b/source/Internal/Framework/Templating/Cache/TemplateCacheService.php deleted file mode 100644 index 3e12554c1ce..00000000000 --- a/source/Internal/Framework/Templating/Cache/TemplateCacheService.php +++ /dev/null @@ -1,33 +0,0 @@ -context->getAllShopIds(); - - foreach ($shops as $shop) { - $this->shopTemplateCacheService->invalidateCache($shop); - } - } -} diff --git a/source/Internal/Framework/Templating/Cache/TemplateCacheServiceInterface.php b/source/Internal/Framework/Templating/Cache/TemplateCacheServiceInterface.php deleted file mode 100644 index 208939dc381..00000000000 --- a/source/Internal/Framework/Templating/Cache/TemplateCacheServiceInterface.php +++ /dev/null @@ -1,16 +0,0 @@ -shopAdapter->activateTheme($themeId); $this->moduleCacheService->invalidateAll(); - $this->templateCacheService->invalidateTemplateCache(); + $this->shopTemplateCacheService->invalidateAllShopsCache(); $output->writeLn('' . sprintf(self::MESSAGE_THEME_ACTIVATED, $themeId) . ''); return Command::SUCCESS; diff --git a/source/Internal/Transition/Utility/BasicContext.php b/source/Internal/Transition/Utility/BasicContext.php index 79e0bcbf310..dae5d1e2a0a 100644 --- a/source/Internal/Transition/Utility/BasicContext.php +++ b/source/Internal/Transition/Utility/BasicContext.php @@ -240,12 +240,4 @@ public function getFacts(): Facts } return $this->facts; } - - public function getTemplateCacheDirectory(): string - { - return Path::join( - $this->getCacheDirectory(), - 'template_cache' - ); - } } diff --git a/source/Internal/Transition/Utility/BasicContextInterface.php b/source/Internal/Transition/Utility/BasicContextInterface.php index dce249995cf..831326ae5b6 100644 --- a/source/Internal/Transition/Utility/BasicContextInterface.php +++ b/source/Internal/Transition/Utility/BasicContextInterface.php @@ -137,9 +137,4 @@ public function getFacts(): Facts; public function getCacheDirectory(): string; public function getModuleCacheDirectory(): string; - - /** - * @deprecated Use OxidEsales\Eshop\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface instead - */ - public function getTemplateCacheDirectory(): string; } diff --git a/tests/Integration/Internal/Framework/Templating/Cache/ShopTemplateCacheServiceTest.php b/tests/Integration/Internal/Framework/Templating/Cache/ShopTemplateCacheServiceTest.php index 8e9659d3ef7..2ddc9c34465 100644 --- a/tests/Integration/Internal/Framework/Templating/Cache/ShopTemplateCacheServiceTest.php +++ b/tests/Integration/Internal/Framework/Templating/Cache/ShopTemplateCacheServiceTest.php @@ -21,7 +21,6 @@ final class ShopTemplateCacheServiceTest extends TestCase use ContainerTrait; private int $shopId; - private array $shopsIds; private string $shopTemplateCachePath; private Filesystem $filesystem; private ShopTemplateCacheServiceInterface $shopTemplateCacheService; @@ -30,7 +29,6 @@ protected function setUp(): void { $this->filesystem = new Filesystem(); $this->shopId = $this->get(ContextInterface::class)->getCurrentShopId(); - $this->shopsIds = $this->get(ContextInterface::class)->getAllShopIds(); $this->shopTemplateCachePath = $this->get(ShopTemplateCacheServiceInterface::class) ->getCacheDirectory($this->shopId); @@ -49,55 +47,30 @@ public function testInvalidateCache(): void $this->assertEquals(0, $this->countShopCacheFiles($this->shopId)); } - public function testInvalidateAllShopsCache(): void - { - $this->assertNotEquals(0, $this->countCacheFiles()); - - $this->get(ShopTemplateCacheServiceInterface::class)->invalidateAllShopsCache(); - - $this->assertEquals(0, $this->countCacheFiles()); - } - private function clearTemplateCache(): void { - foreach ($this->shopsIds as $shopId) { - $this->filesystem->remove( - $this->get(ShopTemplateCacheServiceInterface::class) - ->getCacheDirectory($shopId) - ); - } + $this->filesystem->remove( + $this->get(ShopTemplateCacheServiceInterface::class)->getCacheDirectory($this->shopId) + ); } private function countShopCacheFiles($shopId): int { - return count(\glob($this->get(ShopTemplateCacheServiceInterface::class) - ->getCacheDirectory($shopId))); - } - - private function countCacheFiles(): int - { - $files = 0; - foreach ($this->shopsIds as $shopId) { - $files += $this->countShopCacheFiles($shopId); - } - return $files; + return count(\glob($this->get(ShopTemplateCacheServiceInterface::class)->getCacheDirectory($shopId))); } private function populateTemplateCache(): void { $numberOfTestFiles = 3; - foreach ($this->shopsIds as $shopId) { - $templateCachePath = $this->get(ShopTemplateCacheServiceInterface::class) - ->getCacheDirectory($shopId); - $this->filesystem->mkdir($templateCachePath); - for ($i = 0; $i < $numberOfTestFiles; $i++) { - $this->filesystem->touch( - Path::join( - $templateCachePath, - uniqid('template-file-' . $shopId, true) - ) - ); - } + $templateCachePath = $this->get(ShopTemplateCacheServiceInterface::class)->getCacheDirectory($this->shopId); + $this->filesystem->mkdir($templateCachePath); + for ($i = 0; $i < $numberOfTestFiles; $i++) { + $this->filesystem->touch( + Path::join( + $templateCachePath, + uniqid('template-file-' . $this->shopId, true) + ) + ); } } } diff --git a/tests/Integration/Internal/Framework/Templating/Cache/TemplateCacheServiceTest.php b/tests/Integration/Internal/Framework/Templating/Cache/TemplateCacheServiceTest.php deleted file mode 100644 index 9bf405cda5f..00000000000 --- a/tests/Integration/Internal/Framework/Templating/Cache/TemplateCacheServiceTest.php +++ /dev/null @@ -1,84 +0,0 @@ -filesystem = new Filesystem(); - $this->shopsIds = $this->get(ContextInterface::class)->getAllShopIds(); - - $this->clearTemplateCache(); - $this->populateTemplateCache(); - - parent::setUp(); - } - - public function testInvalidateTemplateCache(): void - { - $this->assertNotEquals(0, $this->countCacheFiles()); - - $this->get(TemplateCacheServiceInterface::class)->invalidateTemplateCache(); - - self::assertEquals(0, $this->countCacheFiles()); - } - - private function clearTemplateCache(): void - { - foreach ($this->shopsIds as $shopId) { - $this->filesystem->remove( - $this->get(ShopTemplateCacheServiceInterface::class) - ->getCacheDirectory($shopId) - ); - } - } - - private function countCacheFiles(): int - { - $files = 0; - foreach ($this->shopsIds as $shopId) { - $files += count(\glob($this->get(ShopTemplateCacheServiceInterface::class) - ->getCacheDirectory($shopId))); - } - return $files; - } - - private function populateTemplateCache(): void - { - $numberOfTestFiles = 3; - foreach ($this->shopsIds as $shopId) { - $templateCachePath = $this->get(ShopTemplateCacheServiceInterface::class) - ->getCacheDirectory($shopId); - $this->filesystem->mkdir($templateCachePath); - for ($i = 0; $i < $numberOfTestFiles; $i++) { - $this->filesystem->touch( - Path::join( - $templateCachePath, - uniqid('template-file-' . $shopId, true) - ) - ); - } - } - } -} diff --git a/tests/Unit/Internal/BasicContextStub.php b/tests/Unit/Internal/BasicContextStub.php index 9e3db9b641d..a5fcaf54ccd 100644 --- a/tests/Unit/Internal/BasicContextStub.php +++ b/tests/Unit/Internal/BasicContextStub.php @@ -38,7 +38,6 @@ class BasicContextStub implements BasicContextInterface private string $composerVendorName; private string $cacheDirectory; private string $moduleCacheDirectory; - private string $templateCacheDirectory; private int $currentShopId; private string $activeModuleServicesFilePath; @@ -64,7 +63,6 @@ public function __construct() $this->composerVendorName = $basicContext->getComposerVendorName(); $this->cacheDirectory = $basicContext->getCacheDirectory(); $this->moduleCacheDirectory = $basicContext->getModuleCacheDirectory(); - $this->templateCacheDirectory = $basicContext->getTemplateCacheDirectory(); $this->currentShopId = $basicContext->getCurrentShopId(); $this->activeModuleServicesFilePath = $basicContext->getActiveModuleServicesFilePath($this->getCurrentShopId()); $this->shopConfigurableServicesFilePath = $basicContext->getShopConfigurableServicesFilePath( @@ -237,11 +235,6 @@ public function getShopConfigurationDirectory(int $shopId): string return Path::join($this->getProjectConfigurationDirectory(), 'shops', (string)$shopId); } - public function getTemplateCacheDirectory(): string - { - return $this->templateCacheDirectory; - } - public function setTemplateCacheDirectory(string $templateCacheDirectory): void { $this->templateCacheDirectory = $templateCacheDirectory; diff --git a/tests/Unit/Internal/Framework/Cache/Command/ClearCacheCommandTest.php b/tests/Unit/Internal/Framework/Cache/Command/ClearCacheCommandTest.php index da7417f1fab..adc77f14305 100644 --- a/tests/Unit/Internal/Framework/Cache/Command/ClearCacheCommandTest.php +++ b/tests/Unit/Internal/Framework/Cache/Command/ClearCacheCommandTest.php @@ -12,7 +12,7 @@ use OxidEsales\EshopCommunity\Internal\Framework\Cache\Command\ClearCacheCommand; use OxidEsales\EshopCommunity\Internal\Framework\DIContainer\Service\ContainerCacheInterface; use OxidEsales\EshopCommunity\Internal\Framework\Module\Cache\ModuleCacheServiceInterface; -use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\TemplateCacheService; +use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\ShopTemplateCacheServiceInterface; use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface; use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface; use PHPUnit\Framework\TestCase; @@ -26,8 +26,8 @@ public function testClearCacheTriggersRegularAndTemplatesCleaners(): void $shopAdapterMock = $this->createMock(ShopAdapterInterface::class); $shopAdapterMock->expects($this->once())->method('invalidateModulesCache'); - $templateCacheServiceMock = $this->createMock(TemplateCacheService::class); - $templateCacheServiceMock->expects($this->once())->method('invalidateTemplateCache'); + $shopTemplateCacheServiceMock = $this->createMock(ShopTemplateCacheServiceInterface::class); + $shopTemplateCacheServiceMock->expects($this->once())->method('invalidateAllShopsCache'); $containerCacheMock = $this->createMock(ContainerCacheInterface::class); $containerCacheMock->expects($this->once())->method('invalidate'); @@ -40,7 +40,7 @@ public function testClearCacheTriggersRegularAndTemplatesCleaners(): void $command = new ClearCacheCommand( $shopAdapterMock, - $templateCacheServiceMock, + $shopTemplateCacheServiceMock, $containerCacheMock, $moduleCacheServiceMock, $contextMock diff --git a/tests/Unit/Internal/Framework/Templating/Cache/ShopTemplateCacheServiceTest.php b/tests/Unit/Internal/Framework/Templating/Cache/ShopTemplateCacheServiceTest.php index 9722bc4905a..828503083fa 100644 --- a/tests/Unit/Internal/Framework/Templating/Cache/ShopTemplateCacheServiceTest.php +++ b/tests/Unit/Internal/Framework/Templating/Cache/ShopTemplateCacheServiceTest.php @@ -29,18 +29,36 @@ protected function setUp(): void $this->contextMock = $this->getMockBuilder(ContextInterface::class)->getMock(); $this->fileSystemMock = $this->getMockBuilder(Filesystem::class)->getMock(); - $this->contextMock->expects($this->any()) - ->method('getCacheDirectory') - ->willReturn($this->cacheDirectory); - $this->shopTemplateCacheService = new ShopTemplateCacheService($this->contextMock, $this->fileSystemMock); } public function testGetCacheDirectory() { $shopId = 123; + $this->contextMock->expects($this->once()) + ->method('getCacheDirectory') + ->willReturn($this->cacheDirectory); + $shopCachePath = $this->shopTemplateCacheService->getCacheDirectory($shopId); $this->assertEquals($this->cacheDirectory . '/template_cache/shops/' . $shopId, $shopCachePath); } + public function testInvalidateAllShopsCache() + { + $shops = [1,2,3]; + $this->contextMock->expects($this->once()) + ->method('getAllShopIds') + ->willReturn($shops); + + $this->fileSystemMock + ->expects($this->exactly(3)) + ->method('exists') + ->willReturn(true, false, true); + + $this->fileSystemMock + ->expects($this->exactly(2)) + ->method('remove'); + + $this->shopTemplateCacheService->invalidateAllShopsCache(); + } }