From 254b4b7722536de08700f4b1ba2e03d991eb9101 Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Tue, 2 May 2023 10:09:15 +0200 Subject: [PATCH] Added helper method --- README.md | 8 ++++++++ src/Task.php | 12 +++++++++++- tests/CustomTask.php | 5 +++++ tests/TaskTest.php | 8 ++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4d64337..2171b9d 100644 --- a/README.md +++ b/README.md @@ -104,10 +104,18 @@ class GetFile extends Task } ``` +Blade template: + ```blade cat {{ $options() }} {{ $path }} ``` +You can create a new instance of the Task using the static `make()` method: + +```php +GetFile::make('/etc/hosts')->dispatch(); +``` + ## Task options You may specify a timeout. By default, the timeout is based on the `task-runner.default_timeout` config value. diff --git a/src/Task.php b/src/Task.php index ba816b5..2807a28 100644 --- a/src/Task.php +++ b/src/Task.php @@ -156,11 +156,21 @@ public function pending(): PendingTask return new PendingTask($this); } + /** + * Returns a new PendingTask with this task. + * + * @return \ProtoneMedia\LaravelTaskRunner\PendingTask + */ + public static function make(...$arguments): PendingTask + { + return (new static(...$arguments))->pending(); + } + /** * Helper methods to create a new PendingTask. */ public static function __callStatic($name, $arguments) { - return app(static::class)->pending()->{$name}(...$arguments); + return (new static)->pending()->{$name}(...$arguments); } } diff --git a/tests/CustomTask.php b/tests/CustomTask.php index 758a1f5..8573d5c 100644 --- a/tests/CustomTask.php +++ b/tests/CustomTask.php @@ -14,6 +14,11 @@ class CustomTask extends Task public $someData = 'foo'; + public function __construct($someData = 'foo') + { + $this->someData = $someData; + } + public function someMethod() { return 'bar'; diff --git a/tests/TaskTest.php b/tests/TaskTest.php index 92b79e8..8058979 100644 --- a/tests/TaskTest.php +++ b/tests/TaskTest.php @@ -5,6 +5,7 @@ use Illuminate\Support\Facades\View; use Mockery; use ProtoneMedia\LaravelTaskRunner\Connection; +use ProtoneMedia\LaravelTaskRunner\PendingTask; it('can generate defaults based on the class name and configuration', function () { $task = new DemoTask; @@ -60,6 +61,13 @@ expect($task->getScript())->toBe('baz foo bar'); }); +it('has a helper method to create the task fluently', function () { + $task = CustomTask::make('otherData'); + + expect($task)->toBeInstanceOf(PendingTask::class); + expect($task->task->someData)->toBe('otherData'); +}); + it('can create a pending task with a static method', function () { $pendingTask = DemoTask::inBackground(); expect($pendingTask->task)->toBeInstanceOf(DemoTask::class);