Skip to content

Commit

Permalink
add documentation for how to use command arguments and options
Browse files Browse the repository at this point in the history
  • Loading branch information
LordSimal committed Jul 9, 2023
1 parent f2f7de2 commit ef51c1b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ which will add the `schedule(Scheduler &$scheduler)` method.
namespace App;

use App\Command\MyAppCommand;
use App\Command\OtherAppCommand;
use Cake\Http\BaseApplication;
use CakeScheduler\CakeSchedulerInterface;
use CakeScheduler\Scheduler\Scheduler;
Expand All @@ -56,6 +57,7 @@ class Application extends BaseApplication implements CakeSchedulerInterface
public function schedule(Scheduler &$scheduler): void
{
$scheduler->execute(MyAppCommand::class)->daily();
$scheduler->execute(OtherAppCommand::class, ['somearg', '--myoption=someoption'])->daily();
}
}
```
Expand Down
8 changes: 7 additions & 1 deletion src/Command/ScheduleViewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ public function execute(Arguments $args, ConsoleIo $io): int
}

$events->each(function (Event $event) use ($io): void {
$msg = sprintf('%s | %s', $event->getExpression(), get_class($event->getCommand()));
$expression = $event->getExpression();
$fqcn = get_class($event->getCommand());
$args = $event->getArgs();
if ($args) {
$fqcn .= ' [' . implode(' ', $args) . ']';
}
$msg = sprintf('%s | %s', $expression, $fqcn);
$io->info($msg);
});

Expand Down
8 changes: 8 additions & 0 deletions src/Scheduler/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,12 @@ public function getCommand(): CommandInterface
{
return $this->command;
}

/**
* @return array
*/
public function getArgs(): array
{
return $this->args;
}
}
29 changes: 26 additions & 3 deletions tests/TestCase/Command/ScheduleRunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public function testRunMultipleCommands(): void
$this->mockService(Scheduler::class, function () {
$schedulerMock = $this->getMockBuilder(Scheduler::class)->getMock();

$versionEvent = new Event(new TestAppCommand(), []);
$routesEvent = new Event(new TestPluginCommand(), []);
$collection = new Collection([$versionEvent, $routesEvent]);
$appEvent = new Event(new TestAppCommand(), []);
$pluginEvent = new Event(new TestPluginCommand(), []);
$collection = new Collection([$appEvent, $pluginEvent]);

$schedulerMock->expects($this->any())
->method('dueEvents')
Expand All @@ -89,4 +89,27 @@ public function testRunMultipleCommands(): void
$this->assertOutputContains('Executing [TestPlugin\\Command\\TestPluginCommand]');
$this->assertOutputContains('Test Plugin Command executed');
}

public function testRunSingleCommandWithArgsAndOptions(): void
{
$this->mockService(Scheduler::class, function () {
$schedulerMock = $this->getMockBuilder(Scheduler::class)->getMock();

$event = new Event(new TestAppCommand(), ['somearg', '--myoption=someoption']);
$collection = new Collection([$event]);

$schedulerMock->expects($this->any())
->method('dueEvents')
->willReturn($collection);

return $schedulerMock;
});
$this->exec('schedule:run');

$this->assertExitSuccess();
$this->assertOutputContains('Executing [TestApp\\Command\\TestAppCommand]');
$this->assertOutputContains('Test App Command executed');
$this->assertOutputContains('with arg somearg');
$this->assertOutputContains('with option someoption');
}
}
22 changes: 22 additions & 0 deletions tests/TestCase/Command/ScheduleViewCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Cake\Collection\Collection;
use Cake\Console\TestSuite\ConsoleIntegrationTestTrait;
use Cake\TestSuite\TestCase;
use CakeScheduler\Scheduler\Event;
use CakeScheduler\Scheduler\Scheduler;
use TestApp\Command\TestAppCommand;

class ScheduleViewCommandTest extends TestCase
{
Expand All @@ -33,6 +35,26 @@ public function testRunScheduleView(): void
$this->assertOutputContains('0 0 * * * | TestPlugin\Command\TestPluginCommand');
}

public function testRunScheduleViewWithEventsHavingArgsAndOptions(): void
{
$this->mockService(Scheduler::class, function () {
$schedulerMock = $this->getMockBuilder(Scheduler::class)->getMock();

$event = new Event(new TestAppCommand(), ['somearg', '--myoption=someoption']);
$collection = new Collection([$event]);

$schedulerMock->expects($this->any())
->method('allEvents')
->willReturn($collection);

return $schedulerMock;
});
$this->exec('schedule:view');

$this->assertExitSuccess();
$this->assertOutputContains('* * * * * | TestApp\Command\TestAppCommand [somearg --myoption=someoption]');
}

public function testRunScheduleViewNoEvents(): void
{
$this->mockService(Scheduler::class, function () {
Expand Down
20 changes: 20 additions & 0 deletions tests/test_app/src/Command/TestAppCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,31 @@
use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;

class TestAppCommand extends Command
{
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
$parser->addArgument('myarg', [
'help' => 'Some help text',
'required' => false
])
->addOption('myoption');

return $parser;
}

public function execute(Arguments $args, ConsoleIo $io)
{
$io->info('Test App Command executed');
$arg = $args->getArgument('myarg');
if ($arg) {
$io->info('with arg ' . $arg);
}
$option = $args->getOption('myoption');
if ($option) {
$io->info('with option ' . $option);
}
}
}

0 comments on commit ef51c1b

Please sign in to comment.