Skip to content

Commit

Permalink
feat: Add support for custom task ids (#81)
Browse files Browse the repository at this point in the history
* Add support for custom task ids

* Update tests

* Apply suggestions from code review

---------

Co-authored-by: Kevin Bond <[email protected]>
  • Loading branch information
bobvandevijver and kbond authored Jun 5, 2023
1 parent 440710e commit 8857dd3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/Command/ScheduleListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,14 @@ private function getScheduleIssues(Schedule $schedule): iterable

$task = $taskGroup[0];

yield new \LogicException(\sprintf('Task "%s" (%s) is duplicated %d times. Make their descriptions unique to fix.', $task, $task->getExpression(), $count));
$description = \sprintf('Task "%s" (%s) is duplicated %d times. ', $task, $task->getExpression(), $count);
if ($task->hasCustomIdentifier()) {
$description .= 'Make sure to use unique identifiers.';
} else {
$description .= 'Make their descriptions unique to fix, or set a custom identifier with `identifiedBy`.';
}

yield new \LogicException($description);
}
}

Expand Down
22 changes: 21 additions & 1 deletion src/Schedule/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ abstract class Task

private string $expression = self::DEFAULT_EXPRESSION;
private ?\DateTimeZone $timezone = null;
private ?string $customId = null;

public function __construct(private string $description)
{
}

final public function __toString(): string
{
if ($this->customId) {
return "{$this->getType()} `$this->customId`: {$this->getDescription()}";
}

return "{$this->getType()}: {$this->getDescription()}";
}

Expand All @@ -57,7 +62,12 @@ public function getType(): string

final public function getId(): string
{
return \sha1(static::class.$this->getExpression().$this->getDescription());
return $this->customId ?? \sha1(static::class.$this->getExpression().$this->getDescription());
}

final public function hasCustomIdentifier(): bool
{
return $this->customId !== null;
}

final public function getDescription(): string
Expand Down Expand Up @@ -93,6 +103,16 @@ final public function isDue(\DateTimeInterface $timestamp): bool
return $this->getExpression()->isDue($timestamp, $this->getTimezoneValue());
}

/**
* Set a unique identifier for this task.
*/
final public function identifiedBy(string $id): self
{
$this->customId = $id;

return $this;
}

/**
* Set a unique description for this task.
*/
Expand Down
9 changes: 6 additions & 3 deletions tests/Command/ScheduleListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ public function shows_schedule_issue_for_duplicate_task_id()
->addTask(new MockTask('task3'))
->addTask(new MockTask('task3'))
->addTask(new MockTask('task3'))
->addTask((new MockTask('task4'))->identifiedBy('task4_id'))
->addTask((new MockTask('task4'))->identifiedBy('task4_id'))
->getRunner()
;

Expand All @@ -335,9 +337,10 @@ public function shows_schedule_issue_for_duplicate_task_id()
$output = $this->normalizeOutput($commandTester);

$this->assertSame(1, $commandTester->getStatusCode());
$this->assertStringContainsString('[WARNING] 2 issues with schedule:', $output);
$this->assertStringContainsString('[ERROR] Task "MockTask: task2" (* * * * *) is duplicated 2 times. Make their descriptions unique to fix.', $output);
$this->assertStringContainsString('[ERROR] Task "MockTask: task3" (* * * * *) is duplicated 3 times. Make their descriptions unique to fix.', $output);
$this->assertStringContainsString('[WARNING] 3 issues with schedule:', $output);
$this->assertStringContainsString('[ERROR] Task "MockTask: task2" (* * * * *) is duplicated 2 times.', $output);
$this->assertStringContainsString('[ERROR] Task "MockTask: task3" (* * * * *) is duplicated 3 times.', $output);
$this->assertStringContainsString('[ERROR] Task "MockTask `task4_id`: task4" (* * * * *) is duplicated 2 times.', $output);
}

private function normalizeOutput(CommandTester $tester): string
Expand Down
9 changes: 9 additions & 0 deletions tests/Schedule/TaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ public function has_unique_id_based_on_description_and_frequency()
})->getId(), self::task('task')->getId());
}

/**
* @test
*/
public function uses_custom_id_when_set()
{
$this->assertSame('task1', self::task()->identifiedBy('task1')->getId());
$this->assertNotSame(self::task()->identifiedBy('task1')->getId(), self::task()->getId());
}

/**
* @test
*
Expand Down

0 comments on commit 8857dd3

Please sign in to comment.