Skip to content

Commit

Permalink
Merge pull request #5 from sivabalan02/feature/reuse-empty-group-if-n…
Browse files Browse the repository at this point in the history
…o-tasks

feat(Group): Rename existing group if it has no tasks during new group creation
  • Loading branch information
tomloprod authored Jun 20, 2024
2 parents abf731f + f37021d commit 4fca4c6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/Services/TimeWardenManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ public function group(string $groupName): self
{
$this->stop();

/** @todo do the same as task(). overwrite empty groups, avoid groups with same name (in this case... active group with name?) */
$this->groups[] = new Group($groupName);
$group = $this->getLastGroup();
if ($group && ! $group->getLastTask() instanceof Task) {
$group->name = $groupName;
} else {
$this->groups[] = new Group($groupName);
}

return self::$instance;
}
Expand Down
20 changes: 17 additions & 3 deletions tests/Services/TimeWardenManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,34 @@
it('can create and retrieve groups', function (): void {
$instance = TimeWardenManager::instance();

$instance->group('Group1');
$instance->group('Group2');
$instance->group('Group1')->task('foo');
$instance->group('Group2')->task('bar');

$groups = $instance->getGroups();

expect($groups)->toHaveCount(2);

expect($groups[0])->toBeInstanceOf(Group::class);
expect($groups[1])->toBeInstanceOf(Group::class);

expect($groups[0]->name)->toBe('Group1');

expect($groups[1]->name)->toBe('Group2');
});

it('overwrite last group if doesn\'t have tasks when a new group is created', function (): void {
$instance = TimeWardenManager::instance();

$instance->group('Group1');
$instance->group('Group2');
$instance->group('Group3');

$groups = $instance->getGroups();

expect($groups)->toHaveCount(1);
expect($groups[0]->name)->toBe('Group3');
expect($groups[0])->toBeInstanceOf(Group::class);
});

it('can create tasks of timewarden instance', function (): void {
$instance = TimeWardenManager::instance();

Expand Down

0 comments on commit 4fca4c6

Please sign in to comment.