Skip to content

Commit b252164

Browse files
committed
Add integration tests and Travis config
Add Varnish config Add backend to Varnish config Add note about minimum Varnish version Fix backend config and add debugging Use PHP built-in webserver on Travis Add base test class and Varnish tests Use localhost Add note on Varnish config Debug PHP server start Fix PHP server start
1 parent ad4eee6 commit b252164

File tree

7 files changed

+201
-1
lines changed

7 files changed

+201
-1
lines changed

.travis.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 5.3
54
- 5.4
65
- 5.5
76

@@ -12,5 +11,15 @@ env:
1211

1312
before_script:
1413
- composer require symfony/framework-bundle:${SYMFONY_VERSION}
14+
- sudo apt-get update -qq
15+
16+
# Install Varnish
17+
- sudo apt-get install -qq varnish
18+
- sudo cp -f Tests/Functional/Fixtures/varnish/fos.vcl /etc/varnish/default.vcl
19+
- sudo service varnish restart
20+
21+
# Start PHP built-in web server
22+
- php -S 0.0.0.0:8000 -t Tests/Functional/Fixtures/web/ &
23+
- sleep 1
1524

1625
script: phpunit --coverage-text

Resources/doc/varnish.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Varnish
22
=======
33

4+
This bundle is compatible with Varnish version 3.0 onwards. In order to use
5+
this bundle with Varnish, you probably have to make changes to your Varnish
6+
configuration.
7+
8+
Below you will find detailed Varnish configuration recommendations. For a quick
9+
overview, have a look at [the configuration that we use for our functional
10+
tests](Tests/Functional/Fixtures/varnish/fos.vcl).
411

