diff --git a/spec/Scheduler.spec.php b/spec/Scheduler.spec.php index 2fca294..8dd0f8b 100644 --- a/spec/Scheduler.spec.php +++ b/spec/Scheduler.spec.php @@ -9,6 +9,7 @@ * the LICENSE file that was distributed with this source code. */ +use BlitzPHP\Container\Container; use BlitzPHP\Tasks\Scheduler; use BlitzPHP\Tasks\Task; @@ -30,6 +31,54 @@ expect('Hello')->toBe($task->getAction()()); }); + it('Peut sauvegarder un callable', function () { + $class = new class () { + public function execute() + { + return 'Hello'; + } + }; + + $task = $this->scheduler->call([$class, 'execute']); + + expect($task)->toBeAnInstanceOf(Task::class); + expect('Hello')->toBe($task->getAction()()); + }); + + it('Peut sauvegarder une classe invokable', function () { + $class = new class () { + public function __invoke() + { + return 'Hello'; + } + }; + + $task = $this->scheduler->call($class); + + expect($task)->toBeAnInstanceOf(Task::class); + expect('Hello')->toBe($task->getAction()()); + }); + + it("Peut faire de l'injection de dependance dans un callable", function () { + $class = new class () { + public function __invoke(Container $container) + { + $container->set('foo', 'bar'); + + return $container; + } + }; + + $task = $this->scheduler->call($class); + + expect($task)->toBeAnInstanceOf(Task::class); + + $container = $task->run(); + + expect($container)->toBeAnInstanceOf(Container::class); + expect($container->get('foo'))->toBe('bar'); + }); + it('Peut sauvegarder une commande klinge', function () { $task = $this->scheduler->command('foo:bar'); diff --git a/src/Scheduler.php b/src/Scheduler.php index 177341f..044888c 100644 --- a/src/Scheduler.php +++ b/src/Scheduler.php @@ -38,7 +38,7 @@ public function getTasks(): array /** * Planifie l'execution d'une closure. */ - public function call(Closure $func): Task + public function call(callable $func): Task { return $this->createTask('closure', $func); } diff --git a/src/Task.php b/src/Task.php index dc5cf5d..7f64960 100644 --- a/src/Task.php +++ b/src/Task.php @@ -231,7 +231,7 @@ protected function runShell(): array */ protected function runClosure(): mixed { - return $this->getAction()->__invoke(); + return service('container')->call($this->getAction()); } /**