Skip to content

Commit

Permalink
Added helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed May 2, 2023
1 parent 67fb237 commit 254b4b7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 11 additions & 1 deletion src/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
5 changes: 5 additions & 0 deletions tests/CustomTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class CustomTask extends Task

public $someData = 'foo';

public function __construct($someData = 'foo')
{
$this->someData = $someData;
}

public function someMethod()
{
return 'bar';
Expand Down
8 changes: 8 additions & 0 deletions tests/TaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 254b4b7

Please sign in to comment.