Skip to content

Commit

Permalink
Add cache pool list command
Browse files Browse the repository at this point in the history
  • Loading branch information
kenariosz committed Apr 5, 2024
1 parent fc8c185 commit 9115b35
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 3 deletions.
49 changes: 49 additions & 0 deletions source/Internal/Framework/Cache/Command/CachePoolListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\EshopCommunity\Internal\Framework\Cache\Command;

use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\CachePoolName;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

final class CachePoolListCommand extends Command
{
protected static $defaultName = 'oe:cache:pool:list';

public function __construct(private readonly ContextInterface $context)
{
parent::__construct();
}

protected function configure(): void
{
$this
->setHelp(
<<<'EOF'
The <info>%command.name%</info> command lists all available dynamic cache pools.
EOF
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$io->table(
['Pool name'],
array_map(static fn($pool) => [$pool], CachePoolName::getAll($this->context->getAllShopIds()))
);

return 0;
}
}
7 changes: 6 additions & 1 deletion source/Internal/Framework/Cache/Command/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ services:
oxid_esales.command.clearcache_command:
class: OxidEsales\EshopCommunity\Internal\Framework\Cache\Command\ClearCacheCommand
tags:
- { name: 'console.command', command: 'oe:cache:clear' }
- { name: 'console.command', command: 'oe:cache:clear' }

oxid_esales.command.cache_pool_list_command:
class: OxidEsales\EshopCommunity\Internal\Framework\Cache\Command\CachePoolListCommand
tags:
- { name: 'console.command', command: 'oe:cache:pool:list' }
21 changes: 21 additions & 0 deletions source/Internal/Framework/Cache/Pool/CachePoolName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool;

class CachePoolName
{
private static string $namePrefix = 'oxid_pool_';

public static function get(int $shopId): string
{
return static::$namePrefix . $shopId;
}

public static function getAll(array $shopIds): array
{
return array_map(
static fn($id) => static::$namePrefix . $id,
$shopIds
);
}
}
3 changes: 2 additions & 1 deletion source/Internal/Framework/DIContainer/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OxidEsales\EshopCommunity\Internal\Framework\DIContainer;

use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\CachePoolName;
use OxidEsales\EshopCommunity\Internal\Framework\DIContainer\Dao\ProjectYamlDao;
use OxidEsales\EshopCommunity\Internal\Framework\DIContainer\Service\ProjectYamlImportService;
use OxidEsales\EshopCommunity\Internal\Framework\Logger\LoggerServiceFactory;
Expand Down Expand Up @@ -39,7 +40,7 @@ public function getContainer(): SymfonyContainerBuilder
$symfonyContainer->addCompilerPass(new RegisterListenersPass());
$symfonyContainer->addCompilerPass(new AddConsoleCommandPass());
$symfonyContainer->setParameter('oxid_cache_directory', $this->context->getCacheDirectory());
$symfonyContainer->setParameter('oxid_current_cache_pool', 'shop_' . $this->context->getCurrentShopId());
$symfonyContainer->setParameter('oxid_current_cache_pool', CachePoolName::get($this->context->getCurrentShopId()));

$this->loadEditionServices($symfonyContainer);
$this->loadModuleServices($symfonyContainer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Container;

use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\CachePoolName;
use OxidEsales\EshopCommunity\Internal\Framework\DIContainer\ContainerBuilder;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
use OxidEsales\EshopCommunity\Tests\Unit\Internal\ContextStub;
Expand Down Expand Up @@ -111,7 +112,7 @@ public function testCachePoolParameterIsSet(): void
$container = $this->makeContainer($context);

$this->assertSame(
'shop_' . $context->getCurrentShopId(),
CachePoolName::get($context->getCurrentShopId()),
$container->getParameter('oxid_current_cache_pool')
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Framework\Cache\Command;

use OxidEsales\EshopCommunity\Internal\Framework\Cache\Command\CachePoolListCommand;
use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\CachePoolName;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

#[Group('cache')]
class CachePoolListCommandTest extends TestCase
{
public function testClearCacheTriggersRegularAndTemplatesCleaners(): void
{
$context = $this->createStub(ContextInterface::class);
$context->method('getAllShopIds')->willReturn([1, 3, 4, 2,]);

$application = new Application();
$application->add(new CachePoolListCommand($context));

$command = $application->find('oe:cache:pool:list');
$commandTester = new CommandTester($command);
$commandTester->execute([]);

$commandTester->assertCommandIsSuccessful();

$output = $commandTester->getDisplay();
$this->assertStringContainsString(CachePoolName::get(3), $output);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace OxidEsales\EshopCommunity\Tests\Integration\Internal\Framework\Cache\Pool;

use OxidEsales\EshopCommunity\Internal\Framework\Cache\Pool\CachePoolName;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

#[Group('cache')]
class CachePoolNameTest extends TestCase
{
public function testGetPoolName(): void
{
$shopId = 1;
$this->assertSame(
'oxid_pool_' . $shopId,
CachePoolName::get($shopId)
);
}

public function testGetAllPoolName(): void
{
$this->assertSame(
['oxid_pool_1', 'oxid_pool_2', 'oxid_pool_3', 'oxid_pool_4', 'oxid_pool_5'],
CachePoolName::getAll([1, 2, 3, 4, 5,])
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
use OxidEsales\EshopCommunity\Internal\Framework\Module\Cache\CacheNotFoundException;
use OxidEsales\EshopCommunity\Internal\Framework\Module\Cache\ModuleCacheServiceInterface;
use OxidEsales\EshopCommunity\Tests\ContainerTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

#[Group('cache')]
final class ModuleCacheTest extends TestCase
{
use ContainerTrait;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
use OxidEsales\EshopCommunity\Internal\Framework\Templating\Cache\TemplateCacheService;
use OxidEsales\EshopCommunity\Internal\Transition\Adapter\ShopAdapterInterface;
use OxidEsales\EshopCommunity\Internal\Transition\Utility\ContextInterface;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[Group('cache')]
class ClearCacheCommandTest extends TestCase
{
public function testClearCacheTriggersRegularAndTemplatesCleaners(): void
Expand Down

0 comments on commit 9115b35

Please sign in to comment.