512
Configuration and usage
613
-----------------------
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
backend default {
2+
.host = "localhost";
3+
.port = "8000";
4+
}
5+
6+
acl invalidators {
7+
"localhost";
8+
}
9+
10+
sub vcl_recv {
11+
if (req.request == "PURGE") {
12+
if (!client.ip ~ invalidators) {
13+
error 405 "Not allowed";
14+
}
15+
return (lookup);
16+
}
17+
18+
if (req.request == "BAN") {
19+
if (!client.ip ~ invalidators) {
20+
error 405 "Not allowed.";
21+
}
22+
ban("obj.http.host ~ " + req.http.x-host + " && obj.http.x-url ~ " + req.http.x-url + " && obj.http.content-type ~ " + req.http.x-content-type);
23+
error 200 "Banned";
24+
}
25+
}
26+
27+
sub vcl_fetch {
28+
29+
# Set Ban-lurker friendly tags
30+
set beresp.http.x-url = req.url;
31+
set beresp.http.x-host = req.http.host;
32+
33+
# Ban cache tags
34+
if (beresp.status >= 200 && beresp.status < 400
35+
&& (req.request == "PUT" || req.request == "POST" || req.request == "PATCH" || req.request == "DELETE")
36+
) {
37+
ban("obj.http.x-cache-tags ~ " + beresp.http.x-cache-tags);
38+
}
39+
}
40+
41+
sub vcl_hit {
42+
if (req.request == "PURGE") {
43+
purge;
44+
error 200 "Purged";
45+
}
46+
}
47+
48+
sub vcl_miss {
49+
if (req.request == "PURGE") {
50+
purge;
51+
error 404 "Not in cache";
52+
}
53+
}
54+
55+
sub vcl_deliver {
56+
# Add extra headers if debugging is enabled
57+
if (resp.http.x-cache-debug) {
58+
if (obj.hits > 0) {
59+
set resp.http.X-Cache = "HIT";
60+
} else {
61+
set resp.http.X-Cache = "MISS";
62+
}
63+
} else {
64+
# Remove custom headers when delivering to client
65+
unset resp.http.x-url;
66+
unset resp.http.x-host;
67+
}
68+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
header('Cache-Control: max-age=3600');
3+
header('Content-Type: text/html');
4+
header('X-Cache-Debug: 1');
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
header('Cache-Control: max-age=3600');
3+
header('Content-Type: text/json');
4+
header('X-Cache-Debug: 1');
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace FOS\HttpCacheBundle\Tests\Functional;
4+
5+
use Guzzle\Http\Client;
6+
use Guzzle\Http\Message\Response;
7+
8+
abstract class FunctionalTestCase extends \PHPUnit_Framework_TestCase
9+
{
10+
private static $client;
11+
12+
const CACHE_MISS = 'MISS';
13+
const CACHE_HIT = 'HIT';
14+
15+
public static function getClient()
16+
{
17+
if (null === self::$client) {
18+
self::$client = new Client('http://localhost:6081');
19+
}
20+
21+
return self::$client;
22+
}
23+
24+
public static function getResponse($url)
25+
{
26+
return self::getClient()->get($url);
27+
}
28+
29+
public function assertMiss(Response $response, $message = null)
30+
{
31+
$this->assertEquals(self::CACHE_MISS, (string) $response->getHeader('X-Cache'), $message);
32+
}
33+
34+
public function assertHit(Response $response, $message = null)
35+
{
36+
$this->assertEquals(self::CACHE_HIT, (string) $response->getHeader('X-Cache'), $message);
37+
}
38+
}

Tests/Functional/VarnishTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace FOS\HttpCacheBundle\Tests\Functional;
4+
5+
use FOS\HttpCacheBundle\Invalidation\Varnish;
6+
7+
8+
class VarnishTest extends FunctionalTestCase
9+
{
10+
/**
11+
* @var Varnish
12+
*/
13+
protected $varnish;
14+
15+
public function setUp()
16+
{
17+
$this->varnish = new Varnish(array('http://127.0.0.1:6081'), 'localhost:6081');
18+
19+
// After each test, restart Varnish to clear caches
20+
exec('sudo service varnish restart');
21+
}
22+
23+
public function testBanAll()
24+
{
25+
$this->assertMiss(self::getResponse('/cache.php')->send());
26+
$this->assertHit(self::getResponse('/cache.php')->send());
27+
28+
$this->assertMiss(self::getResponse('/json.php')->send());
29+
$this->assertHit(self::getResponse('/json.php')->send());
30+
31+
$this->varnish->ban('.*')->flush();
32+
$this->assertMiss(self::getResponse('/cache.php')->send());
33+
$this->assertMiss(self::getResponse('/json.php')->send());
34+
}
35+
36+
public function testBanContentType()
37+
{
38+
$this->assertMiss(self::getResponse('/cache.php')->send());
39+
$this->assertHit(self::getResponse('/cache.php')->send());
40+
41+
$this->assertMiss(self::getResponse('/json.php')->send());
42+
$this->assertHit(self::getResponse('/json.php')->send());
43+
44+
$this->varnish->ban('.*', 'text/html')->flush();
45+
$this->assertMiss(self::getResponse('/cache.php')->send());
46+
$this->assertHit(self::getResponse('/json.php')->send());
47+
}
48+
49+
public function testPurge()
50+
{
51+
$this->assertMiss(self::getResponse('/cache.php')->send());
52+
$this->assertHit(self::getResponse('/cache.php')->send());
53+
54+
$this->varnish->purge('/cache.php')->flush();
55+
$this->assertMiss(self::getResponse('/cache.php')->send());
56+
}
57+
58+
public function testRefresh()
59+
{
60+
$this->assertMiss(self::getResponse('/cache.php')->send());
61+
$response = self::getResponse('/cache.php')->send();
62+
$this->assertHit($response);
63+
64+
$this->varnish->refresh('/cache.php')->flush();
65+
66+
sleep(1);
67+
$refreshed = self::getResponse('/cache.php')->send();
68+
$this->assertGreaterThan((string) $response->getHeader('Age'), (string) $refreshed->getHeader('Age'));
69+
}
70+
}

0 commit comments

Comments
 (0)