Skip to content

Commit

Permalink
increase test coverage and fix a bug in TagSubscriberPass
Browse files Browse the repository at this point in the history
  • Loading branch information
dbu committed Aug 4, 2014
1 parent db41aa3 commit 7d54491
Show file tree
Hide file tree
Showing 14 changed files with 416 additions and 11 deletions.
5 changes: 5 additions & 0 deletions Configuration/InvalidatePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class InvalidatePath extends ConfigurationAnnotation
*/
private $paths;

/**
* Handle path given without explicit key.
*
* @param string $data
*/
public function setValue($data)
{
$this->setPaths((is_array($data) ? $data: array($data)));
Expand Down
7 changes: 7 additions & 0 deletions Configuration/InvalidateRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class InvalidateRoute extends ConfigurationAnnotation
*/
private $params;

/**
* Handle route name given without explicit key.
*
* @param string $value The route name.
*/
public function setValue($value)
{
$this->setName($value);
Expand Down Expand Up @@ -67,12 +72,14 @@ public function setParams($params)
print_r($value, true)
));
}
// @codeCoverageIgnoreStart
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
throw new InvalidConfigurationException(sprintf(
'@InvalidateRoute param %s uses an expression but the ExpressionLanguage is not available.',
$name
));
}
// @codeCoverageIgnoreEnd
}
}

Expand Down
7 changes: 7 additions & 0 deletions Configuration/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class Tag extends ConfigurationAnnotation
private $tags;
private $expression;

/**
* Handle tags given without explicit key.
*
* @param string|array $data
*/
public function setValue($data)
{
$this->setTags(is_array($data) ? $data: array($data));
Expand All @@ -33,9 +38,11 @@ public function setValue($data)
*/
public function setExpression($expression)
{
// @codeCoverageIgnoreStart
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
throw new InvalidConfigurationException('@Tag param %s uses an expression but the ExpressionLanguage is not available.');
}
// @codeCoverageIgnoreEnd
$this->expression = $expression;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* Add tagged provider to the hash generator for user context
*/
class UserContextListenerPass implements CompilerPassInterface
class HashGeneratorPass implements CompilerPassInterface
{
const TAG_NAME = "fos_http_cache.user_context_provider";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;

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

namespace FOS\HttpCacheBundle\DependencyInjection;

use FOS\HttpCacheBundle\DependencyInjection\Compiler\UserContextListenerPass;
use FOS\HttpCacheBundle\DependencyInjection\Compiler\HashGeneratorPass;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -174,7 +174,7 @@ private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loa

if ($config['role_provider']) {
$container->getDefinition($this->getAlias().'.user_context.role_provider')
->addTag(UserContextListenerPass::TAG_NAME)
->addTag(HashGeneratorPass::TAG_NAME)
->setAbstract(false);
}
}
Expand Down
8 changes: 4 additions & 4 deletions FOSHttpCacheBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace FOS\HttpCacheBundle;

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

Expand All @@ -25,7 +25,7 @@ class FOSHttpCacheBundle extends Bundle
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new LoggerPass());
$container->addCompilerPass(new TagListenerPass());
$container->addCompilerPass(new UserContextListenerPass());
$container->addCompilerPass(new TagSubscriberPass());
$container->addCompilerPass(new HashGeneratorPass());
}
}
54 changes: 54 additions & 0 deletions Tests/Unit/Command/BaseInvalidateCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the FOSHttpCacheBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCacheBundle\Tests\Unit\Command;

use FOS\HttpCacheBundle\Command\InvalidatePathCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

