Skip to content

Commit 5fc7559

Browse files
committed
tests(core): improvements
1 parent 91c4d75 commit 5fc7559

File tree

11 files changed

+83
-9
lines changed

11 files changed

+83
-9
lines changed

.github/workflows/code-style.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
branches:
88
- main
99
schedule:
10-
- cron: "0 0 * * *"
10+
- cron: "5 0 * * *"
1111

1212
jobs:
1313
php-cs-fixer:

.github/workflows/infection.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
branches:
88
- main
99
schedule:
10-
- cron: "0 0 * * *"
10+
- cron: "5 0 * * *"
1111

1212
jobs:
1313
infection:

.github/workflows/phpunit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
branches:
88
- main
99
schedule:
10-
- cron: "0 0 * * *"
10+
- cron: "5 0 * * *"
1111

1212
jobs:
1313
phpunit:

.github/workflows/rector.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
branches:
88
- main
99
schedule:
10-
- cron: "0 0 * * *"
10+
- cron: "5 0 * * *"
1111

1212
jobs:
1313
phpstan:

.github/workflows/security.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
branches:
88
- main
99
schedule:
10-
- cron: "0 0 * * *"
10+
- cron: "5 0 * * *"
1111

1212
jobs:
1313
security:

.github/workflows/static-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
branches:
88
- main
99
schedule:
10-
- cron: "0 0 * * *"
10+
- cron: "5 0 * * *"
1111

1212
jobs:
1313
phpstan:

src/Command/ExportCommand.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,31 @@
55
namespace SchedulerBundle\Command;
66

77
use SchedulerBundle\Export\ExporterRegistryInterface;
8+
use SchedulerBundle\SchedulerInterface;
9+
use SchedulerBundle\Task\TaskInterface;
810
use Symfony\Component\Console\Command\Command;
911
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Input\InputOption;
1013
use Symfony\Component\Console\Output\OutputInterface;
1114
use Symfony\Component\Console\Style\SymfonyStyle;
15+
use Throwable;
1216

1317
/**
1418
* @author Guillaume Loulier <[email protected]>
1519
*/
1620
final class ExportCommand extends Command
1721
{
1822
private ExporterRegistryInterface $exporterRegistry;
23+
private SchedulerInterface $scheduler;
1924

2025
protected static $defaultName = 'scheduler:export';
2126

22-
public function __construct(ExporterRegistryInterface $exporterRegistry)
23-
{
27+
public function __construct(
28+
ExporterRegistryInterface $exporterRegistry,
29+
SchedulerInterface $scheduler
30+
) {
2431
$this->exporterRegistry = $exporterRegistry;
32+
$this->scheduler = $scheduler;
2533

2634
parent::__construct();
2735
}
@@ -33,6 +41,10 @@ protected function configure(): void
3341
{
3442
$this
3543
->setDescription('Export tasks to a specific format')
44+
->setDefinition([
45+
new InputOption('format', null, InputOption::VALUE_OPTIONAL, 'The format used to export tasks', 'crontab'),
46+
new InputOption('filename', null, InputOption::VALUE_OPTIONAL, 'The name of the filename used to export tasks', 'crontab'),
47+
])
3648
;
3749
}
3850

@@ -43,7 +55,30 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4355
{
4456
$style = new SymfonyStyle($input, $output);
4557

46-
// TODO
58+
try {
59+
$tasks = $this->scheduler->getTasks();
60+
if (0 === $tasks->count()) {
61+
$style->warning('No tasks found');
62+
63+
return Command::FAILURE;
64+
}
65+
66+
$filename = $input->getOption('filename');
67+
$exporter = $this->exporterRegistry->find($input->getOption('format'));
68+
69+
$tasks->walk(function (TaskInterface $task) use ($exporter, $filename): void {
70+
$exporter->export($filename, $task);
71+
});
72+
} catch (Throwable $throwable) {
73+
$style->error([
74+
'An error occurred when exporting tasks',
75+
$throwable->getMessage(),
76+
]);
77+
78+
return Command::FAILURE;
79+
}
80+
81+
$style->success('The export has succeed');
4782

4883
return Command::SUCCESS;
4984
}

src/DependencyInjection/SchedulerBundleExtension.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use SchedulerBundle\Command\DebugProbeCommand;
1818
use SchedulerBundle\Command\ExecuteExternalProbeCommand;
1919
use SchedulerBundle\Command\ExecuteTaskCommand;
20+
use SchedulerBundle\Command\ExportCommand;
2021
use SchedulerBundle\Command\ListFailedTasksCommand;
2122
use SchedulerBundle\Command\ListTasksCommand;
2223
use SchedulerBundle\Command\RebootSchedulerCommand;
@@ -606,6 +607,17 @@ private function registerCommands(ContainerBuilder $container): void
606607
])
607608
;
608609

610+
$container->register(ExportCommand::class, ExportCommand::class)
611+
->setArguments([
612+
new Reference(ExporterRegistryInterface::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE),
613+
new Reference(SchedulerInterface::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE),
614+
])
615+
->addTag('console.command')
616+
->addTag('container.preload', [
617+
'class' => ExportCommand::class,
618+
])
619+
;
620+
609621
$container->register(ListFailedTasksCommand::class, ListFailedTasksCommand::class)
610622
->setArguments([
611623
new Reference(WorkerInterface::class, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE),

src/Export/CronTabExporter.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SchedulerBundle\Export;
66

77
use SchedulerBundle\Task\TaskInterface;
8+
use function file_exists;
89

910
/**
1011
* @author Guillaume Loulier <[email protected]>
@@ -13,8 +14,14 @@ final class CronTabExporter implements ExporterInterface
1314
{
1415
public function export(string $filename, TaskInterface $task): void
1516
{
17+
if (file_exists($filename)) {
18+
return;
19+
}
1620
}
1721

22+
/**
23+
* {@inheritdoc}
24+
*/
1825
public function support(string $format): bool
1926
{
2027
return 'crontab' === $format;

src/Export/ExporterInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@ interface ExporterInterface
1313
{
1414
public function export(string $filename, TaskInterface $task): void;
1515

16+
/**
17+
* Determine if the exporter support the current @param string $format.
18+
*/
1619
public function support(string $format): bool;
1720
}

0 commit comments

Comments
 (0)