Skip to content

Add integration tests against a Symfony app #39

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 6 commits into from
Mar 15, 2014
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
28 changes: 28 additions & 0 deletions DependencyInjection/Compiler/LoggerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace FOS\HttpCacheBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Attach Symfony2 logger to cache manager
*/
class LoggerPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->has('logger')) {
return;
}

$subscriber = $container->getDefinition('fos_http_cache.proxy.log_subscriber')
->setAbstract(false);

$container->getDefinition('fos_http_cache.cache_manager')
->addMethodCall('addSubscriber', array($subscriber));
}
}
27 changes: 27 additions & 0 deletions DependencyInjection/Compiler/TagListenerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace FOS\HttpCacheBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Check for required ControllerListener if TagListener is enabled
*/
class TagListenerPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if ($container->has('fos_http_cache.tag_listener')
&& !$container->has('sensio_framework_extra.controller.listener')
) {
throw new \RuntimeException(
'The TagListener requires SensioFrameworkExtraBundle’s ControllerListener. '
. 'Please install sensio/framework-extra-bundle and add it to your AppKernel.'
);
}
}
}
19 changes: 12 additions & 7 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function getConfigTreeBuilder()

$this->addRulesSection($rootNode);
$this->addVarnishSection($rootNode);
$this->addTagListenerSection($rootNode);
$this->addFlashMessageListenerSection($rootNode);
$this->addInvalidatorsSection($rootNode);

Expand Down Expand Up @@ -116,20 +117,24 @@ private function addVarnishSection(ArrayNodeDefinition $rootNode)
->end();
}

private function addTagListenerSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('tag_listener')
->canBeDisabled()
->end()
->end();
}

private function addFlashMessageListenerSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('flash_message_listener')
->canBeUnset()
->treatFalseLike(array('enabled' => false))
->treatTrueLike(array('enabled' => true))
->treatNullLike(array('enabled' => true))
->canBeEnabled()
->children()
->scalarNode('enabled')
->defaultTrue()
->info('Whether to enable the listener. Automatically set if the flash_message_listener is configured.')
->end()
->scalarNode('name')
->defaultValue('flashes')
->info('Name of the cookie to set for flashes.')
Expand Down
13 changes: 9 additions & 4 deletions DependencyInjection/FOSHttpCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function load(array $configs, ContainerBuilder $container)
$config = $this->processConfiguration($configuration, $configs);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
$loader->load('cache_manager.xml');

$container->setParameter($this->getAlias().'.debug', $config['debug']);
$container->setParameter($this->getAlias().'.invalidators', $config['invalidators']);
Expand Down Expand Up @@ -56,9 +56,6 @@ public function load(array $configs, ContainerBuilder $container)
}

if (isset($config['varnish'])) {
if (!class_exists('\Guzzle\Http\Client')) {
throw new \RuntimeException('The Varnish component requires guzzle/http to be installed');
}
$loader->load('varnish.xml');
$container->setParameter($this->getAlias().'.varnish.ips', $config['varnish']['ips']);
$container->setParameter($this->getAlias().'.varnish.host', $config['varnish']['host']);
Expand All @@ -68,6 +65,14 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('authorization_request_listener.xml');
}

