From 40aa4e271e4e35c3f184861fdee51b2a5dbee382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Fri, 16 Aug 2024 16:13:25 +0200 Subject: [PATCH] Some small modifications to output and the allowed context keys --- src/CronBuilder.php | 10 ++++------ tests/CronBuilderTest.php | 9 +++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/CronBuilder.php b/src/CronBuilder.php index 9d1e5ed..ce89f7a 100644 --- a/src/CronBuilder.php +++ b/src/CronBuilder.php @@ -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)); } @@ -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(); } @@ -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'); diff --git a/tests/CronBuilderTest.php b/tests/CronBuilderTest.php index f309278..abccd22 100644 --- a/tests/CronBuilderTest.php +++ b/tests/CronBuilderTest.php @@ -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); @@ -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); @@ -51,11 +53,17 @@ public function it_builds_with_prod_env_set(): void public function it_merges(): void { $existingCrontab = << 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 = << 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()));