-
-
Notifications
You must be signed in to change notification settings - Fork 53
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
feature: cache flush safety & cache flush command #118
base: main
Are you sure you want to change the base?
Changes from all commits
6d0287f
e2cf29d
402c728
c8291bf
550487b
a35337f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Peck\Console\Commands\Cache; | ||
|
||
use Exception; | ||
use Peck\Plugins\Cache; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[AsCommand(name: 'cache:clear', description: 'Clears cached data.')] | ||
final class ClearCommand extends Command | ||
{ | ||
/** | ||
* Configures the command. | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this->addOption('directory', 'd', InputOption::VALUE_OPTIONAL, 'Cache directory'); | ||
$this->addOption('prefix', 'p', InputOption::VALUE_OPTIONAL, 'Cache file prefix'); | ||
} | ||
|
||
/** | ||
* Executes the command. | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$output->writeln('<info>Clearing cache...</info>'); | ||
|
||
$directory = $input->getOption('directory'); | ||
$prefix = $input->getOption('prefix'); | ||
|
||
$prefix = match (is_string($prefix)) { | ||
true => trim($prefix), | ||
default => Cache::CACHE_PREFIX, | ||
}; | ||
|
||
if (is_string($directory) && ! is_dir($directory)) { | ||
$output->writeln('<error>The specified cache directory does not exist.</error>'); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
match (is_string($directory)) { | ||
true => Cache::create($directory, $prefix)->flush(), | ||
default => Cache::default()->flush(), | ||
}; | ||
|
||
$output->writeln('<info>Cache successfully cleared!</info>'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Peck\Console\Commands\Cache\ClearCommand; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
it('clears default cache directory', function (): void { | ||
$application = new Application; | ||
|
||
$application->add(new ClearCommand); | ||
|
||
$command = $application->find('cache:clear'); | ||
|
||
$commandTester = new CommandTester($command); | ||
|
||
$commandTester->execute([]); | ||
|
||
$output = $commandTester->getDisplay(); | ||
|
||
expect(trim($output))->toContain('Clearing cache...'); | ||
expect(trim($output))->toContain('Cache successfully cleared!'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should also check if the files where deleted? Or do we leave that to the unit test of the Cache class? |
||
}); | ||
|
||
it('clears custom cache directory', function (): void { | ||
|
||
$application = new Application; | ||
|
||
$application->add(new ClearCommand); | ||
|
||
$command = $application->find('cache:clear'); | ||
|
||
$commandTester = new CommandTester($command); | ||
|
||
if (! is_dir('/tmp/.peck.cache')) { | ||
@mkdir('/tmp/.peck.cache'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't you check if this directory was created? If it was not created there is a permission problem. I am unsure how the test behaves in that scenario, when we ignore the errors. |
||
} | ||
|
||
$commandTester->execute([ | ||
'--directory' => '/tmp/.peck.cache', | ||
]); | ||
|
||
$output = $commandTester->getDisplay(); | ||
|
||
expect(trim($output))->toContain('Clearing cache...'); | ||
expect(trim($output))->toContain('Cache successfully cleared!'); | ||
|
||
@unlink('/tmp/.peck.cache'); | ||
}); | ||
|
||
it('throws an exception when the specified directory does not exist', function (): void { | ||
$application = new Application; | ||
|
||
$application->add(new ClearCommand); | ||
|
||
$command = $application->find('cache:clear'); | ||
|
||
$commandTester = new CommandTester($command); | ||
|
||
$commandTester->execute([ | ||
'--directory' => '/tmp/peck.cache', | ||
]); | ||
|
||
$output = $commandTester->getDisplay(); | ||
|
||
expect(trim($output))->toContain('The specified cache directory does not exist.'); | ||
}); | ||
|
||
it('only deletes cached files from custom cache directory', function (): void { | ||
$application = new Application; | ||
|
||
$application->add(new ClearCommand); | ||
|
||
$command = $application->find('cache:clear'); | ||
|
||
$commandTester = new CommandTester($command); | ||
|
||
if (! is_dir('/tmp/peck_custom')) { | ||
@mkdir('/tmp/peck_custom'); | ||
} | ||
|
||
file_put_contents('/tmp/peck_custom/peck_1', 'test'); | ||
file_put_contents('/tmp/peck_custom/peck_2', 'test'); | ||
file_put_contents('/tmp/peck_custom/not_a_cached_file', 'test'); | ||
|
||
$commandTester->execute([ | ||
'--directory' => '/tmp/peck_custom', | ||
]); | ||
|
||
expect(count(glob('/tmp/peck_custom/*')))->toBe(1); | ||
|
||
array_map('unlink', array_filter((array) glob('/tmp/peck_custom/*'))); | ||
}); | ||
|
||
it('deletes all files from cache directory based on custom prefix', function (): void { | ||
$application = new Application; | ||
|
||
$application->add(new ClearCommand); | ||
|
||
$command = $application->find('cache:clear'); | ||
|
||
$commandTester = new CommandTester($command); | ||
|
||
if (! is_dir('/tmp/peck_custom')) { | ||
@mkdir('/tmp/peck_custom'); | ||
} | ||
|
||
file_put_contents('/tmp/peck_custom/peck_1', 'test'); | ||
file_put_contents('/tmp/peck_custom/peck_2', 'test'); | ||
file_put_contents('/tmp/peck_custom/pecker_1', 'test'); | ||
file_put_contents('/tmp/peck_custom/pecker_2', 'test'); | ||
|
||
$commandTester->execute([ | ||
'--directory' => '/tmp/peck_custom', | ||
'--prefix' => 'pecker_', | ||
]); | ||
|
||
expect(count(glob('/tmp/peck_custom/*')))->toBe(2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would also pass if the --prefix was ignored and the default was used (as the number of files for both prefixes are the same). If possible also assert the file names. |
||
|
||
array_map('unlink', array_filter((array) glob('/tmp/peck_custom/*'))); | ||
}); | ||
|
||
it('deletes all cache if prefix is empty', function (): void { | ||
$application = new Application; | ||
|
||
$application->add(new ClearCommand); | ||
|
||
$command = $application->find('cache:clear'); | ||
|
||
$commandTester = new CommandTester($command); | ||
|
||
if (! is_dir('/tmp/peck_custom')) { | ||
@mkdir('/tmp/peck_custom'); | ||
} | ||
|
||
file_put_contents('/tmp/peck_custom/peck_1', 'test'); | ||
file_put_contents('/tmp/peck_custom/peck_2', 'test'); | ||
file_put_contents('/tmp/peck_custom/pecker_1', 'test'); | ||
file_put_contents('/tmp/peck_custom/pecker_2', 'test'); | ||
|
||
$commandTester->execute([ | ||
'--directory' => '/tmp/peck_custom', | ||
'--prefix' => '', | ||
]); | ||
|
||
expect(glob('/tmp/peck_custom/*'))->toBeEmpty(); | ||
|
||
array_map('unlink', array_filter((array) glob('/tmp/peck_custom/*'))); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit unsure about this, as this "interacts with"/changes the files outside the tests directory.
But I guess it is the only way to test this.