Skip to content

Commit

Permalink
Merge branch '1.next' of github.com:LordSimal/cakephp-scheduler into …
Browse files Browse the repository at this point in the history
…1.next
  • Loading branch information
LordSimal committed Sep 15, 2024
2 parents 2a7eb17 + eaa8ae4 commit c83faa5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
47 changes: 45 additions & 2 deletions src/Scheduler/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

use Cake\Collection\Collection;
use Cake\Collection\CollectionInterface;
use Cake\Command\Command;
use Cake\Console\Arguments;
use Cake\Console\CommandInterface;
use Cake\Console\ConsoleIo;
use Cake\Core\Container;
use Cake\Core\ContainerInterface;
use Cake\Event\EventDispatcherInterface;
Expand Down Expand Up @@ -48,12 +51,16 @@ public function __construct(?Container $container = null)
}

/**
* @param string $command The FQCN of the command to be executed
* @param callable|string $command The FQCN of the command to be executed or a callable function
* @param array $args Args which should be passed on to the command
* @return \CakeScheduler\Scheduler\Event
*/
public function execute(string $command, array $args = []): Event
public function execute(string|callable $command, array $args = []): Event
{
if (is_callable($command)) {
return $this->addCallable($command, $args);
}

try {
$commandObj = $this->container->get($command);
} catch (ContainerExceptionInterface | NotFoundExceptionInterface $ex) {
Expand Down Expand Up @@ -82,6 +89,42 @@ protected function addCommand(CommandInterface $command, array $args = []): Even
return $event;
}

/**
* @param callable $callable
* @param array $args
* @return \CakeScheduler\Scheduler\Event
*/
protected function addCallable(callable $callable, array $args = []): Event
{
$command = new class ($callable, $args) extends Command {
/**
* @param callable $callable
* @param array $args
*/
public function __construct(
protected $callable,
protected array $args = []
) {
}

/**
* @param \Cake\Console\Arguments $args
* @param \Cake\Console\ConsoleIo $io
* @return int|null
*/
public function execute(Arguments $args, ConsoleIo $io): ?int
{
array_push($this->args, $io);

return call_user_func_array($this->callable, $this->args);
}
};
$event = new Event($command, $args);
$this->events = $this->events->appendItem($event);

return $event;
}

/**
* @return \Cake\Collection\CollectionInterface
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/Command/ScheduleRunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,24 @@ public function execute(Arguments $args, ConsoleIo $io): void
$this->assertOutputNotContains('Executing [TestPlugin\\Command\\TestPluginCommand]');
$this->assertOutputNotContains('Test Plugin Command executed');
}

public function testRunSingleCallable(): void
{
$this->mockService(Scheduler::class, function () {
$scheduler = new Scheduler(new Container());
$scheduler->execute(function ($a, $b, $c, ConsoleIo $io) {
$io->info('Sum');

return $a + $b + $c;
}, [1,2,3]);

return $scheduler;
});

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

$this->assertExitSuccess();
$this->assertOutputContains('Executing [Cake\\Command\\Command@anonymous');
$this->assertOutputContains('<info>Sum</info>');
}
}
12 changes: 12 additions & 0 deletions tests/TestCase/Scheduler/SchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cake\Chronos\Chronos;
use Cake\Command\VersionCommand;
use Cake\Console\Command\HelpCommand;
use Cake\Console\ConsoleIo;
use Cake\Core\Container;
use Cake\TestSuite\TestCase;
use CakeScheduler\Scheduler\Scheduler;
Expand Down Expand Up @@ -46,6 +47,17 @@ public function testAddMultipleDueEvents(): void
Chronos::setTestNow('now');
}

public function testAddCallable(): void
{
$this->scheduler->execute(function ($a, $b, $c, ConsoleIo $io) {
$io->info('Sum');

return $a + $b + $c;
}, [1,2,3]);
$events = $this->scheduler->dueEvents();
$this->assertNotEmpty($events);
}

public function testAddUnknownCommand(): void
{
$this->expectException(InvalidArgumentException::class);
Expand Down

0 comments on commit c83faa5

Please sign in to comment.