Skip to content

Commit 9e62f28

Browse files
authored
#643: Fix cache control expression Symfony DI configuration (#644)
1 parent 05da76f commit 9e62f28

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

src/DependencyInjection/FOSHttpCacheExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,10 @@ private function parseResponseMatcher(ContainerBuilder $container, array $config
259259
$id = 'fos_http_cache.cache_control.expression.'.md5($config['match_response']);
260260
if (!$container->hasDefinition($id)) {
261261
$childDefinition = (new ChildDefinition('fos_http_cache.response_matcher.cache_control.expression'))
262-
->replaceArgument(0, $config['match_response'])
262+
->setArgument(0, $config['match_response'])
263263
;
264-
if (!empty($config['match_response_expression_service'])) {
265-
$childDefinition->replaceArgument(1, new Reference($config['match_response_expression_service']));
264+
if (!empty($config['expression_language'])) {
265+
$childDefinition->setArgument(1, new Reference($config['expression_language']));
266266
}
267267
$container
268268
->setDefinition($id, $childDefinition)

tests/Unit/DependencyInjection/FOSHttpCacheExtensionTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
2727
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
2828
use Symfony\Component\DependencyInjection\Reference;
29+
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
2930
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
3031
use Symfony\Component\Routing\Router;
3132

@@ -489,6 +490,38 @@ public function testConfigLoadCacheControlExpression(): void
489490
$this->assertListenerHasRule($container, 'fos_http_cache.event_listener.cache_control');
490491
}
491492

493+
public function testConfigLoadCacheControlExpressionWithOverriddenExpressionLanguage(): void
494+
{
495+
$config = $this->getCacheControlExpressionFullConfig();
496+
497+
$container = $this->createContainer();
498+
$this->extension->load([$config], $container);
499+
500+
$id = 'fos_http_cache.cache_control.expression.'.md5('foobar');
501+
$this->assertTrue($container->hasDefinition($id), 'expression child definition not created as expected');
502+
$this->assertEquals(
503+
[
504+
'foobar',
505+
new Reference('app.expression_language'),
506+
],
507+
$container->getDefinition($id)->getArguments()
508+
);
509+
}
510+
511+
public function testContainerCompilesWithCacheControlExpressionConfig(): void
512+
{
513+
$config = $this->getCacheControlExpressionFullConfig();
514+
515+
$container = $this->createContainer();
516+
$this->extension->load([$config], $container);
517+
518+
$container->addDefinitions(['app.expression_language' => new Definition(ExpressionLanguage::class)]);
519+
520+
$container->compile();
521+
522+
$this->expectNotToPerformAssertions();
523+
}
524+
492525
/**
493526
* Check if comma separated strings are parsed as expected.
494527
*/
@@ -813,6 +846,28 @@ private function getBaseConfig(): array
813846
];
814847
}
815848

849+
/**
850+
* @return array<string, mixed>
851+
*/
852+
private function getCacheControlExpressionFullConfig(): array
853+
{
854+
return [
855+
'cache_control' => [
856+
'rules' => [
857+
[
858+
'match' => [
859+
'match_response' => 'foobar',
860+
'expression_language' => 'app.expression_language',
861+
],
862+
'headers' => [
863+
'cache_control' => ['public' => true],
864+
],
865+
],
866+
],
867+
],
868+
];
869+
}
870+
816871
/**
817872
* @param array $methods List of methods for the matcher. Empty array to not check.
818873
*

0 commit comments

Comments
 (0)