diff --git a/src/Scheduler/Platform/Nix.php b/src/Scheduler/Platform/Nix.php index bcff9e0..b270a41 100644 --- a/src/Scheduler/Platform/Nix.php +++ b/src/Scheduler/Platform/Nix.php @@ -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; + } } diff --git a/src/Scheduler/Platform/Windows.php b/src/Scheduler/Platform/Windows.php index f9046c6..ef8bba2 100644 --- a/src/Scheduler/Platform/Windows.php +++ b/src/Scheduler/Platform/Windows.php @@ -54,6 +54,14 @@ public function output() return $this->output; } + /** + * {@inheritdoc} + */ + public function scheduled($search = null) + { + return array(); + } + /** * Builds the schedule command * diff --git a/src/Scheduler/SchedulerInterface.php b/src/Scheduler/SchedulerInterface.php index a2c7e89..e0ce9dc 100644 --- a/src/Scheduler/SchedulerInterface.php +++ b/src/Scheduler/SchedulerInterface.php @@ -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); } diff --git a/tests/unit/Scheduler/Platform/NixTest.php b/tests/unit/Scheduler/Platform/NixTest.php index 50a0ac2..2b9ffaf 100644 --- a/tests/unit/Scheduler/Platform/NixTest.php +++ b/tests/unit/Scheduler/Platform/NixTest.php @@ -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 @@ -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' + ) + ); + + $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; @@ -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); }