Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ClearCacheCommand::class for clear the cache #380

Merged
merged 1 commit into from
Sep 14, 2024
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
96 changes: 96 additions & 0 deletions src/System/Integrate/Console/ClearCacheCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace System\Integrate\Console;

use System\Console\Command;
use System\Console\Traits\CommandTrait;
use System\Integrate\Application;
use System\Template\Property;

use function System\Console\fail;
use function System\Console\info;
use function System\Console\ok;

/**
* @property bool $update
* @property bool $force
*/
class ClearCacheCommand extends Command
{
use CommandTrait;

/**
* Register command.
*
* @var array<int, array<string, mixed>>
*/
public static array $command = [
[
'pattern' => 'cache:clear',
'fn' => [ClearCacheCommand::class, 'clear'],
],
];

/**
* @return array<string, array<string, string|string[]>>
*/
public function printHelp()
{
return [
'commands' => [
'cache:clear' => 'Clear cache (default drive)',
],
'options' => [
'--all' => 'Clear all registered cache driver.',
'--drivers' => 'Clear spesific driver name.',
],
'relation' => [
'cache:clear' => ['--all', '--drivers'],
],
];
}

public function clear(Application $app): int
{
if (false === $app->has('cache')) {
fail('Cache is not set yet.')->out();

return 1;
}

/** @var \System\Cache\CacheManager|null */
$cache = $app['cache'];

/** @var string[]|null */
$drivers = null;

/** @var string[]|string|bool */
$user_drivers = $this->option('drivers', false);

if ($this->option('all', false) && false === $user_drivers) {
$drivers = array_keys(
(fn (): array => $this->{'driver'})->call($cache) // @phpstan-ignore-line
);
}

if ($user_drivers) {
$drivers = is_array($user_drivers) ? $user_drivers : [$user_drivers];
}

if (null === $drivers) {
$cache->driver()->clear();
ok('Done default cache driver has been clear.')->out(false);

return 0;
}

foreach ($drivers as $driver) {
$cache->driver($driver)->clear();
info("clear '{$driver}' driver.")->out(false);
}

return 0;
}
}
86 changes: 86 additions & 0 deletions tests/Integrate/Commands/ClearCacheCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace System\Integrate\Commands;

use PHPUnit\Framework\TestCase;
use System\Cache\CacheManager;
use System\Cache\Storage\ArrayStorage;
use System\Integrate\Application;
use System\Integrate\Console\ClearCacheCommand;

class ClearCacheCommandTest extends TestCase
{
private ?Application $app = null;

protected function setUp(): void
{
$this->app = new Application(__DIR__);
}

protected function teardown(): void
{
$this->app->flush();
$this->app = null;
}

/** @test */
public function itCantRunCommand(): void
{
$command = new ClearCacheCommand(['cli', 'clear:cache']);

ob_start();
$code = $command->clear($this->app);
$out =ob_get_clean();

$this->assertEquals(1, $code);
$this->assertStringContainsString('Cache is not set yet.', $out);
}

/** @test */
public function itCanClearDefaultDriver(): void
{
$this->app->set('cache', fn () => new CacheManager());
$command = new ClearCacheCommand(['cli', 'clear:cache']);

ob_start();
$code = $command->clear($this->app);
$out =ob_get_clean();

$this->assertEquals(0, $code);
$this->assertStringContainsString('Done default cache driver has been clear.', $out);
}

/** @test */
public function itCanAllDriver(): void
{
$cache_manager = new CacheManager();
$cache_manager->setDriver('array', new ArrayStorage());
$this->app->set('cache', fn () => $cache_manager);
$command = new ClearCacheCommand(['cli', 'clear:cache', '--all'], ['all' => true]);

ob_start();
$code = $command->clear($this->app);
$out =ob_get_clean();

$this->assertEquals(0, $code);
$this->assertStringContainsString("clear 'array' driver.", $out);
}

/** @test */
public function itCanSpesifikDriver(): void
{
$cache_manager = new CacheManager();
$cache_manager->setDriver('array', new ArrayStorage());
$this->app->set('cache', fn () => $cache_manager);
$command = new ClearCacheCommand(['cli', 'clear:cache', '--drivers array'], ['drivers' => 'array']);

ob_start();
$code = $command->clear($this->app);
$out =ob_get_clean();

$this->assertEquals(0, $code);
$this->assertStringContainsString("clear 'array' driver.", $out);
}
}
Loading