Skip to content

Commit 7d54491

Browse files
committed
increase test coverage and fix a bug in TagSubscriberPass
1 parent db41aa3 commit 7d54491

14 files changed

+416
-11
lines changed

Configuration/InvalidatePath.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class InvalidatePath extends ConfigurationAnnotation
2323
*/
2424
private $paths;
2525

26+
/**
27+
* Handle path given without explicit key.
28+
*
29+
* @param string $data
30+
*/
2631
public function setValue($data)
2732
{
2833
$this->setPaths((is_array($data) ? $data: array($data)));

Configuration/InvalidateRoute.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class InvalidateRoute extends ConfigurationAnnotation
2929
*/
3030
private $params;
3131

32+
/**
33+
* Handle route name given without explicit key.
34+
*
35+
* @param string $value The route name.
36+
*/
3237
public function setValue($value)
3338
{
3439
$this->setName($value);
@@ -67,12 +72,14 @@ public function setParams($params)
6772
print_r($value, true)
6873
));
6974
}
75+
// @codeCoverageIgnoreStart
7076
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
7177
throw new InvalidConfigurationException(sprintf(
7278
'@InvalidateRoute param %s uses an expression but the ExpressionLanguage is not available.',
7379
$name
7480
));
7581
}
82+
// @codeCoverageIgnoreEnd
7683
}
7784
}
7885

Configuration/Tag.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class Tag extends ConfigurationAnnotation
2323
private $tags;
2424
private $expression;
2525

26+
/**
27+
* Handle tags given without explicit key.
28+
*
29+
* @param string|array $data
30+
*/
2631
public function setValue($data)
2732
{
2833
$this->setTags(is_array($data) ? $data: array($data));
@@ -33,9 +38,11 @@ public function setValue($data)
3338
*/
3439
public function setExpression($expression)
3540
{
41+
// @codeCoverageIgnoreStart
3642
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
3743
throw new InvalidConfigurationException('@Tag param %s uses an expression but the ExpressionLanguage is not available.');
3844
}
45+
// @codeCoverageIgnoreEnd
3946
$this->expression = $expression;
4047
}
4148

