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\Http\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() |
63
|
1 |
|
{ |
64
|
23 |
|
$this->getProxy()->clear(); |
65
|
23 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Stop Varnish. |
69
|
|
|
*/ |
70
|
23 |
|
protected function tearDown() |
71
|
|
|
{ |
72
|
23 |
|
$this->getProxy()->stop(); |
73
|
23 |
|
} |
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
|
10 |
|
protected function getConfigFile() |
83
|
|
|
{ |
84
|
10 |
|
if (!defined('VARNISH_FILE')) { |
85
|
|
|
throw new \Exception( |
86
|
|
|
'Specify the varnish configuration file path in phpunit.xml or override getConfigFile()' |
87
|
|
|
); |
88
|
1 |
|
} |
89
|
|
|
|
90
|
10 |
|
return VARNISH_FILE; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get Varnish binary. |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
23 |
|
protected function getBinary() |
99
|
|
|
{ |
100
|
23 |
|
return defined('VARNISH_BINARY') ? VARNISH_BINARY : null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get Varnish port. |
105
|
|
|
* |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
23 |
|
protected function getCachingProxyPort() |
109
|
|
|
{ |
110
|
23 |
|
return defined('VARNISH_PORT') ? VARNISH_PORT : 6181; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get Varnish management port. |
115
|
|
|
* |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
23 |
|
protected function getVarnishMgmtPort() |
119
|
|
|
{ |
120
|
23 |
|
return defined('VARNISH_MGMT_PORT') ? VARNISH_MGMT_PORT : null; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get Varnish cache directory. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
23 |
|
protected function getCacheDir() |
129
|
|
|
{ |
130
|
23 |
|
return defined('VARNISH_CACHE_DIR') ? VARNISH_CACHE_DIR : null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Defaults to 4. |
135
|
|
|
* |
136
|
|
|
* @return int |
137
|
|
|
*/ |
138
|
13 |
|
protected function getVarnishVersion() |
139
|
|
|
{ |
140
|
13 |
|
return getenv('VARNISH_VERSION') ?: '4.0'; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
23 |
|
protected function getProxy() |
147
|
|
|
{ |
148
|
23 |
|
if (null === $this->proxy) { |
149
|
23 |
|
$this->proxy = new VarnishProxy($this->getConfigFile()); |
150
|
23 |
|
if ($this->getBinary()) { |
151
|
|
|
$this->proxy->setBinary($this->getBinary()); |
152
|
|
|
} |
153
|
|
|
|
154
|
23 |
|
if ($this->getCachingProxyPort()) { |
155
|
23 |
|
$this->proxy->setPort($this->getCachingProxyPort()); |
156
|
23 |
|
} |
157
|
|
|
|
158
|
23 |
|
if ($this->getVarnishMgmtPort()) { |
159
|
|
|
$this->proxy->setManagementPort($this->getVarnishMgmtPort()); |
160
|
|
|
} |
161
|
|
|
|
162
|
23 |
|
if ($this->getCacheDir()) { |
163
|
|
|
$this->proxy->setCacheDir($this->getCacheDir()); |
164
|
|
|
} |
165
|
23 |
|
} |
166
|
|
|
|
167
|
23 |
|
return $this->proxy; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get Varnish proxy client. |
172
|
|
|
* |
173
|
|
|
* @return Varnish |
174
|
|
|
*/ |
175
|
10 |
View Code Duplication |
protected function getProxyClient() |
|
|
|
|
176
|
|
|
{ |
177
|
10 |
|
if (null === $this->proxyClient) { |
178
|
10 |
|
$httpDispatcher = new HttpDispatcher( |
179
|
10 |
|
['http://127.0.0.1:'.$this->getCachingProxyPort()], |
180
|
10 |
|
$this->getHostName().':'.$this->getCachingProxyPort() |
181
|
10 |
|
); |
182
|
10 |
|
$this->proxyClient = new Varnish($httpDispatcher); |
183
|
10 |
|
} |
184
|
|
|
|
185
|
10 |
|
return $this->proxyClient; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get the hostname where your application can be reached. |
190
|
|
|
* |
191
|
|
|
* @throws \Exception |
192
|
|
|
* |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
23 |
|
protected function getHostName() |
196
|
|
|
{ |
197
|
23 |
|
if (!defined('WEB_SERVER_HOSTNAME')) { |
198
|
|
|
throw new \Exception( |
199
|
|
|
'To use this test, you need to define the WEB_SERVER_HOSTNAME constant in your phpunit.xml' |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
|
203
|
23 |
|
return WEB_SERVER_HOSTNAME; |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.