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\Nginx; |
15
|
|
|
use FOS\HttpCache\Test\Proxy\NginxProxy; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Starts and clears the NGINX proxy between tests. |
19
|
|
|
* |
20
|
|
|
* You can define constants in your phpunit.xml to control how this test behaves. |
21
|
|
|
* |
22
|
|
|
* Note that the WEB_SERVER_HOSTNAME must also match with what you have in your |
23
|
|
|
* NGINX configuration file. |
24
|
|
|
* |
25
|
|
|
* To define constants in the phpunit file, use this syntax: |
26
|
|
|
* <php> |
27
|
|
|
* <const name="NGINX_FILE" value="./tests/FOS/HttpCache/Tests/Functional/Fixtures/nginx/fos.conf" /> |
28
|
|
|
* </php> |
29
|
|
|
* |
30
|
|
|
* NGINX_FILE NGINX configuration file (required if not passed to setUp) |
31
|
|
|
* NGINX_BINARY Executable for NGINX. This can also be the full path |
32
|
|
|
* to the file if the binary is not automatically found |
33
|
|
|
* (default nginx) |
34
|
|
|
* NGINX_PORT Port NGINX listens to (default 8088) |
35
|
|
|
* NGINX_CACHE_PATH directory to use for cache |
36
|
|
|
* Must match `proxy_cache_path` directive in |
37
|
|
|
* your configuration file. |
38
|
|
|
* (default sys_get_temp_dir() + '/foshttpcache-nginx') |
39
|
|
|
* WEB_SERVER_HOSTNAME hostname where your application can be reached (required) |
40
|
|
|
*/ |
41
|
|
|
trait NginxTest |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var Nginx |
45
|
|
|
*/ |
46
|
|
|
protected $proxyClient; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var NginxProxy |
50
|
|
|
*/ |
51
|
|
|
protected $proxy; |
52
|
|
|
|
53
|
6 |
|
protected function setUp() |
54
|
|
|
{ |
55
|
6 |
|
$this->getProxy()->clear(); |
56
|
6 |
|
} |
57
|
|
|
|
58
|
6 |
|
protected function tearDown() |
59
|
|
|
{ |
60
|
6 |
|
$this->getProxy()->stop(); |
61
|
6 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The default implementation looks at the constant NGINX_FILE. |
65
|
|
|
* |
66
|
|
|
* @throws \Exception |
67
|
|
|
* |
68
|
|
|
* @return string the path to the NGINX server configuration file to use with this test. |
69
|
|
|
*/ |
70
|
6 |
|
protected function getConfigFile() |
71
|
|
|
{ |
72
|
6 |
|
if (!defined('NGINX_FILE')) { |
73
|
|
|
throw new \Exception( |
74
|
|
|
'Specify the NGINX' |
75
|
|
|
.' configuration file path in phpunit.xml or override getConfigFile()' |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// NGINX needs an absolute path |
80
|
6 |
|
return realpath(NGINX_FILE); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Defaults to "nginx". |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
6 |
|
protected function getBinary() |
89
|
|
|
{ |
90
|
6 |
|
return defined('NGINX_BINARY') ? NGINX_BINARY : null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Defaults to 8088. |
95
|
|
|
* |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
6 |
|
protected function getCachingProxyPort() |
99
|
|
|
{ |
100
|
6 |
|
return defined('NGINX_PORT') ? NGINX_PORT : 8088; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get NGINX cache directory. |
105
|
|
|
*/ |
106
|
6 |
|
protected function getCacheDir() |
107
|
|
|
{ |
108
|
6 |
|
return defined('NGINX_CACHE_PATH') ? NGINX_CACHE_PATH : null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get the hostname where your application can be reached. |
113
|
|
|
* |
114
|
|
|
* @throws \Exception |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
6 |
|
protected function getHostName() |
119
|
|
|
{ |
120
|
6 |
|
if (!defined('WEB_SERVER_HOSTNAME')) { |
121
|
|
|
throw new \Exception( |
122
|
|
|
'To use this test, you need to define the WEB_SERVER_HOSTNAME constant in your phpunit.xml' |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
6 |
|
return WEB_SERVER_HOSTNAME; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return NginxProxy |
131
|
|
|
*/ |
132
|
6 |
|
protected function getProxy() |
133
|
|
|
{ |
134
|
6 |
|
if (null === $this->proxy) { |
135
|
6 |
|
$this->proxy = new NginxProxy($this->getConfigFile()); |
136
|
6 |
|
$this->proxy->setPort($this->getCachingProxyPort()); |
137
|
|
|
|
138
|
6 |
|
if ($this->getBinary()) { |
139
|
|
|
$this->proxy->setBinary($this->getBinary()); |
140
|
|
|
} |
141
|
|
|
|
142
|
6 |
|
if ($this->getCacheDir()) { |
143
|
|
|
$this->proxy->setCacheDir($this->getCacheDir()); |
144
|
|
|
} |
145
|
6 |
|
} |
146
|
|
|
|
147
|
6 |
|
return $this->proxy; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get proxy client. |
152
|
|
|
* |
153
|
|
|
* @param string $purgeLocation Optional purgeLocation |
154
|
|
|
* |
155
|
|
|
* @return Nginx |
156
|
|
|
*/ |
157
|
|
|
protected function getProxyClient($purgeLocation = '') |
158
|
|
|
{ |
159
|
|
|
if (null === $this->proxyClient) { |
160
|
|
|
$this->proxyClient = new Nginx( |
161
|
|
|
['http://127.0.0.1:'.$this->getCachingProxyPort()], |
162
|
|
|
['base_uri' => $this->getHostName()] |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
$this->proxyClient->setPurgeLocation($purgeLocation); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this->proxyClient; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|