diff --git a/src/AsyncTask.php b/src/AsyncTask.php index 4b24ee5..a2abd6a 100644 --- a/src/AsyncTask.php +++ b/src/AsyncTask.php @@ -114,6 +114,21 @@ public function __construct(Closure|AsyncTaskInterface $theTask, string|null $ta $this->taskID = $taskID; } + /** + * Returns an instance of a fake AsyncTask with the same task parameters and task ID. + * @return FakeAsyncTask The fake AsyncTask object for testing. + */ + public function fake(): FakeAsyncTask + { + $fakeTask = new FakeAsyncTask($this->theTask, taskID: $this->taskID); + if ($this->getTimeLimit() === null) { + $fakeTask->withoutTimeLimit(); + } else { + $fakeTask->withTimeLimit($this->timeLimit); + } + return $fakeTask; + } + public function __serialize(): array { // serialize only the necessary info to reduce runner cmd length @@ -131,6 +146,18 @@ public function __unserialize($data): void ] = $data; } + /** + * Returns a status object for the started AsyncTask. + * + * If this task does not have an explicit task ID, a new one will be generated on-the-fly. + * @return AsyncTaskStatus The status object for the started AsyncTask. + */ + protected function getTaskStatusObject(): AsyncTaskStatus + { + $taskID = $this->taskID ?? Str::ulid()->toString(); + return new AsyncTaskStatus($taskID); + } + /** * Inside an available PHP process, runs this AsyncTask instance. * @@ -192,8 +219,7 @@ public function run(): void public function start(): AsyncTaskStatus { // prepare the task details - $taskID = $this->taskID ?? Str::ulid()->toString(); - $taskStatus = new AsyncTaskStatus($taskID); + $taskStatus = $this->getTaskStatusObject(); // prepare the runner command $serializedTask = $this->toBase64Serial(); diff --git a/src/AsyncTaskStatus.php b/src/AsyncTaskStatus.php index c1c6699..c1862e1 100644 --- a/src/AsyncTaskStatus.php +++ b/src/AsyncTaskStatus.php @@ -50,6 +50,15 @@ public function __construct( } } + /** + * Returns an instance of a fake status object with the same task ID. + * @return FakeAsyncTaskStatus The fake AsyncTaskStatus object for testing. + */ + public function fake(): FakeAsyncTaskStatus + { + return new FakeAsyncTaskStatus($this->taskID); + } + /** * Returns the task ID encoded in base64, mainly for result checking. * @return string The encoded task ID. diff --git a/src/FakeAsyncTask.php b/src/FakeAsyncTask.php new file mode 100644 index 0000000..090e91b --- /dev/null +++ b/src/FakeAsyncTask.php @@ -0,0 +1,44 @@ +getTaskStatusObject()->fake(); + } +} diff --git a/src/FakeAsyncTaskStatus.php b/src/FakeAsyncTaskStatus.php new file mode 100644 index 0000000..94e6b16 --- /dev/null +++ b/src/FakeAsyncTaskStatus.php @@ -0,0 +1,41 @@ +fakeIsRunning; + } + + /** + * Force the fake task to become stopped. + * + * Note: once stopped, the fake async task cannot be made running again. Use a new status object if the fake task needs to be restarted. + * @return void + */ + public function fakeStopRunning(): void + { + $this->fakeIsRunning = false; + } +} diff --git a/tests/FakeAsyncTaskTest.php b/tests/FakeAsyncTaskTest.php new file mode 100644 index 0000000..a95125e --- /dev/null +++ b/tests/FakeAsyncTaskTest.php @@ -0,0 +1,54 @@ +getStoragePath("testFakeTaskDoesNotRun.txt"); + $task = new AsyncTask(new DummyAsyncTask("Hello world!", $testFileName)); + $fakeTask = $task->fake(); + + // fake start it + $fakeTask->start(); + sleep(1); + // there should have no file outputs + $this->assertFileDoesNotExist($testFileName); + } + + public function testFakeTaskSameParams() + { + // the fake task should preserve its parameters + $testFileName = $this->getStoragePath("testFakeTaskSameDetails.txt"); + $task = new AsyncTask(new DummyAsyncTask("Hello world!", $testFileName)); + $randomDuration = rand(1, 10); + $task->withTimeLimit($randomDuration); + + // fake it... + $fakeTask = $task->fake(); + // ...and the parameters stay the same + $this->assertEquals($task->getTimeLimit(), $fakeTask->getTimeLimit()); + + // it is difficult to test the task ID since it would be exposing something that should not be exposed, so we will just have to believe it. + } + + public function testFakeTaskStatus() + { + $taskID = "testFakeTaskStatus"; + $taskStatus = new AsyncTaskStatus($taskID); + $fakeTaskStatusFromFake = $taskStatus->fake(); + $fakeTaskStatusFromNew = new FakeAsyncTaskStatus($taskID); + + // should have same task ID + $this->assertEquals($taskStatus->getEncodedTaskID(), $fakeTaskStatusFromFake->getEncodedTaskID()); + $this->assertEquals($taskStatus->getEncodedTaskID(), $fakeTaskStatusFromNew->getEncodedTaskID()); + } +}