|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Vectorial1024\LaravelProcessAsync; |
| 6 | + |
| 7 | +use Closure; |
| 8 | +use Illuminate\Process\InvokedProcess; |
| 9 | +use Illuminate\Support\Facades\Process; |
| 10 | +use Illuminate\Support\Str; |
| 11 | +use InvalidArgumentException; |
| 12 | +use LogicException; |
| 13 | +use loophp\phposinfo\OsInfo; |
| 14 | +use RuntimeException; |
| 15 | + |
| 16 | +use function Opis\Closure\{serialize, unserialize}; |
| 17 | + |
| 18 | +/** |
| 19 | + * The fake AsyncTask class for testing. |
| 20 | + */ |
| 21 | +class FakeAsyncTask extends AsyncTask |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Creates a FakeAsyncTask instance. |
| 25 | + * |
| 26 | + * @param \Closure|AsyncTaskInterface $theTask The task to be executed in the background. |
| 27 | + * @param string|null $taskID (optional) The user-specified task ID of this AsyncTask. Should be unique. |
| 28 | + * @see AsyncTask::fake() an alternative way of creating FakeAsyncTask instances. |
| 29 | + */ |
| 30 | + public function __construct(Closure|AsyncTaskInterface $theTask, string|null $taskID = null) |
| 31 | + { |
| 32 | + parent::__construct($theTask, taskID: $taskID); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Dummy overriding method to prevent the FakeAsyncTask object from actually running the specified background task. |
| 37 | + * @return void |
| 38 | + */ |
| 39 | + public function run(): void |
| 40 | + { |
| 41 | + // don't do anything! |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Fakes the AsyncTask being started in the background, but does not actually start the task. |
| 47 | + * @return AsyncTaskStatus The status object for the fake-started FakeAsyncTask. |
| 48 | + */ |
| 49 | + public function start(): AsyncTaskStatus |
| 50 | + { |
| 51 | + // todo fake version |
| 52 | + $taskID = $this->taskID ?? Str::ulid()->toString(); |
| 53 | + return new AsyncTaskStatus($taskID); |
| 54 | + } |
| 55 | +} |
0 commit comments