DependencyInjection/Compiler/UserContextListenerPass.php renamed to DependencyInjection/Compiler/HashGeneratorPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* Add tagged provider to the hash generator for user context
2121
*/
22-
class UserContextListenerPass implements CompilerPassInterface
22+
class HashGeneratorPass implements CompilerPassInterface
2323
{
2424
const TAG_NAME = "fos_http_cache.user_context_provider";
2525

DependencyInjection/Compiler/TagListenerPass.php renamed to DependencyInjection/Compiler/TagSubscriberPass.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616

1717
/**
18-
* Check for required ControllerListener if TagListener is enabled
18+
* Check for required ControllerListener if TagSubscriber is enabled
1919
*/
20-
class TagListenerPass implements CompilerPassInterface
20+
class TagSubscriberPass implements CompilerPassInterface
2121
{
2222
/**
2323
* {@inheritdoc}
2424
*/
2525
public function process(ContainerBuilder $container)
2626
{
27-
if ($container->has('fos_http_cache.tag_listener')
27+
if ($container->has('fos_http_cache.event_listener.tag')
2828
&& !$container->has('sensio_framework_extra.controller.listener')
2929
) {
3030
throw new \RuntimeException(
31-
'The TagListener requires SensioFrameworkExtraBundle’s ControllerListener. '
31+
'Tag support requires SensioFrameworkExtraBundle’s ControllerListener for the annotations. '
3232
. 'Please install sensio/framework-extra-bundle and add it to your AppKernel.'
3333
);
3434
}

DependencyInjection/FOSHttpCacheExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace FOS\HttpCacheBundle\DependencyInjection;
1313

14-
use FOS\HttpCacheBundle\DependencyInjection\Compiler\UserContextListenerPass;
14+
use FOS\HttpCacheBundle\DependencyInjection\Compiler\HashGeneratorPass;
1515
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\Config\FileLocator;
@@ -174,7 +174,7 @@ private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loa
174174

175175
if ($config['role_provider']) {
176176
$container->getDefinition($this->getAlias().'.user_context.role_provider')
177-
->addTag(UserContextListenerPass::TAG_NAME)
177+
->addTag(HashGeneratorPass::TAG_NAME)
178178
->setAbstract(false);
179179
}
180180
}

FOSHttpCacheBundle.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace FOS\HttpCacheBundle;
1313

1414
use FOS\HttpCacheBundle\DependencyInjection\Compiler\LoggerPass;
15-
use FOS\HttpCacheBundle\DependencyInjection\Compiler\TagListenerPass;
16-
use FOS\HttpCacheBundle\DependencyInjection\Compiler\UserContextListenerPass;
15+
use FOS\HttpCacheBundle\DependencyInjection\Compiler\TagSubscriberPass;
16+
use FOS\HttpCacheBundle\DependencyInjection\Compiler\HashGeneratorPass;
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
1818
use Symfony\Component\HttpKernel\Bundle\Bundle;
1919

@@ -25,7 +25,7 @@ class FOSHttpCacheBundle extends Bundle
2525
public function build(ContainerBuilder $container)
2626
{
2727
$container->addCompilerPass(new LoggerPass());
28-
$container->addCompilerPass(new TagListenerPass());
29-
$container->addCompilerPass(new UserContextListenerPass());
28+
$container->addCompilerPass(new TagSubscriberPass());
29+
$container->addCompilerPass(new HashGeneratorPass());
3030
}
3131
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCacheBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCacheBundle\Tests\Unit\Command;
13+
14+
use FOS\HttpCacheBundle\Command\InvalidatePathCommand;
15+
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Tester\CommandTester;
17+
18+
class BaseInvalidateCommandTest extends \PHPUnit_Framework_TestCase
19+
{
20+
public function testContainerAccess()
21+
{
22+
$invalidator = \Mockery::mock('\FOS\HttpCacheBundle\CacheManager')
23+
->shouldReceive('invalidatePath')->once()->with('/my/path')
24+
->shouldReceive('invalidatePath')->once()->with('/other/path')
25+
->shouldReceive('invalidatePath')->once()->with('/another')
26+
->getMock()
27+
;
28+
$container = \Mockery::mock('\Symfony\Component\DependencyInjection\ContainerInterface')
29+
->shouldReceive('get')->once()->with('fos_http_cache.cache_manager')->andReturn($invalidator)
30+
->getMock()
31+
;
32+
33+
$application = new Application();
34+
$command = new InvalidatePathCommand();
35+
$command->setContainer($container);
36+
$application->add($command);
37+
38+
$command = $application->find('fos:httpcache:invalidate:path');
39+
$commandTester = new CommandTester($command);
40+
$commandTester->execute(array(
41+
'command' => $command->getName(),
42+
'paths' => array('/my/path', '/other/path'),
43+
));
44+
45+
// the only output should be generated by the listener in verbose mode
46+
$this->assertEquals('', $commandTester->getDisplay());
47+
48+
// the cache manager is only fetched once
49+
$commandTester->execute(array(
50+
'command' => $command->getName(),
51+
'paths' => array('/another'),
52+
));
53+
}
54+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCacheBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCacheBundle\Tests\Unit\Configuration;
13+
14+
use FOS\HttpCacheBundle\Command\InvalidatePathCommand;
15+
use FOS\HttpCacheBundle\Configuration\InvalidateRoute;
16+
use Symfony\Component\Console\Application;
17+
use Symfony\Component\Console\Tester\CommandTester;
18+
19+
/**
20+
* Test the @InvalidateRoute annotation.
21+
*/
22+
class InvalidateRouteTest extends \PHPUnit_Framework_TestCase
23+
{
24+
/**
25+
* @expectedException \RuntimeException
26+
* @expectedExceptionMessage InvalidateRoute params must be an array
27+
*/
28+
public function testExecuteInvalidParams()
29+
{
30+
new InvalidateRoute(array(
31+
'name' => 'test',
32+
'params' => 'foo',
33+
));
34+
}
35+
36+
/**
37+
* @expectedException \RuntimeException
38+
* @expectedExceptionMessage InvalidateRoute param id must be string
39+
*/
40+
public function testExecuteNoExpression()
41+
{
42+
new InvalidateRoute(array(
43+
'name' => 'test',
44+
'params' => array(
45+
'id' => array(
46+
'this-is-not-expression' => 'something',
47+
)
48+
),
49+
));
50+
}
51+
}

Tests/Unit/Configuration/TagTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCacheBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCacheBundle\Tests\Unit\Configuration;
13+
14+
use FOS\HttpCacheBundle\Configuration\Tag;
15+
16+
/**
17+
* Test the @InvalidateRoute annotation.
18+
*/
19+
class TagTest extends \PHPUnit_Framework_TestCase
20+
{
21+
/**
22+
* @expectedException \FOS\HttpCacheBundle\Exception\InvalidTagException
23+
* @expectedExceptionMessage is invalid because it contains ,
24+
*/
25+
public function testExecuteInvalidParams()
26+
{
27+
new Tag(array(
28+
'tags' => array('foo,bar'),
29+
));
30+
}
31+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCacheBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCacheBundle\Tests\Unit\DependencyInjection\Compiler;
13+
14+
use FOS\HttpCacheBundle\DependencyInjection\Compiler\HashGeneratorPass;
15+
use FOS\HttpCacheBundle\DependencyInjection\FOSHttpCacheExtension;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\DefinitionDecorator;
18+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
19+
20+
class HashGeneratorPassTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* @var FOSHttpCacheExtension
24+
*/
25+
private $extension;
26+
27+
/**
28+
* @var HashGeneratorPass
29+
*/
30+
private $userContextListenerPass;
31+
32+
protected function setUp()
33+
{
34+
$this->extension = new FOSHttpCacheExtension();
35+
$this->userContextListenerPass = new HashGeneratorPass();
36+
}
37+
38+
/**
39+
* Nothing happens when user_context.hash_generator is not enabled
40+
*/
41+
public function testConfigNoContext()
42+
{
43+
$container = $this->createContainer();
44+
$config = $this->getBaseConfig();
45+
$this->extension->load(array($config), $container);
46+
$this->userContextListenerPass->process($container);
47+
$this->assertCount(9, $container->getDefinitions());
48+
}
49+
50+
/**
51+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
52+
* @expectedExceptionMessage No user context providers found
53+
*/
54+
public function testConfigNoProviders()
55+
{
56+
$container = $this->createContainer();
57+
$config = $this->getBaseConfig();
58+
$config['user_context']['enabled'] = true;
59+
$this->extension->load(array($config), $container);
60+
$this->userContextListenerPass->process($container);
61+
}
62+
63+
protected function createContainer()
64+
{
65+
return new ContainerBuilder(new ParameterBag(array(
66+
'kernel.debug' => false,
67+
)));
68+
}
69+
70+
protected function getBaseConfig()
71+
{
72+
return array(
73+
'proxy_client' => array(
74+
'varnish' => array(
75+
'base_url' => 'my_hostname',
76+
'servers' => array(
77+
'127.0.0.1'
78+
)
79+
)
80+
)
81+
);
82+
}
83+
}

0 commit comments

Comments
 (0)