Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Progressable.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,28 @@ public function incrementStep(int $amount = 1): static {

return $this->setStep($current + $amount);
}

/**
* Get the current state as an array.
*
* @return array<string, mixed>
*/
public function toArray(): array {
$overallProgress = null;
$eta = null;
if (isset($this->overallUniqueName)) {
$overallProgress = $this->getOverallProgress();
$eta = $this->getEstimatedTimeRemaining();
}

return [
'progress' => $this->getLocalProgress(),
'overall_progress' => $overallProgress,
'total_steps' => $this->getTotalSteps(),
'current_step' => $this->getStep(),
'eta' => $eta,
'status_message' => $this->getStatusMessage(),
'metadata' => $this->getMetadata(),
];
}
}
39 changes: 39 additions & 0 deletions tests/ProgressableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,4 +553,43 @@ public function test_set_step_without_total_steps(): void {
$this->assertEquals(5, $this->getStep());
$this->assertEquals(0, $this->getLocalProgress());
}

public function test_to_array(): void {
$this->setOverallUniqueName('test_to_array_'.$this->testId);
$this->setTotalSteps(10);
$this->setStep(5);
$this->setStatusMessage('Processing item 5');
$this->setMetadata(['foo' => 'bar']);

$array = $this->toArray();

$this->assertIsArray($array);
$this->assertArrayHasKey('progress', $array);
$this->assertEquals(50, $array['progress']);
$this->assertArrayHasKey('overall_progress', $array);
$this->assertEquals(50, $array['overall_progress']);
$this->assertArrayHasKey('total_steps', $array);
$this->assertEquals(10, $array['total_steps']);
$this->assertArrayHasKey('current_step', $array);
$this->assertEquals(5, $array['current_step']);
$this->assertArrayHasKey('eta', $array);
// ETA depends on start time and elapsed time, which might be null if 0 time elapsed
$this->assertArrayHasKey('status_message', $array);
$this->assertEquals('Processing item 5', $array['status_message']);
$this->assertArrayHasKey('metadata', $array);
$this->assertEquals(['foo' => 'bar'], $array['metadata']);
}

public function test_to_array_without_unique_name(): void {
// Just checking toArray when no unique name is set
$array = $this->toArray();

$this->assertIsArray($array);
$this->assertEquals(0, $array['progress']);
$this->assertNull($array['overall_progress']);
$this->assertNull($array['total_steps']);
$this->assertNull($array['current_step']);
$this->assertNull($array['status_message']);
$this->assertEquals([], $array['metadata']);
}
}