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