From 6daa8714358a14961dbdde869b1cbf2650d66f8d Mon Sep 17 00:00:00 2001 From: fwolfsjaeger Date: Wed, 4 Aug 2021 21:21:03 +0200 Subject: [PATCH] Added Schedule methods: months, daysOfWeek, daysOfMonth (#37) Allow using crontab value lists and ranges Added Unittests for new methods Added .editorconfig for consistent code style --- .editorconfig | 9 +++ Command/ListCommand.php | 2 +- Command/RunCommand.php | 2 +- Event/EventDispatcher.php | 4 +- Task/Schedule.php | 86 +++++++++++++++++++------ Tests/Task/ScheduleTest.php | 125 ++++++++++++++++++++++-------------- 6 files changed, 157 insertions(+), 71 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..99580d0 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 +trim_trailing_whitespace = true diff --git a/Command/ListCommand.php b/Command/ListCommand.php index 56f66ee..6f41d3e 100644 --- a/Command/ListCommand.php +++ b/Command/ListCommand.php @@ -57,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { } $table->addRow($row); - }; + } $table->render(); diff --git a/Command/RunCommand.php b/Command/RunCommand.php index 98150df..385102c 100644 --- a/Command/RunCommand.php +++ b/Command/RunCommand.php @@ -41,7 +41,7 @@ protected function execute(InputInterface $input, OutputInterface $output) { $this->scheduler->run(); } else { $tasks = $this->scheduler->getTasks(); - $id = intval($id); + $id = (int)$id; if (array_key_exists($id - 1, $tasks) === false) { throw new \Exception("There are no tasks corresponding to this ID"); diff --git a/Event/EventDispatcher.php b/Event/EventDispatcher.php index 28adc3b..298217f 100644 --- a/Event/EventDispatcher.php +++ b/Event/EventDispatcher.php @@ -36,10 +36,10 @@ public function getSubscribers() { */ public function dispatch(string $event, $args = []) { foreach($this->subscribers as $subscriber) { - $events = $subscriber->getEvents(); + $events = $subscriber::getEvents(); $keys = array_keys($events); - if (in_array($event, $keys)) { + if (in_array($event, $keys, true)) { call_user_func_array([$subscriber, $events[$event]], $args); } } diff --git a/Task/Schedule.php b/Task/Schedule.php index 9659eac..148fc4d 100644 --- a/Task/Schedule.php +++ b/Task/Schedule.php @@ -27,38 +27,77 @@ class Schedule { * Schedule constructor. * @param string $expr the default cron */ - public function __construct($expr = "* * * * *") + public function __construct(string $expr = "* * * * *") { $this->cron = new CronExpression($expr, new FieldFactory()); } + /** + * Sets the cron to work at these months + * @param string|int $months + * @return $this + */ + public function months($months): self + { + $this->cron->setPart(CronExpression::MONTH, $months); + return $this; + } + + /** + * Sets the cron to work at these days of the week + * ATTENTION: resets daysOfMonth + * @param string|int $days + * @return $this + */ + public function daysOfWeek($days): self + { + $this->cron->setPart(CronExpression::DAY, '*'); + $this->cron->setPart(CronExpression::WEEKDAY, $days); + return $this; + } + + /** + * Sets the cron to work at these days of the month + * ATTENTION: resets daysOfWeek + * @param string|int $days + * @return $this + */ + public function daysOfMonth($days): self + { + $this->cron->setPart(CronExpression::DAY, $days); + $this->cron->setPart(CronExpression::WEEKDAY, '*'); + return $this; + } + /** * Sets the cron to work every day * @return $this */ - public function daily() + public function daily(): self { $this->cron->setPart(CronExpression::DAY, "*"); return $this; } /** - * Set the cron to work at this hour - * @param int $hour + * Set the cron to work at these hours + * @param string|int $hours * @return $this */ - public function hours(int $hour) { - $this->cron->setPart(CronExpression::HOUR, (string)$hour); + public function hours($hours): self + { + $this->cron->setPart(CronExpression::HOUR, (string)$hours); return $this; } /** - * Set the cron to work at this minutes - * @param int $minutes + * Set the cron to work at these minutes + * @param string|int $minutes * @return $this */ - public function minutes(int $minutes) { - $this->cron->setPart(CronExpression::MINUTE, (string) $minutes); + public function minutes($minutes): self + { + $this->cron->setPart(CronExpression::MINUTE, (string)$minutes); return $this; } @@ -67,7 +106,8 @@ public function minutes(int $minutes) { * @param int $minutes * @return $this */ - public function everyMinutes(int $minutes = 1) { + public function everyMinutes(int $minutes = 1): self + { return $this->everyX($minutes, CronExpression::MINUTE); } @@ -76,7 +116,8 @@ public function everyMinutes(int $minutes = 1) { * @param int $hours * @return $this */ - public function everyHours(int $hours = 1) { + public function everyHours(int $hours = 1): self + { return $this->everyX($hours, CronExpression::HOUR); } @@ -88,7 +129,8 @@ public function everyHours(int $hours = 1) { * @param int $part * @return $this */ - public function everyX(int $time = 1, int $part = CronExpression::MINUTE) { + public function everyX(int $time = 1, int $part = CronExpression::MINUTE): self + { if ($time === 0 || $time === 1) { $expr = "*"; } else { @@ -102,14 +144,16 @@ public function everyX(int $time = 1, int $part = CronExpression::MINUTE) { /** * @return CronExpression */ - public function getCron() { + public function getCron(): CronExpression + { return $this->cron; } /** * @return string */ - public function getExpression() { + public function getExpression(): string + { return $this->cron->getExpression(); } @@ -119,7 +163,8 @@ public function getExpression() { * @param string $value * @return $this */ - public function setExpression(string $value) { + public function setExpression(string $value): self + { $this->cron->setExpression($value); return $this; } @@ -129,10 +174,10 @@ public function setExpression(string $value) { * @param string $value * @return $this */ - public function setPart(int $position, string $value) + public function setPart(int $position, string $value): self { - $this->cron->setPart($position, $value); - return $this; + $this->cron->setPart($position, $value); + return $this; } /** @@ -140,7 +185,8 @@ public function setPart(int $position, string $value) * @param DateTimeInterface|string $currentTime * @return bool */ - public function isDue($currentTime = 'now') { + public function isDue($currentTime = 'now'): bool + { return $this->cron->isDue($currentTime); } } diff --git a/Tests/Task/ScheduleTest.php b/Tests/Task/ScheduleTest.php index 7713611..7749763 100644 --- a/Tests/Task/ScheduleTest.php +++ b/Tests/Task/ScheduleTest.php @@ -73,6 +73,37 @@ public function testDaily() { "1 2 * 4 5" ); } + + public function testMonths() { + $schedule = new Schedule("1 2 3 4 5"); + $schedule->months('7-9'); + + $this->assertEquals( + $schedule->getExpression(), + "1 2 3 7-9 5" + ); + } + + public function testDaysOfWeek() { + $schedule = new Schedule("1 2 3 4 5"); + $schedule->daysOfWeek('mon'); + + $this->assertEquals( + $schedule->getExpression(), + "1 2 * 4 mon" + ); + } + + public function testDaysOfMonth() { + $schedule = new Schedule("1 2 3 4 5"); + $schedule->daysOfMonth('7,8,9'); + + $this->assertEquals( + $schedule->getExpression(), + "1 2 7,8,9 4 *" + ); + } + public function testSetExpression() { $schedule = new Schedule("* * * * *"); $schedule->setExpression("0 * * * *"); @@ -82,52 +113,52 @@ public function testSetExpression() { "0 * * * *" ); } - public function testSetExpressionAllowedValues() - { - $schedule = new Schedule("* * 2,7,12 * *"); - $schedule->setExpression("0 8-12 2,7,12 oct sat,sun"); - - $this->assertEquals( - "0 8-12 2,7,12 oct sat,sun", - $schedule->getExpression() - - ); - } - - public function testSetPartExpression() - { - $schedule = new Schedule(); - $schedule->setPart(CronExpression::MINUTE, '0'); - $schedule->setPart(CronExpression::HOUR, '8-12'); - $schedule->setPart(CronExpression::DAY, '2,7,12'); - $schedule->setPart(CronExpression::MONTH, 'oct'); - $schedule->setPart(CronExpression::WEEKDAY, 'sat,sun'); - - $this->assertEquals( - "0 8-12 2,7,12 oct sat,sun", - $schedule->getExpression() - ); - } - - public function testInvalidExpressionFullMonth() - { - $this->expectException(InvalidArgumentException::class); - $schedule = new Schedule(); - $schedule->setPart(CronExpression::MONTH, 'october'); - } - - public function testInvalidExpressionUnknownValue() - { - $this->expectException(InvalidArgumentException::class); - $schedule = new Schedule(); - $schedule->setPart(CronExpression::MONTH, 'movember'); - } - - public function testInvalidExpressionNegative() - { - $this->expectException(InvalidArgumentException::class); - $schedule = new Schedule(); - $schedule->setPart(CronExpression::MINUTE, '-5'); - } + public function testSetExpressionAllowedValues() + { + $schedule = new Schedule("* * 2,7,12 * *"); + $schedule->setExpression("0 8-12 2,7,12 oct sat,sun"); + + $this->assertEquals( + "0 8-12 2,7,12 oct sat,sun", + $schedule->getExpression() + + ); + } + + public function testSetPartExpression() + { + $schedule = new Schedule(); + $schedule->setPart(CronExpression::MINUTE, '0'); + $schedule->setPart(CronExpression::HOUR, '8-12'); + $schedule->setPart(CronExpression::DAY, '2,7,12'); + $schedule->setPart(CronExpression::MONTH, 'oct'); + $schedule->setPart(CronExpression::WEEKDAY, 'sat,sun'); + + $this->assertEquals( + "0 8-12 2,7,12 oct sat,sun", + $schedule->getExpression() + ); + } + + public function testInvalidExpressionFullMonth() + { + $this->expectException(InvalidArgumentException::class); + $schedule = new Schedule(); + $schedule->setPart(CronExpression::MONTH, 'october'); + } + + public function testInvalidExpressionUnknownValue() + { + $this->expectException(InvalidArgumentException::class); + $schedule = new Schedule(); + $schedule->setPart(CronExpression::MONTH, 'movember'); + } + + public function testInvalidExpressionNegative() + { + $this->expectException(InvalidArgumentException::class); + $schedule = new Schedule(); + $schedule->setPart(CronExpression::MINUTE, '-5'); + } }