if ($config['tag_listener']['enabled']) {
if (!class_exists('\Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
throw new \RuntimeException('The TagListener requires symfony/expression-language');
}

$loader->load('tag_listener.xml');
}

if (!empty($config['flash_message_listener']) && $config['flash_message_listener']['enabled']) {
$loader->load('flash_message_listener.xml');

Expand Down
11 changes: 11 additions & 0 deletions FOSHttpCacheBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@

namespace FOS\HttpCacheBundle;

use FOS\HttpCacheBundle\DependencyInjection\Compiler\LoggerPass;
use FOS\HttpCacheBundle\DependencyInjection\Compiler\TagListenerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class FOSHttpCacheBundle extends Bundle
{
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new LoggerPass());
$container->addCompilerPass(new TagListenerPass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@
class="FOS\HttpCacheBundle\CacheManager">
<argument type="service" id="fos_http_cache.http_cache" />
<argument type="service" id="router" />
<call method="setEventDispatcher">
<argument id="event_dispatcher" type="service" on-invalid="ignore" />
</call>
</service>

<service id="fos_http_cache.event_listener.invalidation" class="FOS\HttpCacheBundle\EventListener\InvalidationListener">
<service id="fos_http_cache.proxy.log_subscriber"
class="FOS\HttpCache\EventListener\LogSubscriber"
abstract="true">
<argument type="service" id="logger" />
</service>

<service id="fos_http_cache.event_listener.invalidation"
class="FOS\HttpCacheBundle\EventListener\InvalidationListener">
<argument type="service" id="fos_http_cache.cache_manager" />
<argument type="service" id="fos_http_cache.invalidator.collection" />
<argument type="service" id="router" />
Expand Down
14 changes: 14 additions & 0 deletions Resources/config/tag_listener.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="fos_http_cache.tag_listener"
class="FOS\HttpCacheBundle\EventListener\TagListener">
<argument type="service" id="fos_http_cache.cache_manager" />
<tag name="kernel.event_subscriber" event="kernel.response" />
</service>
</services>
</container>
9 changes: 3 additions & 6 deletions Resources/config/varnish.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

<services>
<service id="fos_http_cache.varnish"
class="FOS\HttpCacheBundle\HttpCache\Varnish">
<argument>%fos_http_cache.http_cache.varnish.ips%</argument>
<argument>%fos_http_cache.http_cache.varnish.host%</argument>
<call method="setLogger">
<argument id="logger" type="service" on-invalid="null" />
</call>
class="FOS\HttpCache\Invalidation\Varnish">
<argument>%fos_http_cache.varnish.ips%</argument>
<argument>%fos_http_cache.varnish.host%</argument>
</service>

<service id="fos_http_cache.http_cache"
Expand Down
21 changes: 0 additions & 21 deletions Tests/EventListener/Fixture/FooControllerTagAtMethod.php

This file was deleted.

57 changes: 57 additions & 0 deletions Tests/Functional/EventListener/TagListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace FOS\HttpCacheBundle\Tests\Functional\EventListener;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class TagListenerTest extends WebTestCase
{
public function testTagsAreSet()
{
$client = static::createClient();

$client->request('GET', '/test/list');
$response = $client->getResponse();
$this->assertEquals('all-items,item-123', $response->headers->get('X-Cache-Tags'));

$client->request('GET', '/test/123');
$response = $client->getResponse();
$this->assertEquals('item-123', $response->headers->get('X-Cache-Tags'));
}

public function testTagsAreInvalidated()
{
$client = static::createClient();

$client->getContainer()->mock(
'fos_http_cache.cache_manager',
'\FOS\HttpCacheBundle\CacheManager'
)
->shouldReceive('invalidateTags')->once()->with(array('all-items'))
->shouldReceive('invalidateTags')->once()->with(array('item-123'))
->shouldReceive('flush')->once()
;

$client->request('POST', '/test/123');
}

public function testErrorIsNotInvalidated()
{
$client = static::createClient();

$client->getContainer()->mock(
'fos_http_cache.cache_manager',
'\FOS\HttpCacheBundle\CacheManager'
)
->shouldReceive('invalidateTags')->never()
->shouldReceive('flush')->once()
;

$client->request('POST', '/test/error');
}

protected function tearDown()
{
static::createClient()->getContainer()->unmock('fos_http_cache.cache_manager');
}
}
40 changes: 40 additions & 0 deletions Tests/Functional/Fixtures/Controller/TestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\HttpCacheBundle\Configuration\Tag;

class TestController extends Controller
{
/**
* @Tag("all-items")
* @Tag("item-123")
*/
public function listAction()
{
return new Response('All items including 123');
}

/**
* @Tag(expression="'item-'~id")
*/
public function itemAction(Request $request, $id)
{
if (!$request->isMethodSafe()) {
$this->container->get('fos_http_cache.cache_manager')->invalidateTags(array('all-items'));
}

return new Response('Item ' . $id . ' invalidated');
}

/**
* @Tag("items")
*/
public function errorAction()
{
return new Response('Forbidden', 403);
}
}
52 changes: 52 additions & 0 deletions Tests/Functional/Fixtures/app/AppKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;

class AppKernel extends Kernel
{
/**
* {@inheritdoc}
*/
public function registerBundles()
{
return array(
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new \Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
new \Symfony\Bundle\MonologBundle\MonologBundle(),
new \FOS\HttpCacheBundle\FOSHttpCacheBundle(),
);
}

/**
* {@inheritdoc}
*/
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config.yml');
}

/**
* {@inheritdoc}
*/
public function getCacheDir()
{
return sys_get_temp_dir().'/fos-http-cache-bundle/cache';
}

/**
* {@inheritdoc}
*/
public function getLogDir()
{
return sys_get_temp_dir().'/fos-http-cache-bundle/logs';
}

/**
* {@inheritdoc}
*/
protected function getContainerBaseClass()
{
return '\PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer';
}
}
15 changes: 15 additions & 0 deletions Tests/Functional/Fixtures/app/config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
framework:
secret: fos
router:
resource: "%kernel.root_dir%/config/routing.yml"
test: ~

fos_http_cache:
varnish:
ips: 127.0.0.1

monolog:
handlers:
main:
type: stream
level: debug
Loading