Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Group): Rename existing group if it has no tasks during new group creation #5

Merged
Merged
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
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
Loading