Skip to content
Merged
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
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 unlessBetween($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->unlessBetween("8:00", "10:00")->filtersPass($app));
$this->assertTrue($event->unlessBetween("10:00", "11:00")->isDue($app));
}
}