Skip to content

Commit

Permalink
Added Schedule methods: months, daysOfWeek, daysOfMonth (#37)
Browse files Browse the repository at this point in the history
Allow using crontab value lists and ranges
Added Unittests for new methods
Added .editorconfig for consistent code style
  • Loading branch information
fwolfsjaeger authored Aug 4, 2021
1 parent 3351488 commit 6daa871
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 71 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
}

$table->addRow($row);
};
}

$table->render();

Expand Down
2 changes: 1 addition & 1 deletion Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions Event/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
86 changes: 66 additions & 20 deletions Task/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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 {
Expand All @@ -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();
}

Expand All @@ -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;
}
Expand All @@ -129,18 +174,19 @@ 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;
}

/**
* Return true if the schedule is due to now
* @param DateTimeInterface|string $currentTime
* @return bool
*/
public function isDue($currentTime = 'now') {
public function isDue($currentTime = 'now'): bool
{
return $this->cron->isDue($currentTime);
}
}
125 changes: 78 additions & 47 deletions Tests/Task/ScheduleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 * * * *");
Expand All @@ -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');
}
}

0 comments on commit 6daa871

Please sign in to comment.