diff --git a/src/Indatus/Dispatcher/Platform.php b/src/Indatus/Dispatcher/Platform.php index d46ee74..bcbbddd 100644 --- a/src/Indatus/Dispatcher/Platform.php +++ b/src/Indatus/Dispatcher/Platform.php @@ -9,6 +9,9 @@ * file that was distributed with this source code. */ +/** + * @codeCoverageIgnore + */ class Platform { /** diff --git a/src/Indatus/Dispatcher/Services/CommandService.php b/src/Indatus/Dispatcher/Services/CommandService.php index c6507cf..8652472 100644 --- a/src/Indatus/Dispatcher/Services/CommandService.php +++ b/src/Indatus/Dispatcher/Services/CommandService.php @@ -130,6 +130,9 @@ public function getRunCommand( array $arguments = array(), array $options = array()) { + /** @var \Indatus\Dispatcher\Platform $platform */ + $platform = App::make('Indatus\Dispatcher\Platform'); + $commandPieces = array( 'php', base_path().'/artisan', @@ -144,12 +147,20 @@ public function getRunCommand( $commandPieces[] = $this->prepareOptions($options); } - $commandPieces[] = '&'; //run in background - $commandPieces[] = '> /dev/null 2>&1'; //don't show output, errors can be viewed in the Laravel log + if ($platform->isUnix()) { + $commandPieces[] = '> /dev/null'; //don't show output, errors can be viewed in the Laravel log + $commandPieces[] = '&'; //run in background + + //run the command as a different user + if (is_string($scheduledCommand->user())) { + array_unshift($commandPieces, 'sudo -u '.$scheduledCommand->user()); + } + } elseif($platform->isWindows()) { + $commandPieces[] = '> NUL'; //don't show output, errors can be viewed in the Laravel log - //run the command as a different user - if (is_string($scheduledCommand->user())) { - array_unshift($commandPieces, 'sudo -u '.$scheduledCommand->user()); + //run in background on windows + array_unshift($commandPieces, '/B'); + array_unshift($commandPieces, 'START'); } return implode(' ', $commandPieces); diff --git a/tests/Drivers/Cron/TestCronScheduler.php b/tests/Drivers/Cron/TestCronScheduler.php index 765903e..ebcae2c 100644 --- a/tests/Drivers/Cron/TestCronScheduler.php +++ b/tests/Drivers/Cron/TestCronScheduler.php @@ -183,7 +183,7 @@ public function testOpts() $this->assertEquals($args, $scheduler->getArguments()); $this->assertEquals($opts, $scheduler->getOptions()); - //be sure schedule reset, if not then we didn't get a fresh instance + //be sure schedule reset, if not then we didn't get a fresh SchedulerClass $this->assertEquals($scheduler->getSchedule(), $this->defaultSchedule); } diff --git a/tests/Services/TestCommandService.php b/tests/Services/TestCommandService.php index 5c17015..c2ea52b 100644 --- a/tests/Services/TestCommandService.php +++ b/tests/Services/TestCommandService.php @@ -158,8 +158,29 @@ public function testGetRunCommand() 'php', base_path().'/artisan', $commandName, - '&', - '> /dev/null 2>&1' + '> /dev/null', + '&' + ))); + } + + public function testGetRunCommandWindows() + { + $this->app->instance('Indatus\Dispatcher\Platform', m::mock('Indatus\Dispatcher\Platform', function ($m) { + $m->shouldReceive('isUnix')->andReturn(false); + $m->shouldReceive('isWindows')->andReturn(true); + })); + + $commandName = 'test:command'; + $scheduledCommand = $this->mockCommand(); + $scheduledCommand->shouldReceive('getName')->andReturn($commandName); + $scheduledCommand->shouldReceive('user')->andReturn(false); + $this->assertEquals($this->commandService->getRunCommand($scheduledCommand), implode(' ', array( + 'START', + '/B', + 'php', + base_path().'/artisan', + $commandName, + '> NUL' ))); } @@ -181,8 +202,8 @@ public function testGetRunCommandWithArguments() base_path().'/artisan', $commandName, 'option', - '&', - '> /dev/null 2>&1' + '> /dev/null', + '&' ))); } @@ -207,8 +228,8 @@ public function testGetRunCommandWithOptions() $commandName, '--option="value"', '--anotherOption', - '&', - '> /dev/null 2>&1' + '> /dev/null', + '&' ))); } @@ -224,8 +245,8 @@ public function testGetRunCommandAsUser() 'php', base_path().'/artisan', $commandName, - '&', - '> /dev/null 2>&1' + '> /dev/null', + '&' ))); }