Skip to content

Integration tests #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.3
- 5.4
- 5.5

Expand All @@ -12,5 +11,15 @@ env:

before_script:
- composer require symfony/framework-bundle:${SYMFONY_VERSION}
- sudo apt-get update -qq

# Install Varnish
- sudo apt-get install -qq varnish
- sudo cp -f Tests/Functional/Fixtures/varnish/fos.vcl /etc/varnish/default.vcl
- sudo service varnish restart

# Start PHP built-in web server
- php -S 0.0.0.0:8000 -t Tests/Functional/Fixtures/web/ &
- sleep 1

script: phpunit --coverage-text
7 changes: 7 additions & 0 deletions Resources/doc/varnish.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Varnish
=======

This bundle is compatible with Varnish version 3.0 onwards. In order to use
this bundle with Varnish, you probably have to make changes to your Varnish
configuration.

Below you will find detailed Varnish configuration recommendations. For a quick
overview, have a look at [the configuration that we use for our functional
tests](Tests/Functional/Fixtures/varnish/fos.vcl).

Configuration and usage
-----------------------
Expand Down
68 changes: 68 additions & 0 deletions Tests/Functional/Fixtures/varnish/fos.vcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
backend default {
.host = "localhost";
.port = "8000";
}

acl invalidators {
"localhost";
}

sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ invalidators) {
error 405 "Not allowed";
}
return (lookup);
}

if (req.request == "BAN") {
if (!client.ip ~ invalidators) {
error 405 "Not allowed.";
}
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);
error 200 "Banned";
}
}

sub vcl_fetch {

# Set Ban-lurker friendly tags
set beresp.http.x-url = req.url;
set beresp.http.x-host = req.http.host;

# Ban cache tags
if (beresp.status >= 200 && beresp.status < 400
&& (req.request == "PUT" || req.request == "POST" || req.request == "PATCH" || req.request == "DELETE")
) {
ban("obj.http.x-cache-tags ~ " + beresp.http.x-cache-tags);
}
}

sub vcl_hit {
if (req.request == "PURGE") {
purge;
error 200 "Purged";
}
}

sub vcl_miss {
if (req.request == "PURGE") {
purge;
error 404 "Not in cache";
}
}

sub vcl_deliver {
# Add extra headers if debugging is enabled
if (resp.http.x-cache-debug) {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
} else {
# Remove custom headers when delivering to client
unset resp.http.x-url;
unset resp.http.x-host;
}
}
4 changes: 4 additions & 0 deletions Tests/Functional/Fixtures/web/cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
header('Cache-Control: max-age=3600');
header('Content-Type: text/html');
header('X-Cache-Debug: 1');
4 changes: 4 additions & 0 deletions Tests/Functional/Fixtures/web/json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
header('Cache-Control: max-age=3600');
header('Content-Type: text/json');
header('X-Cache-Debug: 1');
38 changes: 38 additions & 0 deletions Tests/Functional/FunctionalTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace FOS\HttpCacheBundle\Tests\Functional;

use Guzzle\Http\Client;
use Guzzle\Http\Message\Response;

abstract class FunctionalTestCase extends \PHPUnit_Framework_TestCase
{
private static $client;

const CACHE_MISS = 'MISS';
const CACHE_HIT = 'HIT';

public static function getClient()
{
if (null === self::$client) {
self::$client = new Client('http://localhost:6081');
}

return self::$client;
}

public static function getResponse($url)
{
return self::getClient()->get($url);
}

public function assertMiss(Response $response, $message = null)
{
$this->assertEquals(self::CACHE_MISS, (string) $response->getHeader('X-Cache'), $message);
}

public function assertHit(Response $response, $message = null)
{
$this->assertEquals(self::CACHE_HIT, (string) $response->getHeader('X-Cache'), $message);
}
}
70 changes: 70 additions & 0 deletions Tests/Functional/VarnishTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace FOS\HttpCacheBundle\Tests\Functional;

use FOS\HttpCacheBundle\Invalidation\Varnish;


class VarnishTest extends FunctionalTestCase
{
/**
* @var Varnish
*/
protected $varnish;

public function setUp()
{
$this->varnish = new Varnish(array('http://127.0.0.1:6081'), 'localhost:6081');

// After each test, restart Varnish to clear caches
exec('sudo service varnish restart');
}

public function testBanAll()
{
$this->assertMiss(self::getResponse('/cache.php')->send());
$this->assertHit(self::getResponse('/cache.php')->send());

$this->assertMiss(self::getResponse('/json.php')->send());
$this->assertHit(self::getResponse('/json.php')->send());

$this->varnish->ban('.*')->flush();
$this->assertMiss(self::getResponse('/cache.php')->send());
$this->assertMiss(self::getResponse('/json.php')->send());
}

public function testBanContentType()
{
$this->assertMiss(self::getResponse('/cache.php')->send());
$this->assertHit(self::getResponse('/cache.php')->send());

$this->assertMiss(self::getResponse('/json.php')->send());
$this->assertHit(self::getResponse('/json.php')->send());

$this->varnish->ban('.*', 'text/html')->flush();
$this->assertMiss(self::getResponse('/cache.php')->send());
$this->assertHit(self::getResponse('/json.php')->send());
}

public function testPurge()
{
$this->assertMiss(self::getResponse('/cache.php')->send());
$this->assertHit(self::getResponse('/cache.php')->send());

$this->varnish->purge('/cache.php')->flush();
$this->assertMiss(self::getResponse('/cache.php')->send());
}

public function testRefresh()
{
$this->assertMiss(self::getResponse('/cache.php')->send());
$response = self::getResponse('/cache.php')->send();
$this->assertHit($response);

$this->varnish->refresh('/cache.php')->flush();

sleep(1);
$refreshed = self::getResponse('/cache.php')->send();
$this->assertGreaterThan((string) $response->getHeader('Age'), (string) $refreshed->getHeader('Age'));
}
}