Skip to content

Commit

Permalink
Extract public getTagsInGroup helper
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Jan 3, 2023
1 parent 84dd0bb commit 82dc5df
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public function getTags(): Collection
return $this->tags;
}

/** @return array<string> */
public function getTagsInGroup(string $name): array
{
return $this->tags->get($name) ?? [];
}

/**
* @param array<string>|string $values
* @return $this
Expand Down Expand Up @@ -65,7 +71,7 @@ public function addTagGroups(array $tags): self
*/
public function addTagsToGroup(string $name, array|string $values): self
{
$this->tags->put($name, array_merge($this->tags->get($name, []), (array) $values));
$this->tags->put($name, array_merge($this->getTagsInGroup($name), (array) $values));

return $this;
}
Expand Down
24 changes: 24 additions & 0 deletions packages/framework/tests/Feature/PublicationTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ public function testGetTags()
$this->assertEquals(new Collection(['foo' => ['bar', 'baz']]), (new PublicationTags())->getTags());
}

public function testGetTagsInGroup()
{
$this->file('tags.json', json_encode(['foo' => ['bar', 'baz']]));

$this->assertEquals(['bar', 'baz'], (new PublicationTags())->getTagsInGroup('foo'));
}

public function testGetTagsInGroupOnlyReturnTagsForTheSpecifiedGroup()
{
$this->file('tags.json', json_encode([
'foo' => ['bar', 'baz'],
'bar' => ['foo', 'baz'],
]));

$this->assertEquals(['foo', 'baz'], (new PublicationTags())->getTagsInGroup('bar'));
}

public function testGetTagsInGroupReturnsEmptyArrayWhenGroupDoesNotExist()
{
$this->file('tags.json', json_encode(['foo' => ['bar', 'baz']]));

$this->assertEquals([], (new PublicationTags())->getTagsInGroup('bar'));
}

public function testCanAddTagGroup()
{
$tags = new PublicationTags();
Expand Down

0 comments on commit 82dc5df

Please sign in to comment.