@@ -4,22 +4,30 @@ Testing Your Application
4
4
========================
5
5
6
6
This chapter describes how to test your application against your reverse proxy.
7
-
8
- The FOSHttpCache library provides base test case classes to help you write
9
- functional tests. This is helpful to test the way your application sets caching
10
- headers and invalidates cached content.
11
-
12
- By having your test classes extend one of the test case classes, you get:
13
-
14
- * independent tests: all previously cached content is removed in the tests
15
- ``setUp `` method. The way this is done depends on which reverse proxy you use;
16
- * an instance of this library’s client that is configured to talk to your
17
- reverse proxy server. See reverse proxy specific sections for details;
18
- * convenience methods for executing HTTP requests to your application:
19
- ``$this->getHttpAdapter() `` and ``$this->getResponse() ``;
20
- * custom assertions ``assertHit `` and ``assertMiss `` for validating a cache
7
+ By running your tests against a live instance of your reverse proxy, you can
8
+ validate the caching headers that your application sets, and the invalidation
9
+ rules that it defines.
10
+
11
+ The FOSHttpCache library provides traits and base test classes to help you write
12
+ functional tests. Using the traits, you can extend your own (or your
13
+ framework’s) base test classes. For convenience, you can also extend the
14
+ FOSHttpCache base test classes, as they include a sensible set of traits by
15
+ default.
16
+
17
+ By using the traits, you get:
18
+
19
+ * independent tests: all previously cached content is removed in the test’s
20
+ ``setUp() `` method;
21
+ * an instance of this library’s proxy client that is configured to talk to your
22
+ proxy server;
23
+ * a convenience method for executing (cached) HTTP requests to your application:
24
+ ``$this->getResponse() ``;
25
+ * custom assertions ``assertHit() `` and ``assertMiss() `` for validating a cache
21
26
hit/miss.
22
27
28
+ Configuration
29
+ -------------
30
+
23
31
The recommended way to configure the test case is by setting constants
24
32
in your ``phpunit.xml ``. Alternatively, you can override the getter methods.
25
33
@@ -56,89 +64,70 @@ You can override getters in your test class in the following way::
56
64
}
57
65
}
58
66
59
- VarnishTestCase
60
- ---------------
67
+ Traits
68
+ ------
61
69
62
- Configuration
63
- '''''''''''''
70
+ Caching Proxy Server Traits
71
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
72
+
73
+ FOSHttpCache provides three caching proxy traits that:
74
+
75
+ * if necessary, start your caching proxy server before running the tests
76
+ * clear any cached content between tests, so you can be sure they are isolated
77
+ from one another
78
+ * if necessary, stop the caching proxy server after the tests have finished
79
+ * provide ``getProxyClient() ``, which returns the right
80
+ :doc: `proxy client <proxy-clients >` for your proxy server.
64
81
65
- By default, the ``VarnishTestCase `` starts and stops a Varnish server for you.
66
- Make sure ``symfony/process `` is available in your project:
82
+ You only need to include one of these traits in your test classes. Which one
83
+ you need (``VarnishTest ``, ``NginxTest `` or ``SymfonyTest ``) depends on the
84
+ caching proxy server that you use.
67
85
68
- .. code-block :: bash
86
+ VarnishTest Trait
87
+ """""""""""""""""
69
88
70
- $ composer require symfony/ process
89
+ .. include :: includes/symfony- process.rst
71
90
72
- Then set your Varnish configuration (VCL) file. All available configuration
73
- parameters are shown below .
91
+ Then configure the following parameters. The web server hostname and path to
92
+ your VCL file are required .
74
93
75
94
======================= ========================= ================================================== ===========================================
76
95
Constant Getter Default Description
77
96
======================= ========================= ================================================== ===========================================
97
+ ``WEB_SERVER_HOSTNAME `` ``getHostName() `` hostname your application can be reached at
78
98
``VARNISH_FILE `` ``getConfigFile() `` your Varnish configuration (VCL) file
79
99
``VARNISH_BINARY `` ``getBinary() `` ``varnishd `` your Varnish binary
80
100
``VARNISH_PORT `` ``getCachingProxyPort() `` ``6181 `` port Varnish listens on
81
101
``VARNISH_MGMT_PORT `` ``getVarnishMgmtPort() `` ``6182 `` Varnish management port
82
102
``VARNISH_CACHE_DIR `` ``getCacheDir() `` ``sys_get_temp_dir() `` + ``/foshttpcache-varnish `` directory to use for cache
83
103
``VARNISH_VERSION `` ``getVarnishVersion() `` ``4 `` installed varnish application version
84
- ``WEB_SERVER_HOSTNAME `` ``getHostName() `` hostname your application can be reached at
85
104
======================= ========================= ================================================== ===========================================
86
105
87
- Enable Assertions
88
- '''''''''''''''''
89
-
90
- For the `assertHit ` and `assertMiss ` assertions to work, you need to add a
91
- :ref: `custom X-Cache header <varnish_debugging >` to responses served
92
- by your Varnish.
106
+ NginxTest Trait
107
+ """""""""""""""
93
108
94
- NginxTestCase
95
- -------------
96
-
97
- Configuration
98
- '''''''''''''
99
-
100
- By default, the ``NginxTestCase `` starts and stops the NGINX server for you and
101
- deletes all cached contents. Make sure ``symfony/process `` is available in your
102
- project:
103
-
104
- .. code-block :: bash
105
-
106
- $ composer require symfony/process
109
+ .. include :: includes/symfony-process.rst
107
110
108
- You have to set your NGINX configuration file. All available configuration
109
- parameters are shown below .
111
+ Then configure the following parameters. The web server hostname and path to
112
+ your NGINX configuration file are required .
110
113
111
114
======================= ========================= ================================================ ===========================================
112
115
Constant Getter Default Description
113
116
======================= ========================= ================================================ ===========================================
117
+ ``WEB_SERVER_HOSTNAME `` ``getHostName() `` hostname your application can be reached at
114
118
``NGINX_FILE `` ``getConfigFile() `` your NGINX configuration file
115
119
``NGINX_BINARY `` ``getBinary() `` ``nginx `` your NGINX binary
116
120
``NGINX_PORT `` ``getCachingProxyPort() `` ``8088 `` port NGINX listens on
117
121
``NGINX_CACHE_PATH `` ``getCacheDir() `` ``sys_get_temp_dir() `` + ``/foshttpcache-nginx `` directory to use for cache
118
122
Must match `proxy_cache_path ` directive in
119
123
your configuration file.
120
- ``WEB_SERVER_HOSTNAME `` ``getHostName() `` hostname your application can be reached at
121
124
======================= ========================= ================================================ ===========================================
122
125
123
- Enable Assertions
124
- '''''''''''''''''
125
-
126
- For the `assertHit ` and `assertMiss ` assertions to work, you need to add a
127
- :ref: `custom X-Cache header <nginx_debugging >` to responses served
128
- by your Nginx.
126
+ SymfonyTest Trait
127
+ """""""""""""""""
129
128
130
- SymfonyTestCase
131
- ---------------
132
-
133
- This test case helps to test invalidation requests with a symfony application
134
- running the Symfony HttpCache and invalidating its cache folder to get reliable
135
- tests.
136
-
137
- The ``SymfonyTestCase `` does automatically start a web server. It is assumed
138
- that the web server you run for the application has the HttpCache integrated.
139
-
140
- Configuration
141
- '''''''''''''
129
+ It is assumed that the web server you run for the application has the HttpCache
130
+ integrated.
142
131
143
132
======================= ========================= ================================================ ===========================================
144
133
Constant Getter Default Description
@@ -151,59 +140,91 @@ Constant Getter Default
151
140
running PHPUnit.
152
141
======================= ========================= ================================================ ===========================================
153
142
154
- Enable Assertions
155
- '''''''''''''''''
143
+ HttpCaller Trait
144
+ ~~~~~~~~~~~~~~~~
145
+
146
+ Provides your tests with a ``getResponse `` method, which retrieves a URI from
147
+ your application through the HTTP cache::
156
148
157
- For the `assertHit ` and `assertMiss ` assertions to work, you need to add debug
158
- information in your AppCache. Create the cache kernel with the option
159
- ``'debug' => true `` and add the following to your ``AppCache ``::
149
+ use FOS\HttpCache\Test\HttpCaller;
160
150
161
- public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
151
+ class YourTest extends \PHPUnit_Framework_TestCase
162
152
{
163
- $response = parent::handle($request, $type, $catch);
164
-
165
- if ($response->headers->has('X-Symfony-Cache')) {
166
- if (false !== strpos($response->headers->get('X-Symfony-Cache'), 'miss')) {
167
- $state = 'MISS';
168
- } elseif (false !== strpos($response->headers->get('X-Symfony-Cache'), 'fresh')) {
169
- $state = 'HIT';
170
- } else {
171
- $state = 'UNDETERMINED';
172
- }
173
- $response->headers->set('X-Cache', $state);
153
+ public function testCachingHeaders()
154
+ {
155
+ // Get some response from your application
156
+ $response = $this->getResponse('/path');
157
+
158
+ // Optionally with request headers and a custom method
159
+ $response = $this->getResponse('/path', ['Accept' => 'text/json'], 'PUT');
174
160
}
161
+ }
175
162
176
- return $response;
163
+ CacheAssertions Trait
164
+ ~~~~~~~~~~~~~~~~~~~~~
165
+
166
+ Provides cache hit/miss assertions to your tests. To enable the these
167
+ ``assertHit `` and ``assertMiss `` assertions, you need to configure your caching
168
+ server to set an `X-Cache ` header with the cache status:
169
+
170
+ * :ref: `Varnish <varnish_debugging >`
171
+ * :ref: `NGINX <nginx_debugging >`
172
+ * :ref: `Symfony HttpCache <symfony-cache x-debugging >`
173
+
174
+ Then use the assertions as follows::
175
+
176
+ use FOS\HttpCache\Test\CacheAssertions;
177
+
178
+ class YourTest extends \PHPUnit_Framework_TestCase
179
+ {
180
+ public function testCacheHitOrMiss()
181
+ {
182
+ // Assert the application response is a cache miss
183
+ $this->assertMiss($response);
184
+
185
+ // Or assert it is a hit
186
+ $this->assertHit($response);
187
+ }
177
188
}
178
189
179
- The ``UNDETERMINED `` state should never happen. If it does, it means that your
180
- HttpCache is not correctly set into debug mode.
190
+ Base Classes for Convenience
191
+ ----------------------------
192
+
193
+ If you prefer, you can extend your test classes from ``VarnishTestCase ``,
194
+ ``NginxTestCase `` or ``SymfonyTestCase ``. The appropriate traits will then
195
+ automatically be included.
181
196
182
197
Usage
183
198
-----
184
199
185
200
This example shows how you can test whether the caching headers your
186
- application sets influence Varnish as you expect them to::
201
+ application sets influence your caching proxy as you expect them to::
187
202
188
- use FOS\HttpCache\Test\VarnishTestCase;
203
+ use FOS\HttpCache\Test\CacheAssertions;
204
+ use FOS\HttpCache\Test\HttpCaller;
205
+ use FOS\HttpCache\Test\VarnishTest;
206
+ // or FOS\HttpCache\Test\NginxTest;
207
+ // or FOS\HttpCache\Test\SymfonyTest;
189
208
190
- class YourFunctionalTest extends VarnishTestCase
209
+ class YourTest extends \PHPUnit_Framework_TestCase
191
210
{
192
211
public function testCachingHeaders()
193
212
{
194
- // Varnish is restarted, so you don’t have to worry about previously
195
- // cached content. Before continuing, the VarnishTestCase waits for
196
- // Varnish to become available.
213
+ // The caching proxy is (re)started, so you don’t have to worry
214
+ // about previously cached content. Before continuing, the
215
+ // VarnishTest/ NginxTest trait waits for the caching proxy to
216
+ // become available.
197
217
198
- // Retrieve an URL from your application
218
+ // Retrieve a URL from your application
199
219
$response = $this->getResponse('/your/resource');
200
220
201
221
// Assert the response was a cache miss (came from the backend
202
222
// application)
203
223
$this->assertMiss($response);
204
224
205
- // Assume the URL /your/resource sets caching headers. If we retrieve
206
- // it again, we should have a cache hit (response delivered by Varnish):
225
+ // Assume the URL /your/resource sets caching headers. If we
226
+ // retrieve it again, we should have a cache hit (response delivered
227
+ // by the caching proxy):
207
228
$response = $this->getResponse('/your/resource');
208
229
$this->assertHit($response);
209
230
}
@@ -212,26 +233,34 @@ application sets influence Varnish as you expect them to::
212
233
This example shows how you can test whether your application purges content
213
234
correctly::
214
235
215
- public function testCachePurge()
236
+ use FOS\HttpCache\Test\CacheAssertions;
237
+ use FOS\HttpCache\Test\HttpCaller;
238
+ use FOS\HttpCache\Test\VarnishTest;
239
+ // or FOS\HttpCache\Test\NginxTest;
240
+ // or FOS\HttpCache\Test\SymfonyTest;
241
+
242
+ class YourTest extends \PHPUnit_Framework_TestCase
216
243
{
217
- // Again, Varnish is restarted, so your test is independent from
218
- // other tests
244
+ public function testCachePurge()
245
+ {
246
+ // Again, the caching proxy is restarted, so your test is independent
247
+ // from other tests
219
248
220
- $url = '/blog/articles/1';
249
+ $url = '/blog/articles/1';
221
250
222
- // First request must be a cache miss
223
- $this->assertMiss($this->getResponse($url));
251
+ // First request must be a cache miss
252
+ $this->assertMiss($this->getResponse($url));
224
253
225
- // Next requests must be a hit
226
- $this->assertHit($this->getResponse($url));
254
+ // Next requests must be a hit
255
+ $this->assertHit($this->getResponse($url));
227
256
228
- // Purge
229
- $this->varnish ->purge('/blog/articles/1');
257
+ // Purge
258
+ $this->getProxyClient() ->purge('/blog/articles/1');
230
259
231
- // First request after must again be a miss
232
- $this->assertMiss($this->getResponse($url));
260
+ // First request after must again be a miss
261
+ $this->assertMiss($this->getResponse($url));
262
+ }
233
263
}
234
264
235
- Tests for Nginx look the same but extend the NginxTestCase.
236
265
For more ideas, see this library’s functional tests in the
237
266
:source: `tests/Functional/ ` directory.
0 commit comments