From 3f34d1d9d80da1a64e9a6e20a62f1c1478f2a2b5 Mon Sep 17 00:00:00 2001 From: marek Date: Tue, 26 Sep 2023 18:57:14 +0200 Subject: [PATCH] #12 - check-path option addition --- Command/GenerateCronCommand.php | 13 +++++++ Cron/DumpFile.php | 8 ++++ DependencyInjection/Configuration.php | 3 -- .../LexikCronFileGeneratorExtension.php | 3 -- Exception/WrongConsoleBinPathException.php | 7 ++++ Tests/Cron/DumpFileTest.php | 37 +++++++++++++++++++ .../DependencyInjection/BundleExtension.php | 6 --- .../Command/GenerateCronFileCommandTest.php | 15 ++++++++ Tests/Functional/TestCase.php | 6 --- Tests/Functional/app/AppKernel.php | 9 ----- 10 files changed, 80 insertions(+), 27 deletions(-) create mode 100644 Exception/WrongConsoleBinPathException.php diff --git a/Command/GenerateCronCommand.php b/Command/GenerateCronCommand.php index 2a086e9..916063a 100644 --- a/Command/GenerateCronCommand.php +++ b/Command/GenerateCronCommand.php @@ -4,6 +4,7 @@ use Lexik\Bundle\CronFileGeneratorBundle\Cron\DumpFileFactory; use Lexik\Bundle\CronFileGeneratorBundle\Exception\CronEmptyException; +use Lexik\Bundle\CronFileGeneratorBundle\Exception\WrongConsoleBinPathException; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -33,6 +34,7 @@ protected function configure(): void EOPHP ) ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Execute the dump file as a dry run.') + ->addOption('check-path', 'c', InputOption::VALUE_NONE, 'Check absolute path on crontab generation.') ->addArgument('env-mode', InputArgument::REQUIRED, 'Env config') ; } @@ -44,6 +46,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io->title('Generated cron file'); $dryRun = (bool) $input->getOption('dry-run'); + $checkPath = (bool) $input->getOption('check-path'); try { $dumpFile = $this->dumpFileFactory->createWithEnv($input->getArgument('env-mode')); @@ -63,6 +66,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } + if ($checkPath) { + try { + $dumpFile->checkPath(); + } catch (WrongConsoleBinPathException $e) { + $output->writeln(sprintf('%s.', $e->getMessage())); + + return 1; + } + } + $filename = $dumpFile->dumpFile(); $io->success('File generated'); diff --git a/Cron/DumpFile.php b/Cron/DumpFile.php index 5a90264..b0421b3 100644 --- a/Cron/DumpFile.php +++ b/Cron/DumpFile.php @@ -3,6 +3,7 @@ namespace Lexik\Bundle\CronFileGeneratorBundle\Cron; use Lexik\Bundle\CronFileGeneratorBundle\Exception\CronEmptyException; +use Lexik\Bundle\CronFileGeneratorBundle\Exception\WrongConsoleBinPathException; use Symfony\Component\Filesystem\Filesystem; use Twig\Environment; @@ -32,6 +33,13 @@ public function dumpFile(): string return $destination; } + public function checkPath(): void + { + if (false === file_exists($binPath = $this->configuration->getAbsolutePath())) { + throw new WrongConsoleBinPathException(sprintf('Configured path "%s" does not exist', $binPath)); + } + } + public function dryRun(): string { return $this->render(); diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index c602586..3d99403 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -7,9 +7,6 @@ class Configuration implements ConfigurationInterface { - /** - * {@inheritdoc} - */ public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('lexik_cron_file_generator'); diff --git a/DependencyInjection/LexikCronFileGeneratorExtension.php b/DependencyInjection/LexikCronFileGeneratorExtension.php index 643513e..6bc877c 100644 --- a/DependencyInjection/LexikCronFileGeneratorExtension.php +++ b/DependencyInjection/LexikCronFileGeneratorExtension.php @@ -10,9 +10,6 @@ class LexikCronFileGeneratorExtension extends Extension { - /** - * {@inheritdoc} - */ public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); diff --git a/Exception/WrongConsoleBinPathException.php b/Exception/WrongConsoleBinPathException.php new file mode 100644 index 0000000..dfb1095 --- /dev/null +++ b/Exception/WrongConsoleBinPathException.php @@ -0,0 +1,7 @@ +init('staging'); } + + public function testWrongAbsolutePath() + { + self::expectException(WrongConsoleBinPathException::class); + + $templating = $this->createMock(Environment::class); + $templating->expects($this->never())->method('render'); + + $configuration = new Configuration([ + 'mailto' => null, + 'env_available' => [ + 'staging', + ], + 'user' => [ + 'staging' => 'project_staging', + ], + 'php_version' => 'php7.3', + 'absolute_path' => [ + 'staging' => 'path/to/staging', + ], + 'output_path' => $this->outputDir.'/cron_file', + 'crons' => [ + [ + 'name' => 'test', + 'command' => 'app:test', + 'env' => [ + 'staging' => '* * * * *', + ], + ], + ], + ]); + + $dumpFile = new DumpFile($templating, $configuration); + $dumpFile->init('staging'); + $dumpFile->checkPath(); + } } diff --git a/Tests/Functional/Bundle/DependencyInjection/BundleExtension.php b/Tests/Functional/Bundle/DependencyInjection/BundleExtension.php index 2e9c83e..b74af73 100644 --- a/Tests/Functional/Bundle/DependencyInjection/BundleExtension.php +++ b/Tests/Functional/Bundle/DependencyInjection/BundleExtension.php @@ -9,16 +9,10 @@ class BundleExtension extends Extension implements PrependExtensionInterface { - /** - * {@inheritdoc} - */ public function load(array $configs, ContainerBuilder $container): void { } - /** - * {@inheritdoc} - */ public function prepend(ContainerBuilder $container): void { // Annotation must be disabled since this bundle doesn't use Doctrine diff --git a/Tests/Functional/Command/GenerateCronFileCommandTest.php b/Tests/Functional/Command/GenerateCronFileCommandTest.php index 62d9833..6935548 100644 --- a/Tests/Functional/Command/GenerateCronFileCommandTest.php +++ b/Tests/Functional/Command/GenerateCronFileCommandTest.php @@ -56,4 +56,19 @@ public function testDryRun() $tester->getDisplay() ); } + + public function testCheckExecutivePath() + { + $kernel = $this->bootKernel(); + + $tester = new CommandTester( + $kernel->getContainer()->get('lexik_bundle_cron_file_generator.command.generate_cron_command') + ); + + $this->assertSame(1, $tester->execute(['env-mode' => 'staging', '--check-path' => true])); + + $expected = 'Configured path "path/to/staging" does not exist'; + + $this->assertStringContainsString($expected, $tester->getDisplay()); + } } diff --git a/Tests/Functional/TestCase.php b/Tests/Functional/TestCase.php index a755e82..2e1ad1a 100644 --- a/Tests/Functional/TestCase.php +++ b/Tests/Functional/TestCase.php @@ -12,9 +12,6 @@ abstract class TestCase extends WebTestCase protected static $client; - /** - * {@inheritdoc} - */ protected static function createKernel(array $options = []): KernelInterface { require_once __DIR__.'/app/AppKernel.php'; @@ -22,9 +19,6 @@ protected static function createKernel(array $options = []): KernelInterface return new AppKernel('test', true, isset($options['config']) ? $options['config'] : 'base'); } - /** - * {@inheritdoc} - */ protected function setUp(): void { $fs = new Filesystem(); diff --git a/Tests/Functional/app/AppKernel.php b/Tests/Functional/app/AppKernel.php index cf9de83..2f092c8 100644 --- a/Tests/Functional/app/AppKernel.php +++ b/Tests/Functional/app/AppKernel.php @@ -22,9 +22,6 @@ public function __construct(string $environment, bool $debug, string $config = ' $this->config = $config; } - /** - * {@inheritdoc} - */ public function registerBundles(): iterable { return [ @@ -35,17 +32,11 @@ public function registerBundles(): iterable ]; } - /** - * {@inheritdoc} - */ public function getCacheDir(): string { return sys_get_temp_dir().'/LexikCronFileGeneratorBundle/cache'; } - /** - * {@inheritdoc} - */ public function getLogDir(): string { return sys_get_temp_dir().'/LexikCronFileGeneratorBundle/logs';