Skip to content

Commit

Permalink
Added ability to check if between two times when using Scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBennett committed Sep 1, 2016
1 parent 262145d commit b7983bb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Illuminate/Console/Scheduling/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,49 @@ public function skip(Closure $callback)
return $this;
}

/**
* Schedule the event to run between start and end time.
*
* @param string $startTime
* @param string $endTime
* @return $this
*/
public function between($startTime, $endTime)
{
return $this->when($this->inTimeInterval($startTime, $endTime));
}

/**
* Schedule the event to not run between start and end time.
*
* @param string $startTime
* @param string $endTime
* @return $this
*/
public function exceptBetween($startTime, $endTime)
{
return $this->skip($this->inTimeInterval($startTime, $endTime));
}

/**
* Schedule the event to run between start and end time.
*
* @param string $startTime
* @param string $endTime
* @return \Closure
*/
private function inTimeInterval($startTime, $endTime)
{
$startTime = str_replace(":", "", $startTime);
$endTime = str_replace(":", "", $endTime);

return function() use($startTime, $endTime) {
$now = Carbon::now()->format('Hi');

return $now >= $startTime && $now <= $endTime;
};
}

/**
* Send the output of the command to a given location.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Console/ConsoleScheduledEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,20 @@ public function testEventIsDueCheck()
$this->assertEquals('0 19 * * 3 *', $event->wednesdays()->at('19:00')->timezone('EST')->getExpression());
$this->assertTrue($event->isDue($app));
}

public function testTimeBetweenChecks()
{
$app = m::mock('Illuminate\Foundation\Application[isDownForMaintenance,environment]');
$app->shouldReceive('isDownForMaintenance')->andReturn(false);
$app->shouldReceive('environment')->andReturn('production');
Carbon::setTestNow(Carbon::create(2015, 1, 1, 9, 0, 0));

$event = new Event('php foo');
$this->assertTrue($event->between("8:00", "10:00")->filtersPass($app));
$this->assertTrue($event->between("9:00", "9:00")->filtersPass($app));
$this->assertFalse($event->between("10:00", "11:00")->filtersPass($app));

$this->assertFalse($event->exceptBetween("8:00", "10:00")->filtersPass($app));
$this->assertTrue($event->exceptBetween("10:00", "11:00")->isDue($app));
}
}

0 comments on commit b7983bb

Please sign in to comment.