Skip to content
Open
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
18 changes: 18 additions & 0 deletions src/Scheduler/Platform/Nix.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,22 @@ public function output()
{
return $this->output;
}

/**
* {@inheritdoc}
*/
public function scheduled($search = null)
{
$tasks = array();
$output = array();
exec(self::CRON . ' -l', $output);

foreach ($output as $line) {
if (null === $search || preg_match($search, $line)) {
$parts = explode(' ', $line, 6);
$tasks[] = $parts[5];
}
}
return $tasks;
}
}
8 changes: 8 additions & 0 deletions src/Scheduler/Platform/Windows.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public function output()
return $this->output;
}

/**
* {@inheritdoc}
*/
public function scheduled($search = null)
{
return array();
}

/**
* Builds the schedule command
*
Expand Down
8 changes: 8 additions & 0 deletions src/Scheduler/SchedulerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ public function save(ScheduleInterface $schedule, $cmd);
* @return array
*/
public function output();

/**
* Returns all scheduled tasks that match the given regular expression
*
* @param string $search The regular expression search string
* @param array An array of scheduled tasks
*/
public function scheduled($search = null);
}
101 changes: 82 additions & 19 deletions tests/unit/Scheduler/Platform/NixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
namespace Tictock\Tests\Scheduler\Platform {

use Tictock\Scheduler\Platform\Nix;
use Tictock\Scheduler\SchedulerInterface;
use Tictock\Schedule\ScheduleInterface;
use PHPUnit_Framework_TestCase;

/**
* @coversDefaultClass \Tictock\Scheduler\Platform\Nix
*/
class NixTest extends PHPUnit_Framework_TestCase
{
protected static $expectedCmd = 'crontab */5 * * * * /path/to/program';
protected static $shouldOutput = array('crontab: installing new crontab');
protected static $shouldReturn = 0;
protected static $execCmd = null;
protected static $execReturn = null;
protected static $execOut = null;

/**
* @covers ::save
* @covers ::output
Expand All @@ -24,30 +22,95 @@ public function testSave()
$cmd = '/path/to/program';
$period = '*/5 * * * *';
$nix = new Nix();


$expectedOut = 'crontab: installing new crontab';
$expectedReturn = 0;

$this->execShould(
'crontab ' . $period . ' ' . $cmd,
$expectedOut,
$expectedReturn
);

$schedule = $this->getMockBuilder('\Tictock\Schedule\ScheduleInterface')
->getMock();
$schedule->expects($this->once())
->method('getShorthand')
->will($this->returnValue($period));

$this->assertEquals(
self::$shouldReturn,
$expectedReturn,
$nix->save($schedule, $cmd)
);
$this->assertEquals(self::$shouldOutput, $nix->output());

$this->assertEquals($expectedOut, $nix->output());
}


/**
* @covers ::scheduled
*/
public function testScheduled()
{
$nix = new Nix();

$this->execShould(
'crontab -l',
array(
'*/5 * * * * /path/to/program',
'* * * * * program param1 param2 < in > out'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a test to cover a commented out task.

)
);

$this->assertEquals(
array(
'/path/to/program',
'program param1 param2 < in > out'
),
$nix->scheduled()
);
}

/**
* @covers ::scheduled
*/
public function testScheduledWithSearch()
{
$nix = new Nix();

$this->execShould(
'crontab -l',
array('* * * * * program param1 param2 < in > out')
);

$this->assertEquals(
array('program param1 param2 < in > out'),
$nix->scheduled('/out/')
);
}

/**
* Set the expectation for exec()
*
* @param string $expect
* @param string $output
* @param int $return
*/
protected static function execShould($expect = null, $output = null, $return = null)
{
self::$execCmd = $expect;
self::$execOut = $output;
self::$execReturn = $return;
}

/**
* A cheap exec mock
*/
public static function mockExec($cmd, array &$out, &$return)
{
if ($cmd === self::$expectedCmd) {
$out = self::$shouldOutput;
$return = self::$shouldReturn;
return;
if (self::$execCmd === $cmd) {
$return = self::$execReturn;
$out = self::$execOut;
return $return;
}
$out = 'error';
return 1;
Expand All @@ -56,8 +119,8 @@ public static function mockExec($cmd, array &$out, &$return)
}

namespace Tictock\Scheduler\Platform {
function exec($cmd, array &$out, &$return)

function exec($cmd, array &$out = null, &$return = null)
{
\Tictock\Tests\Scheduler\Platform\NixTest::mockExec($cmd, $out, $return);
}
Expand Down