Skip to content

Commit 45953e9

Browse files
committed
Allow converting real objects to fake objects
1 parent 3af4ca2 commit 45953e9

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/AsyncTask.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ public function __construct(Closure|AsyncTaskInterface $theTask, string|null $ta
114114
$this->taskID = $taskID;
115115
}
116116

117+
/**
118+
* Returns an instance of a fake AsyncTask with the same task parameters and task ID.
119+
* @return FakeAsyncTask The fake AsyncTask object for testing.
120+
*/
121+
public function fake(): FakeAsyncTask
122+
{
123+
$fakeTask = new FakeAsyncTask($this->theTask, taskID: $this->taskID);
124+
if ($this->getTimeLimit() === null) {
125+
$fakeTask->withoutTimeLimit();
126+
} else {
127+
$fakeTask->withTimeLimit($this->timeLimit);
128+
}
129+
return $fakeTask;
130+
}
131+
117132
public function __serialize(): array
118133
{
119134
// serialize only the necessary info to reduce runner cmd length

src/AsyncTaskStatus.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ public function __construct(
5050
}
5151
}
5252

53+
/**
54+
* Returns an instance of a fake status object with the same task ID.
55+
* @return FakeAsyncTaskStatus The fake AsyncTaskStatus object for testing.
56+
*/
57+
public function fake(): FakeAsyncTaskStatus
58+
{
59+
return new FakeAsyncTaskStatus($this->taskID);
60+
}
61+
5362
/**
5463
* Returns the task ID encoded in base64, mainly for result checking.
5564
* @return string The encoded task ID.

src/FakeAsyncTask.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,10 @@ public function run(): void
4444

4545
/**
4646
* 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.
47+
* @return FakeAsyncTaskStatus The status object for the fake-started FakeAsyncTask.
4848
*/
49-
public function start(): AsyncTaskStatus
49+
public function start(): FakeAsyncTaskStatus
5050
{
51-
// todo fake version
52-
return $this->getTaskStatusObject();
51+
return $this->getTaskStatusObject()->fake();
5352
}
5453
}

0 commit comments

Comments
 (0)