Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(event): missing api from laravel #41

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 81 additions & 2 deletions src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,17 @@ public function hourly()
return $this->cron('0 * * * * *');
}

/**
* Schedule the event to run hourly at a given offset in the hour.
*
* @param int $offset
* @return $this
*/
public function hourlyAt($offset)
{
return $this->spliceIntoPosition(1, $offset);
}

/**
* The Cron expression representing the event's frequency.
*
Expand Down Expand Up @@ -287,11 +298,16 @@ protected function spliceIntoPosition($position, $value)
/**
* Schedule the event to run twice daily.
*
* @param int $first
* @param int $second
* @return $this
*/
public function twiceDaily()
public function twiceDaily($first = 1, $second = 13)
{
return $this->cron('0 1,13 * * * *');
$hours = $first.','.$second;

return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, $hours);
}

/**
Expand All @@ -304,6 +320,16 @@ public function weekdays()
return $this->spliceIntoPosition(5, '1-5');
}

/**
* Schedule the event to run only on weekends.
*
* @return $this
*/
public function weekends()
{
return $this->spliceIntoPosition(5, '0,6');
}

/**
* Schedule the event to run only on Mondays.
*
Expand Down Expand Up @@ -419,6 +445,49 @@ public function monthly()
return $this->cron('0 0 1 * * *');
}

/**
* Schedule the event to run monthly on a given day and time.
*
* @param int $day
* @param string $time
* @return $this
*/
public function monthlyOn($day = 1, $time = '0:0')
{
$this->dailyAt($time);

return $this->spliceIntoPosition(3, $day);
}

/**
* Schedule the event to run twice monthly.
*
* @param int $first
* @param int $second
* @return $this
*/
public function twiceMonthly($first = 1, $second = 16)
{
$days = $first.','.$second;

return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, 0)
->spliceIntoPosition(3, $days);
}

/**
* Schedule the event to run quarterly.
*
* @return $this
*/
public function quarterly()
{
return $this->spliceIntoPosition(1, 0)
->spliceIntoPosition(2, 0)
->spliceIntoPosition(3, 1)
->spliceIntoPosition(4, '1-12/3');
}

/**
* Schedule the event to run yearly.
*
Expand Down Expand Up @@ -470,6 +539,16 @@ public function everyTenMinutes()
return $this->everyNMinutes(10);
}

/**
* Schedule the event to run every fifteen minutes.
*
* @return $this
*/
public function everyFifteenMinutes()
{
return $this->spliceIntoPosition(1, '*/15');
}

/**
* Schedule the event to run every thirty minutes.
*
Expand Down
34 changes: 34 additions & 0 deletions tests/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,38 @@ public function testBuildCommandSendOutputTo($command, $outputTo, $result)
$event->sendOutputTo($outputTo);
$this->assertSame($result, $event->buildCommand());
}

public function testExpression()
{
$event = new Event($this->getMock(Mutex::className()), 'test');
$this->assertSame('0 * * * * *', $event->hourly()->getExpression());
$this->assertSame('13 * * * * *', $event->everyMinute()->hourlyAt(13)->getExpression());
$this->assertSame('0 5 0 1 *', $event->everyMinute()->cron('0 5 0 1 *')->getExpression());
$this->assertSame('0 0 * * * *', $event->everyMinute()->daily()->getExpression());
$this->assertSame('0 13 * * 1 *', $event->everyMinute()->weekly()->mondays()->at('13:00')->getExpression());
$this->assertSame('0 4 * * * *', $event->everyMinute()->dailyAt('04:00')->getExpression());
$this->assertSame('0 1,13 * * * *', $event->everyMinute()->twiceDaily()->getExpression());
$this->assertSame('* * * * 1-5 *', $event->everyMinute()->weekdays()->getExpression());
$this->assertSame('* * * * 0,6 *', $event->everyMinute()->weekends()->getExpression());
$this->assertSame('* * * * 1 *', $event->everyMinute()->mondays()->getExpression());
$this->assertSame('* * * * 2 *', $event->everyMinute()->days(2)->getExpression());
$this->assertSame('* * * * 2 *', $event->everyMinute()->tuesdays()->getExpression());
$this->assertSame('* * * * 3 *', $event->everyMinute()->wednesdays()->getExpression());
$this->assertSame('* * * * 4 *', $event->everyMinute()->thursdays()->getExpression());
$this->assertSame('* * * * 5 *', $event->everyMinute()->fridays()->getExpression());
$this->assertSame('* * * * 6 *', $event->everyMinute()->saturdays()->getExpression());
$this->assertSame('* * * * 0 *', $event->everyMinute()->sundays()->getExpression());
$this->assertSame('0 0 * * 0 *', $event->everyMinute()->weekly()->getExpression());
$this->assertSame('0 9 * * 3 *', $event->everyMinute()->weeklyOn(3, '9:00')->getExpression());
$this->assertSame('0 8 2 * * *', $event->everyMinute()->monthlyOn(2, '8:00')->getExpression());
$this->assertSame('0 0 2,8 * * *', $event->everyMinute()->twiceMonthly(2, 8)->getExpression());
$this->assertSame('0 0 1 1-12/3 * *', $event->everyMinute()->quarterly()->getExpression());
$this->assertSame('0 0 1 1 * *', $event->everyMinute()->yearly()->getExpression());
$this->assertSame('* * * * * *', $event->everyMinute()->getExpression());
$this->assertSame('*/6 * * * * *', $event->everyNMinutes(6)->getExpression());
$this->assertSame('*/5 * * * * *', $event->everyFiveMinutes()->getExpression());
$this->assertSame('*/10 * * * * *', $event->everyTenMinutes()->getExpression());
$this->assertSame('*/15 * * * * *', $event->everyFifteenMinutes()->getExpression());
$this->assertSame('0,30 * * * * *', $event->everyThirtyMinutes()->getExpression());
}
}