Skip to content

Commit

Permalink
lexik#12 - check-path option addition (lexik#14)
Browse files Browse the repository at this point in the history
Co-authored-by: marek <[email protected]>
  • Loading branch information
raneomik and marek authored Sep 27, 2023
1 parent 2fafc7a commit 1595e0e
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 27 deletions.
13 changes: 13 additions & 0 deletions 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 @@ -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')
;
}
Expand All @@ -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'));
Expand All @@ -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('<error>%s.</error>', $e->getMessage()));

return 1;
}
}

$filename = $dumpFile->dumpFile();

$io->success('File generated');
Expand Down
8 changes: 8 additions & 0 deletions 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 @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('lexik_cron_file_generator');
Expand Down
3 changes: 0 additions & 3 deletions DependencyInjection/LexikCronFileGeneratorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

class LexikCronFileGeneratorExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
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
{
}
37 changes: 37 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,40 @@ 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',
],
'output_path' => $this->outputDir.'/cron_file',
'crons' => [
[
'name' => 'test',
'command' => 'app:test',
'env' => [
'staging' => '* * * * *',
],
],
],
]);

$dumpFile = new DumpFile($templating, $configuration);
$dumpFile->init('staging');
$dumpFile->checkPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions Tests/Functional/Command/GenerateCronFileCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
6 changes: 0 additions & 6 deletions Tests/Functional/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@ abstract class TestCase extends WebTestCase

protected static $client;

/**
* {@inheritdoc}
*/
protected static function createKernel(array $options = []): KernelInterface
{
require_once __DIR__.'/app/AppKernel.php';

return new AppKernel('test', true, isset($options['config']) ? $options['config'] : 'base');
}

/**
* {@inheritdoc}
*/
protected function setUp(): void
{
$fs = new Filesystem();
Expand Down
9 changes: 0 additions & 9 deletions Tests/Functional/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public function __construct(string $environment, bool $debug, string $config = '
$this->config = $config;
}

/**
* {@inheritdoc}
*/
public function registerBundles(): iterable
{
return [
Expand All @@ -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';
Expand Down

0 comments on commit 1595e0e

Please sign in to comment.