Repository navigation
Expand file tree
/
Copy pathResponseTaggerTest.php
More file actions
168 lines (140 loc) · 5.86 KB
/
Copy pathResponseTaggerTest.php
File metadata and controls
168 lines (140 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/*
* This file is part of the FOSHttpCache package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\HttpCache\Tests\Unit;
use FOS\HttpCache\Exception\InvalidTagException;
use FOS\HttpCache\ResponseTagger;
use FOS\HttpCache\TagHeaderFormatter\CommaSeparatedTagHeaderFormatter;
use FOS\HttpCache\TagHeaderFormatter\TagHeaderFormatter;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
class ResponseTaggerTest extends TestCase
{
use MockeryPHPUnitIntegration;
public function testDefaultFormatter(): void
{
$tagger = new ResponseTagger();
$this->assertEquals('X-Cache-Tags', $tagger->getTagsHeaderName());
}
public function testGetTagsHeaderValue(): void
{
$headerFormatter = \Mockery::mock(TagHeaderFormatter::class)
->shouldReceive('getTagsHeaderValue')
->with(['post-1', 'test,post'])
->once()
->andReturn('post-1,test_post')
->getMock();
$tagger = new ResponseTagger(['header_formatter' => $headerFormatter]);
$this->assertFalse($tagger->hasTags());
$tagger->addTags(['post-1', 'test,post']);
$this->assertTrue($tagger->hasTags());
$this->assertEquals('post-1,test_post', $tagger->getTagsHeaderValue());
$this->assertTrue($tagger->hasTags()); // getting the header does not clear the tags
}
public function testTagResponseReplace(): void
{
$headerFormatter = \Mockery::mock(TagHeaderFormatter::class)
->shouldReceive('getTagsHeaderValue')
->with(['tag-1', 'tag-2'])
->once()
->andReturn('tag-1,tag-2')
// https://github.com/phpstan/phpstan-mockery/issues/8
/* @phpstan-ignore-next-line */
->shouldReceive('getTagsHeaderName')
->once()
->andReturn('FOS-Tags')
->getMock();
$tagger = new ResponseTagger(['header_formatter' => $headerFormatter]);
$response = \Mockery::mock(ResponseInterface::class)
->shouldReceive('withHeader')
->with('FOS-Tags', 'tag-1,tag-2')
->andReturn($this->createMock(ResponseInterface::class))
->getMock();
$tagger->addTags(['tag-1', 'tag-2']);
$tagger->tagResponse($response, true);
$this->assertFalse($tagger->hasTags());
}
public function testTagResponseAdd(): void
{
$headerFormatter = \Mockery::mock(TagHeaderFormatter::class)
->shouldReceive('getTagsHeaderValue')
->with(['tag-1', 'tag-2'])
->once()
->andReturn('tag-1,tag-2')
// https://github.com/phpstan/phpstan-mockery/issues/8
/* @phpstan-ignore-next-line */
->shouldReceive('getTagsHeaderName')
->once()
->andReturn('FOS-Tags')
->getMock();
$tagger = new ResponseTagger(['header_formatter' => $headerFormatter]);
$response = \Mockery::mock(ResponseInterface::class)
->shouldReceive('withAddedHeader')
->with('FOS-Tags', 'tag-1,tag-2')
->andReturn($this->createMock(ResponseInterface::class))
->getMock();
$tagger->addTags(['tag-1', 'tag-2']);
$tagger->tagResponse($response);
$this->assertFalse($tagger->hasTags());
}
public function testTagResponseNoTags(): void
{
/** @var TagHeaderFormatter $headerFormatter */
$headerFormatter = \Mockery::mock(TagHeaderFormatter::class)
->shouldReceive('getTagsHeaderValue')->never()
->getMock();
$tagger = new ResponseTagger(['header_formatter' => $headerFormatter]);
$response = \Mockery::mock(ResponseInterface::class)
->shouldReceive('withHeader')->never()
// https://github.com/phpstan/phpstan-mockery/issues/8
/* @phpstan-ignore-next-line */
->shouldReceive('withAddedHeader')->never()
->getMock();
$tagger->tagResponse($response, true);
}
public function testStrictEmptyTag(): void
{
$headerFormatter = new CommaSeparatedTagHeaderFormatter('FOS-Tags');
$tagHandler = new ResponseTagger(['header_formatter' => $headerFormatter, 'strict' => true]);
$this->expectException(InvalidTagException::class);
$tagHandler->addTags(['post-1', false]);
}
public function testNonStrictEmptyTag(): void
{
$headerFormatter = \Mockery::mock(TagHeaderFormatter::class)
->shouldReceive('getTagsHeaderValue')
->with(['post-1'])
->once()
->andReturn('post-1')
->getMock();
$tagHandler = new ResponseTagger(['header_formatter' => $headerFormatter]);
$tagHandler->addTags(['post-1', false, null, '']);
$this->assertTrue($tagHandler->hasTags());
$this->assertEquals('post-1', $tagHandler->getTagsHeaderValue());
}
public function testUniqueTags(): void
{
$headerFormatter = new CommaSeparatedTagHeaderFormatter('FOS-Tags');
$tagHandler = new ResponseTagger(['header_formatter' => $headerFormatter, 'strict' => true]);
$tagHandler->addTags(['post-1', 'post-2', 'post-1']);
$tagHandler->addTags(['post-2', 'post-3']);
$this->assertTrue($tagHandler->hasTags());
$this->assertEquals('post-1,post-2,post-3', $tagHandler->getTagsHeaderValue());
}
public function testClear(): void
{
$tagger = new ResponseTagger();
$this->assertFalse($tagger->hasTags());
$tagger->addTags(['foo', 'bar']);
$this->assertTrue($tagger->hasTags());
$tagger->clear();
$this->assertFalse($tagger->hasTags());
}
}