Skip to content

Commit 1e3138c

Browse files
committed
document the symfony test case
1 parent 4b6599f commit 1e3138c

File tree

3 files changed

+78
-17
lines changed

3 files changed

+78
-17
lines changed

doc/cache-invalidator.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Create the cache invalidator by passing a proxy client as
1717
$client = new ProxyClient\Varnish(...);
1818
// or
1919
$client = new ProxyClient\Nginx(...);
20+
// or
21+
$client = new ProxyClient\Symfony(...);
2022

2123
$cacheInvalidator = new CacheInvalidator($client);
2224

doc/testing-your-application.rst

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
Testing 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

98
The 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

1312
By 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

2323
The 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

2632
Setting 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
6773
parameters are shown below.
6874

69-
======================= ========================= ================================================== ============================================
75+
======================= ========================= ================================================== ===========================================
7076
Constant 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

8187
Enable Assertions
8288
'''''''''''''''''
@@ -91,8 +97,8 @@ NginxTestCase
9197
Configuration
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
96102
project:
97103

98104
.. code-block:: bash
@@ -102,17 +108,17 @@ project:
102108
You have to set your NGINX configuration file. All available configuration
103109
parameters are shown below.
104110

105-
======================= ========================= ================================================ ==========================================
111+
======================= ========================= ================================================ ===========================================
106112
Constant 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

117123
Enable 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
122128
by 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+
124182
Usage
125183
-----
126184

src/Test/SymfonyTestCase.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@
2323
*
2424
* To define constants in the phpunit file, use this syntax:
2525
* <php>
26-
* <const name="WEB_SERVER_PORT" value="/tmp/foo" />
26+
* <const name="WEB_SERVER_PORT" value="8080" />
2727
* </php>
2828
*
29-
* WEB_SERVER_PORT port the PHP webserver listens on (default 8080)
29+
* WEB_SERVER_PORT port the PHP webserver listens on (required)
30+
* WEB_SERVER_HOSTNAME hostname where your application can be reached (required)
3031
*
31-
* Note that the SymfonyProxy also defines a SYMFONY_CACHE_DIR constant.
32+
* Note that the SymfonyProxy also uses a SYMFONY_CACHE_DIR constant.
3233
*/
3334
abstract class SymfonyTestCase extends ProxyTestCase
3435
{

0 commit comments

Comments
 (0)