Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions spec/Scheduler.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion src/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@
*/
protected function runClosure(): mixed
{
return $this->getAction()->__invoke();
return service('container')->call($this->getAction());

Check failure on line 234 in src/Task.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Parameter #1 $name of function service expects class-string<container>, string given.

Check failure on line 234 in src/Task.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Parameter #1 $name of function service expects class-string<container>, string given.
}

/**
Expand Down
Loading