Skip to content

Commit

Permalink
Some small modifications to output and the allowed context keys
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Aug 16, 2024
1 parent bb25fa1 commit 40aa4e2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/CronBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ public function setContext(array $context): self

public function addContext(string $key, mixed $value): self
{
if (!preg_match('/^[a-zA-Z_]\w*$/', $key)) {
throw new \InvalidArgumentException(sprintf('Invalid context key format: %s', $key));
}

if (!is_scalar($value)) {
throw new \InvalidArgumentException(sprintf('The value must be a scalar, got %s for key %s', gettype($value), $key));
}
Expand Down Expand Up @@ -133,11 +129,13 @@ public function build(): string
}
}

return sprintf("%s\n%s\n%s", $this->getParsedDelimiter('begin'), rtrim($crontab), $this->getParsedDelimiter('end'));
return sprintf("%s\n%s\n%s", $this->getParsedDelimiter('begin'), rtrim($crontab), $this->getParsedDelimiter('end')) . "\n";
}

public static function merge(string $existingCron, self $cronBuilder): string
{
$existingCron = trim($existingCron);

if ('' === $existingCron) {
return $cronBuilder->build();
}
Expand All @@ -161,7 +159,7 @@ public static function merge(string $existingCron, self $cronBuilder): string
}

if (1 === $replacements) {
return $replacedCron;
return trim($replacedCron) . "\n";
}

throw new \RuntimeException('The number of replacements should be 1 or 0');
Expand Down
9 changes: 9 additions & 0 deletions tests/CronBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function it_builds(): void
###> Automatically generated by Setono Cron Builder - DO NOT EDIT ###
0 0 * * * /usr/bin/php /home/johndoe/public_html/send-report.php # Run every day at midnight
###< Automatically generated by Setono Cron Builder - DO NOT EDIT ###
CRON;

self::assertSame($expected, $crontab);
Expand All @@ -40,6 +41,7 @@ public function it_builds_with_prod_env_set(): void
0 0 * * * /usr/bin/php /home/johndoe/public_html/send-report.php # Run every day at midnight
0 0 * * * /usr/bin/php /home/johndoe/public_html/process.php
###< Automatically generated by Setono Cron Builder - DO NOT EDIT ###
CRON;

self::assertSame($expected, $crontab);
Expand All @@ -51,11 +53,17 @@ public function it_builds_with_prod_env_set(): void
public function it_merges(): void
{
$existingCrontab = <<<CRON
0 0 * * * /usr/bin/php /home/johndoe/public_html/other-job.php
###> Automatically generated by Setono Cron Builder - DO NOT EDIT ###
0 0 * * * /usr/bin/php /home/johndoe/public_html/delete-files.php
###< Automatically generated by Setono Cron Builder - DO NOT EDIT ###
CRON;

$expected = <<<CRON
Expand All @@ -64,6 +72,7 @@ public function it_merges(): void
###> Automatically generated by Setono Cron Builder - DO NOT EDIT ###
0 0 * * * /usr/bin/php /home/johndoe/public_html/send-report.php # Run every day at midnight
###< Automatically generated by Setono Cron Builder - DO NOT EDIT ###
CRON;

self::assertSame($expected, CronBuilder::merge($existingCrontab, self::getCronBuilder()));
Expand Down

0 comments on commit 40aa4e2

Please sign in to comment.