33Testing Your Application
44========================
55
6- This chapter describes how to test your application against your reverse proxy
7- instance.
6+ This chapter describes how to test your application against your reverse proxy.
87
98The FOSHttpCache library provides base test case classes to help you write
10- functional tests. This may be helpful to test the way your application sets
11- caching headers and invalidates cached content.
9+ functional tests. This is helpful to test the way your application sets caching
10+ headers and invalidates cached content.
1211
1312By having your test classes extend one of the test case classes, you get:
1413
@@ -18,10 +17,17 @@ By having your test classes extend one of the test case classes, you get:
1817 reverse proxy server. See reverse proxy specific sections for details;
1918* convenience methods for executing HTTP requests to your application:
2019 ``$this->getHttpClient() `` and ``$this->getResponse() ``;
21- * custom assertions ``assertHit `` and ``assertMiss `` for validating a cache hit/miss.
20+ * custom assertions ``assertHit `` and ``assertMiss `` for validating a cache
21+ hit/miss.
2222
2323The recommended way to configure the test case is by setting constants
24- in your `phpunit.xml `. Alternatively, you can override the getter methods.
24+ in your ``phpunit.xml ``. Alternatively, you can override the getter methods.
25+
26+ You will need to run a web server to provide the PHP application you want to
27+ test. The test cases only handle running the caching proxy. With PHP 5.4 or
28+ newer, the easiest is to use the PHP built in web server. See the
29+ ``WebServerListener `` class in ``tests/Functional `` and how it is registered in
30+ ``phpunit.xml.dist ``.
2531
2632Setting Constants
2733~~~~~~~~~~~~~~~~~
@@ -66,17 +72,17 @@ Make sure ``symfony/process`` is available in your project:
6672 Then set your Varnish configuration (VCL) file. All available configuration
6773parameters are shown below.
6874
69- ======================= ========================= ================================================== ============================================
75+ ======================= ========================= ================================================== ===========================================
7076Constant Getter Default Description
71- ======================= ========================= ================================================== ============================================
77+ ======================= ========================= ================================================== ===========================================
7278``VARNISH_FILE `` ``getConfigFile() `` your Varnish configuration (VCL) file
7379``VARNISH_BINARY `` ``getBinary() `` ``varnishd `` your Varnish binary
7480``VARNISH_PORT `` ``getCachingProxyPort() `` ``6181 `` port Varnish listens on
7581``VARNISH_MGMT_PORT `` ``getVarnishMgmtPort() `` ``6182 `` Varnish management port
7682``VARNISH_CACHE_DIR `` ``getCacheDir() `` ``sys_get_temp_dir() `` + ``/foshttpcache-varnish `` directory to use for cache
7783``VARNISH_VERSION `` ``getVarnishVersion() `` ``3 `` installed varnish application version
7884``WEB_SERVER_HOSTNAME `` ``getHostName() `` hostname your application can be reached at
79- ======================= ========================= ================================================== ============================================
85+ ======================= ========================= ================================================== ===========================================
8086
8187Enable Assertions
8288'''''''''''''''''
@@ -91,8 +97,8 @@ NginxTestCase
9197Configuration
9298'''''''''''''
9399
94- By default, the ``NginxTestCase `` starts and stops the NGINX server for you and
95- deletes all cached contents. Make sure ``symfony/process `` is available in your
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
96102project:
97103
98104.. code-block :: bash
@@ -102,17 +108,17 @@ project:
102108 You have to set your NGINX configuration file. All available configuration
103109parameters are shown below.
104110
105- ======================= ========================= ================================================ ==========================================
111+ ======================= ========================= ================================================ ===========================================
106112Constant Getter Default Description
107- ======================= ========================= ================================================ ==========================================
113+ ======================= ========================= ================================================ ===========================================
108114``NGINX_FILE `` ``getConfigFile() `` your NGINX configuration file
109115``NGINX_BINARY `` ``getBinary() `` ``nginx `` your NGINX binary
110116``NGINX_PORT `` ``getCachingProxyPort() `` ``8088 `` port NGINX listens on
111117``NGINX_CACHE_PATH `` ``getCacheDir() `` ``sys_get_temp_dir() `` + ``/foshttpcache-nginx `` directory to use for cache
112118 Must match `proxy_cache_path ` directive in
113119 your configuration file.
114120``WEB_SERVER_HOSTNAME `` ``getHostName() `` hostname your application can be reached at
115- ======================= ========================= ================================================ ==========================================
121+ ======================= ========================= ================================================ ===========================================
116122
117123Enable Assertions
118124'''''''''''''''''
@@ -121,6 +127,58 @@ For the `assertHit` and `assertMiss` assertions to work, you need to add a
121127:ref: `custom X-Cache header <nginx_debugging >` to responses served
122128by your Nginx.
123129
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+ '''''''''''''
142+
143+ ======================= ========================= ================================================ ===========================================
144+ Constant Getter Default Description
145+ ======================= ========================= ================================================ ===========================================
146+ ``WEB_SERVER_HOSTNAME `` ``getHostName() `` hostname your application can be reached at
147+ ``WEB_SERVER_PORT `` ``getConfigFile() `` The port on which the web server runs
148+ ``SYMFONY_CACHE_DIR `` ``getCacheDir() `` ``sys_get_temp_dir() `` + ``/foshttpcache-nginx `` directory to use for cache
149+ Must match the configuration of your
150+ HttpCache and must be writable by the user
151+ running PHPUnit.
152+ ======================= ========================= ================================================ ===========================================
153+
154+ Enable Assertions
155+ '''''''''''''''''
156+
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 ``::
160+
161+ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
162+ {
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);
174+ }
175+
176+ return $response;
177+ }
178+
179+ The ``UNDETERMINED `` state should never happen. If it does, it means that your
180+ HttpCache is not correctly set into debug mode.
181+
124182Usage
125183-----
126184
0 commit comments