Skip to content

Commit

Permalink
printing the summary is now the responsibility of the driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Kuhl committed Mar 24, 2014
1 parent 15e7097 commit d5290df
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 68 deletions.
33 changes: 33 additions & 0 deletions src/Indatus/Dispatcher/Drivers/Cron/ScheduleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,37 @@ public function isDue(ScheduledCommand $command)
$cron = CronExpression::factory($command->schedule($scheduler)->getSchedule());
return $cron->isDue();
}

/**
* @inheritDoc
*/
public function printSummary()
{
$this->table->setHeaders(array('Environment(s)', 'Name', 'Minute', 'Hour', 'Day of Month', 'Month', 'Day of Week', 'Run as'));
/** @var $command \Indatus\Dispatcher\ScheduledCommand */
$commands = 0;
$activeCommands = 0;
foreach ($this->getScheduledCommands() as $command) {
/** @var $command \Indatus\Dispatcher\ScheduledCommand */
$scheduler = $command->schedule(App::make('Indatus\Dispatcher\Schedulable'));

$this->table->addRow(array(
is_array($command->environment()) ? implode(',', $command->environment()) : $command->environment(),
$command->getName(),
$scheduler->getScheduleMinute(),
$scheduler->getScheduleHour(),
$scheduler->getScheduleDayOfMonth(),
$scheduler->getScheduleMonth(),
$scheduler->getScheduleDayOfWeek(),
$command->user()
));
$commands++;
$activeCommands++;
}

//sort by first column
$this->table->sort(0);

$this->table->display();
}
}
2 changes: 1 addition & 1 deletion src/Indatus/Dispatcher/Drivers/Cron/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Scheduler implements Schedulable
{

/**
* @var string Any of the contextual timeframe
* @var string Any of the contextual time frame
*/
const ANY = '*';

Expand Down
31 changes: 1 addition & 30 deletions src/Indatus/Dispatcher/Services/ScheduleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,37 +68,8 @@ public function getDueCommands()

/**
* Review scheduled commands schedule, active status, etc.
* @todo refactor this... it's ugly. The output goes directly to STDOUT
* @return void
*/
public function printSummary()
{
$this->table->setHeaders(array('Environment(s)', 'Name', 'Minute', 'Hour', 'Day of Month', 'Month', 'Day of Week', 'Run as'));
/** @var $command \Indatus\Dispatcher\ScheduledCommand */
$commands = 0;
$activeCommands = 0;
foreach ($this->getScheduledCommands() as $command) {
/** @var $command \Indatus\Dispatcher\ScheduledCommand */
$scheduler = $command->schedule(App::make('Indatus\Dispatcher\Schedulable'));

$this->table->addRow(array(
is_array($command->environment()) ? implode(',', $command->environment()) : $command->environment(),
$command->getName(),
$scheduler->getScheduleMinute(),
$scheduler->getScheduleHour(),
$scheduler->getScheduleDayOfMonth(),
$scheduler->getScheduleMonth(),
$scheduler->getScheduleDayOfWeek(),
$command->user()
));
$commands++;
$activeCommands++;
}

//sort by first column
$this->table->sort(0);

$this->table->display();
}
abstract public function printSummary();

}
55 changes: 55 additions & 0 deletions tests/Drivers/Cron/TestCronScheduleService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* @author Ben Kuhl <[email protected]>
*/

use Mockery as m;

class TestCronScheduleService extends TestCase
{
public function setUp()
{
parent::setUp();
}

public function tearDown()
{
parent::tearDown();
m::close();
}

/**
* Test that a summary is properly generated
* Dangit this is ugly... gotta find a new cli library
*/
public function testPrintSummary()
{
$table = m::mock('Indatus\Dispatcher\Table', function ($m) {
$m->shouldReceive('setHeaders')->once();
$m->shouldReceive('sort')->once();
$m->shouldReceive('display')->once();
});
$scheduledCommand = m::mock('Indatus\Dispatcher\ScheduledCommand', function ($m) use ($table) {
$table->shouldReceive('addRow')->once();

$m->shouldReceive('getName')->once();
$m->shouldReceive('user')->once();
$m->shouldReceive('environment')->twice();
$m->shouldReceive('schedule')->once()->andReturn(m::mock('Indatus\Dispatcher\Drivers\Cron\Scheduler', function ($m) {
$m->shouldReceive('getScheduleMinute');
$m->shouldReceive('getScheduleHour');
$m->shouldReceive('getScheduleDayOfMonth');
$m->shouldReceive('getScheduleMonth');
$m->shouldReceive('getScheduleDayOfWeek');
}));
});
$scheduleService = m::mock('Indatus\Dispatcher\Drivers\Cron\ScheduleService[getScheduledCommands]', array(
$table
), function ($m) use ($scheduledCommand) {
$m->shouldReceive('getScheduledCommands')->once()->andReturn(array(
$scheduledCommand
));
});
$scheduleService->printSummary();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use \Indatus\Dispatcher\Drivers\Cron\Scheduler;

class TestScheduler extends TestCase
class TestCronScheduler extends TestCase
{
/**
* @var Indatus\Dispatcher\Scheduler
Expand All @@ -16,6 +16,7 @@ class TestScheduler extends TestCase

public function setUp()
{
parent::setUp();
$this->scheduler = new Scheduler();
}

Expand Down
37 changes: 1 addition & 36 deletions tests/Services/TestScheduleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

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

class TestScheduleService extends TestCase
{
Expand Down Expand Up @@ -39,41 +39,6 @@ public function testGetScheduledCommands()
$this->assertSameSize($scheduledCommands, $this->scheduleService->getScheduledCommands());
}

/**
* Test that a summary is properly generated
* Dangit this is ugly... gotta find a new cli library
*/
public function testPrintSummary()
{
$table = m::mock('Indatus\Dispatcher\Table', function ($m) {
$m->shouldReceive('setHeaders')->once();
$m->shouldReceive('sort')->once();
$m->shouldReceive('display')->once();
});
$scheduledCommand = m::mock('Indatus\Dispatcher\ScheduledCommand', function ($m) use ($table) {
$table->shouldReceive('addRow')->once();

$m->shouldReceive('getName')->once();
$m->shouldReceive('user')->once();
$m->shouldReceive('environment')->twice();
$m->shouldReceive('schedule')->once()->andReturn(m::mock('Indatus\Dispatcher\Drivers\Cron\Scheduler', function ($m) {
$m->shouldReceive('getScheduleMinute');
$m->shouldReceive('getScheduleHour');
$m->shouldReceive('getScheduleDayOfMonth');
$m->shouldReceive('getScheduleMonth');
$m->shouldReceive('getScheduleDayOfWeek');
}));
});
$scheduleService = m::mock('Indatus\Dispatcher\Drivers\Cron\ScheduleService[getScheduledCommands]', array(
$table
), function ($m) use ($scheduledCommand) {
$m->shouldReceive('getScheduledCommands')->once()->andReturn(array(
$scheduledCommand
));
});
$scheduleService->printSummary();
}

public function testGetDueCommands()
{
$scheduleService = m::mock('Indatus\Dispatcher\Drivers\Cron\ScheduleService[getScheduledCommands,isDue]', array(
Expand Down

0 comments on commit d5290df

Please sign in to comment.