Skip to content

Commit

Permalink
Merge pull request #24 from hmazter/handle-scheduled-callback-and-jobs
Browse files Browse the repository at this point in the history
Handle scheduled Closures and Jobs
  • Loading branch information
hmazter authored Sep 23, 2018
2 parents d97d03e + d166ce5 commit 9bd0c52
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/ScheduleEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ public function getShortCommand(): string
*/
public function getCommandName(): string
{
list($commandName) = Parser::parse($this->getShortCommand());
$shortCommand = $this->getShortCommand();
if (empty($shortCommand)) {
return '';
}
list($commandName) = Parser::parse($shortCommand);
return $commandName;
}

Expand Down
7 changes: 6 additions & 1 deletion src/ScheduleList.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Hmazter\LaravelScheduleList;

use Illuminate\Console\Scheduling\CallbackEvent;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
Expand Down Expand Up @@ -40,14 +41,18 @@ public function all(): array
foreach ($this->schedule->events() as $event) {
$fullCommand = $event->buildCommand();

if ($event instanceof CallbackEvent) {
$fullCommand = 'Closure' . $fullCommand;
}

$scheduleEvent = new ScheduleEvent(
$event->getExpression(),
$event->timezone,
$fullCommand,
$event->description ?? ''
);

if (empty($scheduleEvent->getDescription())) {
if (empty($scheduleEvent->getDescription()) && $scheduleEvent->getCommandName()) {
$scheduleEvent->setDescription($this->getDescriptionFromCommand($scheduleEvent->getCommandName()));
}

Expand Down
26 changes: 19 additions & 7 deletions tests/Integration/ListSchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,32 @@ public function testListSchedulerCommand_withTasksAndTableStyle()
self::assertContains('Description of test command', $consoleOutput[4]);

self::assertContains('ls -lah', $consoleOutput[5]);

self::assertContains('0 13 * * *', $consoleOutput[6]);
self::assertContains('Closure', $consoleOutput[6]);
self::assertContains('A description for a scheduled callback', $consoleOutput[6]);

self::assertContains('0 14 * * *', $consoleOutput[7]);
self::assertContains('Closure', $consoleOutput[7]);
self::assertContains('TestJob', $consoleOutput[7]);
}

public function testListSchedulerCommand_withTasksAndCronStyle()
{
\Illuminate\Support\Facades\Artisan::call('schedule:list', ['--cron' => true]);
$consoleOutput = trim(\Illuminate\Support\Facades\Artisan::output());
$consoleOutput = explode("\n", trim(\Illuminate\Support\Facades\Artisan::output()));

self::assertContains('test:command:name', $consoleOutput[0]);
self::assertContains('artisan', $consoleOutput[0]);
self::assertContains('0 10 * * *', $consoleOutput[0]);
self::assertContains((DIRECTORY_SEPARATOR === '\\') ? 'NUL' : '/dev/null', $consoleOutput[0]);

self::assertContains('test:command:two', $consoleOutput[1]);

self::assertContains('test:command:name', $consoleOutput);
self::assertContains('artisan', $consoleOutput);
self::assertContains('0 10 * * *', $consoleOutput);
self::assertContains((DIRECTORY_SEPARATOR === '\\') ? 'NUL' : '/dev/null', $consoleOutput);
self::assertContains('ls -lah', $consoleOutput[2]);

self::assertContains('test:command:two', $consoleOutput);
self::assertContains('Closure', $consoleOutput[3]);

self::assertContains('ls -lah', $consoleOutput);
self::assertContains('Closure', $consoleOutput[4]);
}
}
6 changes: 6 additions & 0 deletions tests/Integration/MockConsoleKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ class MockConsoleKernel extends \Orchestra\Testbench\Console\Kernel

protected function schedule(\Illuminate\Console\Scheduling\Schedule $schedule)
{
$closure = function () {
echo 'callback or invokable class';
};

$schedule->command('test:command:name')->dailyAt('10:00')->description('Description of event');
$schedule->command('test:command:two')->dailyAt('10:00')->timezone('UTC');
$schedule->exec('ls -lah')->mondays()->at('3:00');
$schedule->call($closure)->dailyAt('13:00')->description('A description for a scheduled callback');
$schedule->job(new \TestJob())->dailyAt('14:00');
}
}
6 changes: 6 additions & 0 deletions tests/Integration/TestJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

class TestJob
{
//
}

0 comments on commit 9bd0c52

Please sign in to comment.