Skip to content

Commit c8d5a79

Browse files
committed
Create the FakeAsyncTask class
1 parent d6db4aa commit c8d5a79

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/AsyncTask.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AsyncTask
3232
* If null, the task will generate an unsaved random ID when it is started.
3333
* @var string|null
3434
*/
35-
private string|null $taskID;
35+
protected string|null $taskID;
3636

3737
/**
3838
* The process that is actually running this task. Tasks that are not started will have null here.

src/FakeAsyncTask.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)