class BaseInvalidateCommandTest extends \PHPUnit_Framework_TestCase
{
public function testContainerAccess()
{
$invalidator = \Mockery::mock('\FOS\HttpCacheBundle\CacheManager')
->shouldReceive('invalidatePath')->once()->with('/my/path')
->shouldReceive('invalidatePath')->once()->with('/other/path')
->shouldReceive('invalidatePath')->once()->with('/another')
->getMock()
;
$container = \Mockery::mock('\Symfony\Component\DependencyInjection\ContainerInterface')
->shouldReceive('get')->once()->with('fos_http_cache.cache_manager')->andReturn($invalidator)
->getMock()
;

$application = new Application();
$command = new InvalidatePathCommand();
$command->setContainer($container);
$application->add($command);

$command = $application->find('fos:httpcache:invalidate:path');
$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => $command->getName(),
'paths' => array('/my/path', '/other/path'),
));

// the only output should be generated by the listener in verbose mode
$this->assertEquals('', $commandTester->getDisplay());

// the cache manager is only fetched once
$commandTester->execute(array(
'command' => $command->getName(),
'paths' => array('/another'),
));
}
}
51 changes: 51 additions & 0 deletions Tests/Unit/Configuration/InvalidateRouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the FOSHttpCacheBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCacheBundle\Tests\Unit\Configuration;

use FOS\HttpCacheBundle\Command\InvalidatePathCommand;
use FOS\HttpCacheBundle\Configuration\InvalidateRoute;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;

/**
* Test the @InvalidateRoute annotation.
*/
class InvalidateRouteTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage InvalidateRoute params must be an array
*/
public function testExecuteInvalidParams()
{
new InvalidateRoute(array(
'name' => 'test',
'params' => 'foo',
));
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage InvalidateRoute param id must be string
*/
public function testExecuteNoExpression()
{
new InvalidateRoute(array(
'name' => 'test',
'params' => array(
'id' => array(
'this-is-not-expression' => 'something',
)
),
));
}
}
31 changes: 31 additions & 0 deletions Tests/Unit/Configuration/TagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the FOSHttpCacheBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCacheBundle\Tests\Unit\Configuration;

use FOS\HttpCacheBundle\Configuration\Tag;

/**
* Test the @InvalidateRoute annotation.
*/
class TagTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \FOS\HttpCacheBundle\Exception\InvalidTagException
* @expectedExceptionMessage is invalid because it contains ,
*/
public function testExecuteInvalidParams()
{
new Tag(array(
'tags' => array('foo,bar'),
));
}
}
83 changes: 83 additions & 0 deletions Tests/Unit/DependencyInjection/Compiler/HashGeneratorPassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the FOSHttpCacheBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCacheBundle\Tests\Unit\DependencyInjection\Compiler;

use FOS\HttpCacheBundle\DependencyInjection\Compiler\HashGeneratorPass;
use FOS\HttpCacheBundle\DependencyInjection\FOSHttpCacheExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

class HashGeneratorPassTest extends \PHPUnit_Framework_TestCase
{
/**
* @var FOSHttpCacheExtension
*/
private $extension;

/**
* @var HashGeneratorPass
*/
private $userContextListenerPass;

protected function setUp()
{
$this->extension = new FOSHttpCacheExtension();
$this->userContextListenerPass = new HashGeneratorPass();
}

/**
* Nothing happens when user_context.hash_generator is not enabled
*/
public function testConfigNoContext()
{
$container = $this->createContainer();
$config = $this->getBaseConfig();
$this->extension->load(array($config), $container);
$this->userContextListenerPass->process($container);
$this->assertCount(9, $container->getDefinitions());
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage No user context providers found
*/
public function testConfigNoProviders()
{
$container = $this->createContainer();
$config = $this->getBaseConfig();
$config['user_context']['enabled'] = true;
$this->extension->load(array($config), $container);
$this->userContextListenerPass->process($container);
}

protected function createContainer()
{
return new ContainerBuilder(new ParameterBag(array(
'kernel.debug' => false,
)));
}

protected function getBaseConfig()
{
return array(
'proxy_client' => array(
'varnish' => array(
'base_url' => 'my_hostname',
'servers' => array(
'127.0.0.1'
)
)
)
);
}
}

0 comments on commit 7d54491

Please sign in to comment.