Skip to content

Commit

Permalink
Merge pull request #61 from troyharvey/47-fortnights
Browse files Browse the repository at this point in the history
Support for scheduling fortnight commands #47
  • Loading branch information
bkuhl committed Jul 28, 2014
2 parents 42f5c92 + d2a26dd commit b604f57
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/Indatus/Dispatcher/Drivers/Cron/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* file that was distributed with this source code.
*/

use App;
use Indatus\Dispatcher\Scheduling\Schedulable;

class Scheduler extends Schedulable
Expand Down Expand Up @@ -122,7 +123,30 @@ public function monthly()
}

/**
* Run once a week at midnight in the morning of the first day of the month
* Run once every other week at midnight on Sunday morning
*
* @return $this
*/
public function everyOtherWeek()
{
$carbon = App::make('Carbon');
$weeks = 2;

if ($carbon->now()->weekOfYear % $weeks == 0) {
$this->setScheduleMinute(0);
$this->setScheduleHour(0);
$this->setScheduleDayOfMonth(self::ANY);
$this->setScheduleMonth(self::ANY);
$this->setScheduleDayOfWeek("0");
} else {
return $this->never();
}

return $this;
}

/**
* Run once a week at midnight on Sunday morning
*
* @return $this
*/
Expand Down Expand Up @@ -169,6 +193,22 @@ public function hourly()
return $this;
}

/**
* Valid cron syntax that will never run. Feb 31st?!
* @see http://stackoverflow.com/a/13938099
* @return $this
*/
public function never()
{
$this->setScheduleMinute(0);
$this->setScheduleHour(0);
$this->setScheduleDayOfMonth(31);
$this->setScheduleMonth(2);
$this->setScheduleDayOfWeek(self::ANY);

return $this;
}

/**
* Set the minutes under which this command will run
* @param mixed $minutesIntoTheHour
Expand Down
29 changes: 29 additions & 0 deletions tests/Drivers/Cron/TestCronScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @author Ben Kuhl <[email protected]>
*/

use Mockery as m;
use Indatus\Dispatcher\Drivers\Cron\Scheduler;

class TestCronScheduler extends TestCase
Expand Down Expand Up @@ -50,6 +51,28 @@ public function testMonthly()
$this->assertEquals($this->scheduler->getSchedule(), '0 0 1 '.Scheduler::ANY.' '.Scheduler::ANY);
}

public function testEveryOtherWeekEven()
{
$carbon = m::mock();
$carbon->shouldReceive('now')->andReturn($carbon);
$carbon->weekOfYear = 32;
App::instance('Carbon', $carbon);

$this->assertInstanceOf($this->schedulerClass, $this->scheduler->everyOtherWeek());
$this->assertEquals($this->scheduler->getSchedule(), '0 0 '.Scheduler::ANY.' '.Scheduler::ANY.' 0');
}

public function testEveryOtherWeekOdd()
{
$carbon = m::mock();
$carbon->shouldReceive('now')->andReturn($carbon);
$carbon->weekOfYear = 33;
App::instance('Carbon', $carbon);

$this->assertInstanceOf($this->schedulerClass, $this->scheduler->everyOtherWeek());
$this->assertEquals($this->scheduler->getSchedule(), '0 0 31 2 '.Scheduler::ANY);
}

public function testWeekly()
{
$this->assertInstanceOf($this->schedulerClass, $this->scheduler->weekly());
Expand All @@ -68,6 +91,12 @@ public function testHourly()
$this->assertEquals($this->scheduler->getSchedule(), '0 '.Scheduler::ANY.' '.Scheduler::ANY.' '.Scheduler::ANY.' '.Scheduler::ANY);
}

public function testNever()
{
$this->assertInstanceOf($this->schedulerClass, $this->scheduler->never());
$this->assertEquals($this->scheduler->getSchedule(), '0 0 31 2 '.Scheduler::ANY);
}

public function testMinutes()
{
$minutes = 15;
Expand Down

0 comments on commit b604f57

Please sign in to comment.