Skip to content

Commit 4959b5d

Browse files
Toflardbu
authored andcommitted
Do not add the same tag multiple times (#423)
Do not add the same tag multiple times
1 parent 4c5e60c commit 4959b5d

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ See also the [GitHub releases page](https://github.com/FriendsOfSymfony/FOSHttpC
1010

1111
* Added: `CleanupCacheTagsListener` to remove the cache tags header from the final
1212
response that is sent to the client. Add this listener to your cache kernel.
13+
14+
### Tagging
15+
16+
* Improved: The `ResponseTagger` does now remove duplicate tags.
1317

1418
2.3.1
1519
-----

src/ResponseTagger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function addTags(array $tags)
123123
throw new InvalidTagException('Empty tags are not allowed');
124124
}
125125

126-
$this->tags = array_merge($this->tags, $filtered);
126+
$this->tags = array_unique(array_merge($this->tags, $filtered));
127127

128128
return $this;
129129
}

tests/Unit/ResponseTaggerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,16 @@ public function testNonStrictEmptyTag()
132132
$this->assertTrue($tagHandler->hasTags());
133133
$this->assertEquals('post-1', $tagHandler->getTagsHeaderValue());
134134
}
135+
136+
public function testUniqueTags()
137+
{
138+
$headerFormatter = new CommaSeparatedTagHeaderFormatter('FOS-Tags');
139+
140+
$tagHandler = new ResponseTagger(['header_formatter' => $headerFormatter, 'strict' => true]);
141+
$tagHandler->addTags(['post-1', 'post-2', 'post-1']);
142+
$tagHandler->addTags(['post-2', 'post-3']);
143+
$this->assertTrue($tagHandler->hasTags());
144+
145+
$this->assertEquals('post-1,post-2,post-3', $tagHandler->getTagsHeaderValue());
146+
}
135147
}

0 commit comments

Comments
 (0)