1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCache package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\HttpCache\Test; |
13
|
|
|
|
14
|
|
|
use FOS\HttpCache\ProxyClient\HttpDispatcher; |
15
|
|
|
use FOS\HttpCache\ProxyClient\Varnish; |
16
|
|
|
use FOS\HttpCache\Test\Proxy\VarnishProxy; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Starts and clears the Varnish proxy between tests. |
20
|
|
|
* |
21
|
|
|
* You can define constants in your phpunit to control how this test behaves. |
22
|
|
|
* |
23
|
|
|
* Note that the WEB_SERVER_HOSTNAME must also match with what you have in your |
24
|
|
|
* .vcl file. |
25
|
|
|
* |
26
|
|
|
* To define constants in the phpunit file, use this syntax: |
27
|
|
|
* <php> |
28
|
|
|
* <const name="VARNISH_FILE" value="./tests/Functional/Fixtures/varnish-3/fos.vcl" /> |
29
|
|
|
* </php> |
30
|
|
|
* |
31
|
|
|
* VARNISH_FILE Varnish configuration file (required if not passed to setUp) |
32
|
|
|
* VARNISH_BINARY executable for Varnish. this can also be the full path |
33
|
|
|
* to the file if the binary is not automatically found |
34
|
|
|
* (default varnishd) |
35
|
|
|
* VARNISH_PORT port Varnish listens on (default 6181) |
36
|
|
|
* VARNISH_MGMT_PORT Varnish management port (default 6182) |
37
|
|
|
* VARNISH_CACHE_DIR directory to use for cache |
38
|
|
|
* (default sys_get_temp_dir() + '/foshttpcache-varnish') |
39
|
|
|
* WEB_SERVER_HOSTNAME hostname/IP your application can be reached at (required) |
40
|
|
|
* |
41
|
|
|
* If you want to test with Varnish 3, you need to define an environment |
42
|
|
|
* variable with the varnish version: |
43
|
|
|
* <php> |
44
|
|
|
* <env name="VARNISH_VERSION" value="3" /> |
45
|
|
|
* </php> |
46
|
|
|
*/ |
47
|
|
|
trait VarnishTest |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @var VarnishProxy |
51
|
|
|
*/ |
52
|
|
|
protected $proxy; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Varnish |
56
|
|
|
*/ |
57
|
|
|
protected $proxyClient; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Start Varnish and discard any cached content. |
61
|
|
|
*/ |
62
|
23 |
|
protected function setUp(): void |
63
|
|
|
{ |
64
|
23 |
|
$this->getProxy()->clear(); |
65
|
23 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Stop Varnish. |
69
|
|
|
*/ |
70
|
24 |
|
protected function tearDown(): void |
71
|
|
|
{ |
72
|
24 |
|
$this->getProxy()->stop(); |
73
|
24 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* The default implementation looks at the constant VARNISH_FILE. |
77
|
|
|
* |
78
|
|
|
* @throws \Exception |
79
|
|
|
* |
80
|
|
|
* @return string the path to the varnish server configuration file to use with this test |
81
|
|
|
*/ |
82
|
11 |
|
protected function getConfigFile() |
83
|
|
|
{ |
84
|
|
|
// @codeCoverageIgnoreStart |
85
|
|
|
if (!defined('VARNISH_FILE')) { |
86
|
|
|
throw new \Exception( |
87
|
|
|
'Specify the varnish configuration file path in phpunit.xml or override getConfigFile()' |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
// @codeCoverageIgnoreEnd |
91
|
|
|
|
92
|
11 |
|
return VARNISH_FILE; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get Varnish binary. |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
23 |
|
protected function getBinary() |
101
|
|
|
{ |
102
|
23 |
|
return defined('VARNISH_BINARY') ? VARNISH_BINARY : null; |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get Varnish port. |
107
|
|
|
* |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
23 |
|
protected function getCachingProxyPort() |
111
|
|
|
{ |
112
|
23 |
|
return defined('VARNISH_PORT') ? VARNISH_PORT : 6181; |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get Varnish management port. |
117
|
|
|
* |
118
|
|
|
* @return int |
119
|
|
|
*/ |
120
|
23 |
|
protected function getVarnishMgmtPort() |
121
|
|
|
{ |
122
|
23 |
|
return defined('VARNISH_MGMT_PORT') ? VARNISH_MGMT_PORT : null; |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get Varnish cache directory. |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
23 |
|
protected function getCacheDir() |
131
|
|
|
{ |
132
|
23 |
|
return defined('VARNISH_CACHE_DIR') ? VARNISH_CACHE_DIR : null; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Defaults to 4. |
137
|
|
|
* |
138
|
|
|
* @return int |
139
|
|
|
*/ |
140
|
13 |
|
protected function getVarnishVersion() |
141
|
|
|
{ |
142
|
13 |
|
return getenv('VARNISH_VERSION') ?: '4.0'; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
24 |
|
protected function getProxy() |
149
|
|
|
{ |
150
|
24 |
|
if (null === $this->proxy) { |
151
|
24 |
|
$this->proxy = new VarnishProxy($this->getConfigFile()); |
152
|
24 |
|
if ($this->getBinary()) { |
153
|
1 |
|
$this->proxy->setBinary($this->getBinary()); |
154
|
|
|
} |
155
|
|
|
|
156
|
24 |
|
if ($this->getCachingProxyPort()) { |
157
|
24 |
|
$this->proxy->setPort($this->getCachingProxyPort()); |
158
|
|
|
} |
159
|
|
|
|
160
|
24 |
|
if ($this->getVarnishMgmtPort()) { |
161
|
1 |
|
$this->proxy->setManagementPort($this->getVarnishMgmtPort()); |
162
|
|
|
} |
163
|
|
|
|
164
|
24 |
|
if ($this->getCacheDir()) { |
165
|
1 |
|
$this->proxy->setCacheDir($this->getCacheDir()); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
24 |
|
return $this->proxy; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get Varnish proxy client. |
174
|
|
|
* |
175
|
|
|
* @return Varnish |
176
|
|
|
*/ |
177
|
10 |
|
protected function getProxyClient() |
178
|
|
|
{ |
179
|
10 |
|
if (null === $this->proxyClient) { |
180
|
10 |
|
$httpDispatcher = new HttpDispatcher( |
181
|
10 |
|
['http://127.0.0.1:'.$this->getCachingProxyPort()], |
182
|
10 |
|
$this->getHostName().':'.$this->getCachingProxyPort() |
183
|
|
|
); |
184
|
|
|
|
185
|
10 |
|
if (getenv('VARNISH_MODULES_VERSION')) { |
186
|
|
|
$config = [ |
187
|
10 |
|
'tags_header' => 'xkey-purge', |
188
|
|
|
'tag_mode' => 'purgekeys', |
189
|
|
|
]; |
190
|
|
|
} else { |
191
|
|
|
$config = []; |
192
|
|
|
} |
193
|
|
|
|
194
|
10 |
|
$this->proxyClient = new Varnish($httpDispatcher, $config); |
195
|
|
|
} |
196
|
|
|
|
197
|
10 |
|
return $this->proxyClient; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get the hostname where your application can be reached. |
202
|
|
|
* |
203
|
|
|
* @throws \Exception |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
23 |
|
protected function getHostName() |
208
|
|
|
{ |
209
|
|
|
// @codeCoverageIgnoreStart |
210
|
|
|
if (!defined('WEB_SERVER_HOSTNAME')) { |
211
|
|
|
throw new \Exception( |
212
|
|
|
'To use this test, you need to define the WEB_SERVER_HOSTNAME constant in your phpunit.xml' |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
// @codeCoverageIgnoreEnd |
216
|
|
|
|
217
|
23 |
|
return WEB_SERVER_HOSTNAME; |
|
|
|
|
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|