Skip to content

Commit

Permalink
#12 - absolute path check support
Browse files Browse the repository at this point in the history
  • Loading branch information
marek committed Sep 22, 2023
1 parent 2fafc7a commit c5e88c4
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 3 deletions.
7 changes: 6 additions & 1 deletion Command/GenerateCronCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,7 +49,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
try {
$dumpFile = $this->dumpFileFactory->createWithEnv($input->getArgument('env-mode'));
} catch (CronEmptyException $e) {
$output->writeln('<error>There is no cron in your configuration. Crons are required to execute this command.</error>');
$output->writeln(sprintf('<error>%s. Crons are required to execute this command.</error>', $e->getMessage()));

return 1;
} catch (WrongConsoleBinPathException $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));

return 1;
}
Expand Down
6 changes: 6 additions & 0 deletions Cron/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public function getOutpath(): string
return $this->globalConfig['output_path'];
}

public function getCheckAbsolutePath()
{
return $this->globalConfig['check_absolute_path'];
}

private function init(): void
{
$this->globalConfig = [
Expand All @@ -100,6 +105,7 @@ private function init(): void
'env' => $this->env,
'output_path' => $this->cronConfig['output_path'],
'mailto' => $this->cronConfig['mailto'],
'check_absolute_path' => $this->cronConfig['check_absolute_path'][$this->env] ?? false,
];
}

Expand Down
9 changes: 8 additions & 1 deletion Cron/DumpFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -42,8 +43,14 @@ public function init($env): void
$this->configuration->initWithEnv($env);
$this->env = $env;

if ($this->configuration->getCheckAbsolutePath()
&& false === file_exists($binPath = $this->configuration->getAbsolutePath())
) {
throw new WrongConsoleBinPathException(sprintf('The configured "absolute_path: %s" does not exist', $binPath));
}

if (empty($this->configuration->getCrons())) {
throw new CronEmptyException('There is no crons found in the configuration.');
throw new CronEmptyException('There is no cron found in the configuration');
}
}

Expand Down
6 changes: 6 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public function getConfigTreeBuilder(): TreeBuilder
->isRequired()
->end()
->end()
->arrayNode('check_absolute_path')
->requiresAtLeastOneElement()
->prototype('boolean')
->defaultFalse()
->end()
->end()
->scalarNode('output_path')->isRequired()->end()
->arrayNode('crons')
->arrayPrototype()
Expand Down
7 changes: 7 additions & 0 deletions Exception/WrongConsoleBinPathException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Lexik\Bundle\CronFileGeneratorBundle\Exception;

class WrongConsoleBinPathException extends \Exception
{
}
5 changes: 5 additions & 0 deletions Tests/Cron/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function testInit()
$this->assertEquals('staging', $configuration->getEnv());
$this->assertEquals('path/to/cron_file', $configuration->getOutpath());
$this->assertSame('[email protected]', $configuration->getMailto());
$this->assertTrue($configuration->getCheckAbsolutePath());

/** @var Cron $cron */
foreach ($configuration->getCrons() as $cron) {
Expand Down Expand Up @@ -135,6 +136,10 @@ private function getFullConfig()
'staging' => 'path/to/staging',
'prod' => 'path/to/prod',
],
'check_absolute_path' => [
'staging' => true,
'prod' => false,
],
'output_path' => 'path/to/cron_file',
'crons' => [
[
Expand Down
31 changes: 31 additions & 0 deletions Tests/Cron/DumpFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Lexik\Bundle\CronFileGeneratorBundle\Cron\Configuration;
use Lexik\Bundle\CronFileGeneratorBundle\Cron\DumpFile;
use Lexik\Bundle\CronFileGeneratorBundle\Exception\CronEmptyException;
use Lexik\Bundle\CronFileGeneratorBundle\Exception\WrongConsoleBinPathException;
use PHPUnit\Framework\TestCase;
use Twig\Environment;

Expand Down Expand Up @@ -90,4 +91,34 @@ public function testEmptyCron()
$dumpFile = new DumpFile($templating, $configuration);
$dumpFile->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',
],
'check_absolute_path' => [
'staging' => true,
],
'output_path' => $this->outputDir.'/cron_file',
'crons' => [],
]);

$dumpFile = new DumpFile($templating, $configuration);
$dumpFile->init('staging');
}
}
17 changes: 16 additions & 1 deletion Tests/Functional/Command/GenerateCronFileCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,22 @@ public function testGenerateEmptyCrons()

$this->assertSame(1, $tester->execute(['env-mode' => 'staging']));

$expected = 'There is no cron in your configuration. Crons are required to execute this command';
$expected = 'There is no cron found in the configuration. Crons are required to execute this command';

$this->assertStringContainsString($expected, $tester->getDisplay());
}

public function testGenerateWithAbsolutePathCheck()
{
$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' => 'prod']));

$expected = 'The configured "absolute_path: path/to/prod" does not exist';

$this->assertStringContainsString($expected, $tester->getDisplay());
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/Functional/app/config/base_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ lexik_cron_file_generator:
absolute_path:
staging: path/to/staging
prod: path/to/prod
check_absolute_path:
prod: true
output_path: '%kernel.cache_dir%/cron_test'
crons:
- { name: 'send email', command: 'app:test', env: { staging: '* * * * *', prod: '* 5 * * *' } }
Expand Down

0 comments on commit c5e88c4

Please sign in to comment.