From 9d936a628170577b9b81addb0155db36009c1eca Mon Sep 17 00:00:00 2001 From: David Maicher Date: Mon, 7 Mar 2022 11:37:23 +0100 Subject: [PATCH] Deprecate caching related functionality (#1011) * Deprecate caching related functionality * fix cs --- UPGRADE-4.x.md | 15 +- composer.json | 4 + src/Block/BlockContextManager.php | 33 ++++ src/Cache/HttpCacheHandler.php | 3 + src/Cache/HttpCacheHandlerInterface.php | 3 + src/Cache/NoopHttpCacheHandler.php | 3 + .../Compiler/BlockHelperCompilerPass.php | 62 ++++++++ src/DependencyInjection/Configuration.php | 74 ++++++++- .../SonataBlockExtension.php | 42 +++++- src/Model/BaseBlock.php | 3 + src/Model/BlockInterface.php | 2 + src/Resources/config/core.xml | 5 +- src/SonataBlockBundle.php | 2 + src/Templating/Helper/BlockHelper.php | 142 ++++++++++++++++-- src/Test/BlockServiceTestCase.php | 4 + tests/App/config.yml | 4 + tests/Block/BlockContextManagerTest.php | 7 + .../DependencyInjection/ConfigurationTest.php | 4 + .../SonataBlockExtensionTest.php | 10 +- tests/Templating/Helper/BlockHelperTest.php | 26 +++- 20 files changed, 419 insertions(+), 29 deletions(-) create mode 100644 src/DependencyInjection/Compiler/BlockHelperCompilerPass.php diff --git a/UPGRADE-4.x.md b/UPGRADE-4.x.md index 5c5a13ec..24a0598e 100644 --- a/UPGRADE-4.x.md +++ b/UPGRADE-4.x.md @@ -1,7 +1,20 @@ UPGRADE 4.x =========== -UPGRADE FROM 4.7 to 4.x +UPGRADE FROM 4.10 to 4.x +======================= + +### Deprecated caching functionality + +- The `Sonata\BlockBundle\Cache\HttpCacheHandlerInterface` interface and it's implementations have been deprecated +- The integration with `SonataCacheBundle` has been deprecated + +To get rid of reported deprecations and to prepare your projects for version 5.0 you need to +- remove dependency on `SonataCacheBundle` if you have it installed +- set configuration option `sonata_block.http_cache` to `false` +- remove all other caching related configuration + +UPGRADE FROM 4.7 to 4.8 ======================= ### `sonata-project/doctrine-extensions` is optional diff --git a/composer.json b/composer.json index 480c1ab7..ff01fee2 100644 --- a/composer.json +++ b/composer.json @@ -79,6 +79,10 @@ } }, "config": { + "allow-plugins": { + "composer/package-versions-deprecated": true, + "phpstan/extension-installer": true + }, "sort-packages": true }, "extra": { diff --git a/src/Block/BlockContextManager.php b/src/Block/BlockContextManager.php index 5405336d..bf553cd3 100644 --- a/src/Block/BlockContextManager.php +++ b/src/Block/BlockContextManager.php @@ -123,6 +123,7 @@ public function get($meta, array $settings = []): BlockContextInterface $blockContext = new BlockContext($block, $settings); + // NEXT_MAJOR: remove next line $this->setDefaultExtraCacheKeys($blockContext, $originalSettings); return $blockContext; @@ -132,17 +133,23 @@ private function configureSettings(OptionsResolver $optionsResolver, BlockInterf { // defaults for all blocks $optionsResolver->setDefaults([ + // NEXT_MAJOR: remove 'use_cache' => true, + // NEXT_MAJOR: remove 'extra_cache_keys' => [], 'attr' => [], 'template' => null, // NEXT_MAJOR: Remove the default value + // NEXT_MAJOR: remove 'ttl' => $block->getTtl(), ]); $optionsResolver + // NEXT_MAJOR: remove ->addAllowedTypes('use_cache', 'bool') + // NEXT_MAJOR: remove ->addAllowedTypes('extra_cache_keys', 'array') ->addAllowedTypes('attr', 'array') + // NEXT_MAJOR: remove ->addAllowedTypes('ttl', 'int') // NEXT_MAJOR: Remove bool and null. ->addAllowedTypes('template', ['null', 'string', 'bool']) @@ -159,6 +166,30 @@ static function (Options $options, $value): string { return ''; } ) + ) + // NEXT_MAJOR: Remove setDeprecated. + ->setDeprecated( + 'use_cache', + ...$this->deprecationParameters( + '4.x', + 'Block option "use_cache" is deprecated since sonata-project/block-bundle 4.x and will be removed in 5.0.' + ) + ) + // NEXT_MAJOR: Remove setDeprecated. + ->setDeprecated( + 'extra_cache_keys', + ...$this->deprecationParameters( + '4.x', + 'Block option "extra_cache_keys" is deprecated since sonata-project/block-bundle 4.x and will be removed in 5.0.' + ) + ) + // NEXT_MAJOR: Remove setDeprecated. + ->setDeprecated( + 'ttl', + ...$this->deprecationParameters( + '4.x', + 'Block option "ttl" is deprecated since sonata-project/block-bundle 4.x and will be removed in 5.0.' + ) ); // add type and class settings for block @@ -169,6 +200,8 @@ static function (Options $options, $value): string { } /** + * // NEXT_MAJOR: remove this method. + * * Adds context settings, to be able to rebuild a block context, to the * extra_cache_keys. * diff --git a/src/Cache/HttpCacheHandler.php b/src/Cache/HttpCacheHandler.php index d8d223b8..5dd4f2e1 100644 --- a/src/Cache/HttpCacheHandler.php +++ b/src/Cache/HttpCacheHandler.php @@ -17,6 +17,9 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\ResponseEvent; +/** + * @deprecated since sonata-project/block-bundle 4.x and will be removed in 5.0. + */ final class HttpCacheHandler implements HttpCacheHandlerInterface { /** diff --git a/src/Cache/HttpCacheHandlerInterface.php b/src/Cache/HttpCacheHandlerInterface.php index 56a60edd..ac585e5a 100644 --- a/src/Cache/HttpCacheHandlerInterface.php +++ b/src/Cache/HttpCacheHandlerInterface.php @@ -17,6 +17,9 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\ResponseEvent; +/** + * @deprecated since sonata-project/block-bundle 4.x and will be removed in 5.0. + */ interface HttpCacheHandlerInterface { /** diff --git a/src/Cache/NoopHttpCacheHandler.php b/src/Cache/NoopHttpCacheHandler.php index a8b3e599..c0cbf378 100644 --- a/src/Cache/NoopHttpCacheHandler.php +++ b/src/Cache/NoopHttpCacheHandler.php @@ -17,6 +17,9 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\ResponseEvent; +/** + * @deprecated since sonata-project/block-bundle 4.x and will be removed in 5.0. + */ final class NoopHttpCacheHandler implements HttpCacheHandlerInterface { public function alterResponse(Response $response): void diff --git a/src/DependencyInjection/Compiler/BlockHelperCompilerPass.php b/src/DependencyInjection/Compiler/BlockHelperCompilerPass.php new file mode 100644 index 00000000..ef3acf17 --- /dev/null +++ b/src/DependencyInjection/Compiler/BlockHelperCompilerPass.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\BlockBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\DependencyInjection\Parameter; +use Symfony\Component\DependencyInjection\Reference; + +/** + * @internal + * + * NEXT_MAJOR: remove this class + */ +final class BlockHelperCompilerPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container): void + { + /** @var array{by_class: array, by_type: array} $cacheBlocks */ + $cacheBlocks = $container->getParameter('sonata_block.cache_blocks'); + + $hasCacheBlocks = false; + foreach ($cacheBlocks as $blocks) { + foreach ($blocks as $cacheType) { + $hasCacheBlocks = $hasCacheBlocks || 'sonata.cache.noop' !== $cacheType; + } + } + + if (!$hasCacheBlocks) { + return; + } + + @trigger_error( + 'Defining cache blocks other than \'sonata.cache.noop\' is deprecated since sonata-project/block-bundle 4.x and will not be supported anymore in 5.0.', + \E_USER_DEPRECATED + ); + + $blockHelperDefinition = $container->getDefinition('sonata.block.templating.helper'); + $blockHelperDefinition->setArguments([ + new Reference('sonata.block.manager'), + new Parameter('sonata_block.cache_blocks'), + new Reference('sonata.block.renderer'), + new Reference('sonata.block.context_manager'), + new Reference('event_dispatcher'), + new Reference('sonata.cache.manager', ContainerInterface::NULL_ON_INVALID_REFERENCE), + new Reference('sonata.block.cache.handler', ContainerInterface::NULL_ON_INVALID_REFERENCE), + new Reference('debug.stopwatch', ContainerInterface::NULL_ON_INVALID_REFERENCE), + ]); + } +} diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index db24cf95..9130864d 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -13,6 +13,7 @@ namespace Sonata\BlockBundle\DependencyInjection; +use Symfony\Component\Config\Definition\BaseNode; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -29,6 +30,13 @@ final class Configuration implements ConfigurationInterface */ private $defaultContainerTemplates; + /** + * NEXT_MAJOR: remove this member. + * + * @var bool + */ + private $httpCacheDisabled = false; + /** * @param array $defaultContainerTemplates */ @@ -49,13 +57,23 @@ public function getConfigTreeBuilder(): TreeBuilder ->fixXmlConfig('block') ->fixXmlConfig('block_by_class') ->validate() - ->always(static function ($value) { - foreach ($value['blocks'] as $name => &$block) { + ->always(function (&$value) { + foreach ($value['blocks'] as &$block) { if (0 === \count($block['contexts'])) { $block['contexts'] = $value['default_contexts']; } } + // NEXT_MAJOR: remove this block + if (true !== $this->httpCacheDisabled) { + @trigger_error( + 'Not setting the "sonata_block.http_cache" config option to false is deprecated since sonata-project/block-bundle 4.x and will fail in 5.0.', + \E_USER_DEPRECATED + ); + } else { + $value['http_cache'] = false; + } + return $value; }) ->end() @@ -73,10 +91,24 @@ public function getConfigTreeBuilder(): TreeBuilder ->end() ->scalarNode('context_manager')->defaultValue('sonata.block.context_manager.default')->end() + // NEXT_MAJOR: deprecate option and only allow setting it to false ->arrayNode('http_cache') ->addDefaultsIfNotSet() + ->beforeNormalization() + ->always(function ($v) { + if (false === $v) { + $this->httpCacheDisabled = true; + + return []; + } + + return $v; + }) + ->end() ->children() + // NEXT_MAJOR: remove option ->scalarNode('handler')->defaultValue('sonata.block.cache.handler.default')->end() + // NEXT_MAJOR: remove option ->booleanNode('listener')->defaultTrue()->end() ->end() ->end() @@ -130,7 +162,16 @@ public function getConfigTreeBuilder(): TreeBuilder ->end() ->end() ->end() - ->scalarNode('cache')->defaultValue('sonata.cache.noop')->end() + // NEXT_MAJOR: remove cache option + ->scalarNode('cache') + ->defaultValue('sonata.cache.noop') + ->setDeprecated( + ...$this->getDeprecationMessage( + 'The "cache" option for configuring blocks is deprecated since sonata-project/block-bundle 4.x and will be removed in 5.0.', + '4.x' + ) + ) + ->end() ->arrayNode('settings') ->info('default settings') ->useAttributeAsKey('id') @@ -152,7 +193,15 @@ public function getConfigTreeBuilder(): TreeBuilder ->prototype('array') ->fixXmlConfig('setting') ->children() - ->scalarNode('cache')->defaultValue('sonata.cache.noop')->end() + ->scalarNode('cache') + ->defaultValue('sonata.cache.noop') + ->setDeprecated( + ...$this->getDeprecationMessage( + 'The "cache" option for configuring blocks_by_class is deprecated since sonata-project/block-bundle 4.x and will be removed in 5.0.', + '4.x' + ) + ) + ->end() ->arrayNode('settings') ->info('default settings') ->useAttributeAsKey('id') @@ -210,4 +259,21 @@ public function getConfiguration(array $config, ContainerBuilder $container) { return new self([]); } + + /** + * @return string[] + */ + private function getDeprecationMessage(string $message, string $version): array + { + // @phpstan-ignore-next-line + if (method_exists(BaseNode::class, 'getDeprecation')) { + return [ + 'sonata-project/block-bundle', + $version, + $message, + ]; + } + + return [$message]; + } } diff --git a/src/DependencyInjection/SonataBlockExtension.php b/src/DependencyInjection/SonataBlockExtension.php index 48f19635..4099ffbb 100644 --- a/src/DependencyInjection/SonataBlockExtension.php +++ b/src/DependencyInjection/SonataBlockExtension.php @@ -75,6 +75,7 @@ public function load(array $configs, ContainerBuilder $container): void $this->configureBlockContainers($container, $config); $this->configureContext($container, $config); $this->configureLoaderChain($container, $config); + // NEXT_MAJOR: remove next line $this->configureCache($container, $config); $this->configureForm($container, $config); $this->configureProfiler($container, $config); @@ -91,6 +92,18 @@ public function load(array $configs, ContainerBuilder $container): void } $container->getDefinition('sonata.block.twig.global')->replaceArgument(0, $config['templates']); + + $container->getDefinition('sonata.block.cache.handler.default') + ->setDeprecated(...$this->getDeprecationMessage( + 'The "%service_id%" service is deprecated since sonata-project/block-bundle 4.x and will be removed in 5.0.', + '4.x', + )); + + $container->getDefinition('sonata.block.cache.handler.noop') + ->setDeprecated(...$this->getDeprecationMessage( + 'The "%service_id%" service is deprecated since sonata-project/block-bundle 4.x and will be removed in 5.0.', + '4.x', + )); } /** @@ -119,17 +132,19 @@ public function configureContext(ContainerBuilder $container, array $config): vo } /** - * NEXT_MAJOR: Change visibility to private. + * NEXT_MAJOR: Remove this method. * * @param array $config */ public function configureCache(ContainerBuilder $container, array $config): void { - $container->setAlias('sonata.block.cache.handler', $config['http_cache']['handler']); + if (\is_array($config['http_cache'])) { + $container->setAlias('sonata.block.cache.handler', $config['http_cache']['handler']); - if (null !== $config['http_cache']['listener']) { - $container->getDefinition($config['http_cache']['handler']) - ->addTag('kernel.event_listener', ['event' => 'kernel.response', 'method' => 'onKernelResponse']); + if (true === $config['http_cache']['listener']) { + $container->getDefinition($config['http_cache']['handler']) + ->addTag('kernel.event_listener', ['event' => 'kernel.response', 'method' => 'onKernelResponse']); + } } $cacheBlocks = ['by_class' => [], 'by_type' => []]; @@ -257,4 +272,21 @@ public function getNamespace(): string { return 'http://sonata-project.com/schema/dic/block'; } + + /** + * @return mixed[] + */ + private function getDeprecationMessage(string $message, string $version): array + { + // @phpstan-ignore-next-line + if (method_exists(Definition::class, 'getDeprecation')) { + return [ + 'sonata-project/block-bundle', + $version, + $message, + ]; + } + + return [true, $message]; + } } diff --git a/src/Model/BaseBlock.php b/src/Model/BaseBlock.php index 39e2632d..072ec991 100644 --- a/src/Model/BaseBlock.php +++ b/src/Model/BaseBlock.php @@ -193,6 +193,9 @@ public function hasParent(): bool return $this->getParent() instanceof self; } + /** + * @deprecated since sonata-project/block-bundle 4.x and will be removed in 5.0. + */ public function getTtl(): int { if (false === $this->getSetting('use_cache', true)) { diff --git a/src/Model/BlockInterface.php b/src/Model/BlockInterface.php index d76cb637..30938a83 100644 --- a/src/Model/BlockInterface.php +++ b/src/Model/BlockInterface.php @@ -94,6 +94,8 @@ public function getUpdatedAt(): ?\DateTime; /** * Returns the block cache TTL. + * + * @deprecated since sonata-project/block-bundle 4.x and will be removed in 5.0. */ public function getTtl(): int; diff --git a/src/Resources/config/core.xml b/src/Resources/config/core.xml index e43434f2..600735ca 100644 --- a/src/Resources/config/core.xml +++ b/src/Resources/config/core.xml @@ -27,13 +27,12 @@ - %sonata_block.cache_blocks% - - + + diff --git a/src/SonataBlockBundle.php b/src/SonataBlockBundle.php index 54de6b1f..103c97c8 100644 --- a/src/SonataBlockBundle.php +++ b/src/SonataBlockBundle.php @@ -13,6 +13,7 @@ namespace Sonata\BlockBundle; +use Sonata\BlockBundle\DependencyInjection\Compiler\BlockHelperCompilerPass; use Sonata\BlockBundle\DependencyInjection\Compiler\GlobalVariablesCompilerPass; use Sonata\BlockBundle\DependencyInjection\Compiler\TweakCompilerPass; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -24,5 +25,6 @@ public function build(ContainerBuilder $container): void { $container->addCompilerPass(new TweakCompilerPass()); $container->addCompilerPass(new GlobalVariablesCompilerPass()); + $container->addCompilerPass(new BlockHelperCompilerPass()); } } diff --git a/src/Templating/Helper/BlockHelper.php b/src/Templating/Helper/BlockHelper.php index f65586f5..c75f4e8d 100644 --- a/src/Templating/Helper/BlockHelper.php +++ b/src/Templating/Helper/BlockHelper.php @@ -60,6 +60,8 @@ class BlockHelper private $blockServiceManager; /** + * NEXT_MAJOR: remove this member and all related code to usages within this class. + * * @var CacheManagerInterface|null */ private $cacheManager; @@ -114,26 +116,141 @@ class BlockHelper private $stopwatch; /** - * @param array{by_class: array, by_type: array} $cacheBlocks + * NEXT_MAJOR: remove the deprecated signature and cleanup the constructor. + * + * @param array{by_class: array, by_type: array}|BlockRendererInterface $blockRendererOrCacheBlocks */ public function __construct( BlockServiceManagerInterface $blockServiceManager, - array $cacheBlocks, - BlockRendererInterface $blockRenderer, - BlockContextManagerInterface $blockContextManager, - EventDispatcherInterface $eventDispatcher, + $blockRendererOrCacheBlocks, + object $blockContextManagerOrBlockRenderer, + object $eventDispatcherOrBlockContextManager, + ?object $stopwatchOrEventDispatcher = null, ?CacheManagerInterface $cacheManager = null, ?HttpCacheHandlerInterface $cacheHandler = null, ?Stopwatch $stopwatch = null ) { $this->blockServiceManager = $blockServiceManager; - $this->cacheBlocks = $cacheBlocks; - $this->blockRenderer = $blockRenderer; - $this->eventDispatcher = $eventDispatcher; - $this->cacheManager = $cacheManager; - $this->blockContextManager = $blockContextManager; - $this->cacheHandler = $cacheHandler; - $this->stopwatch = $stopwatch; + + if ($blockRendererOrCacheBlocks instanceof BlockRendererInterface) { + $this->blockRenderer = $blockRendererOrCacheBlocks; + $this->cacheBlocks = ['by_class' => [], 'by_type' => []]; + } elseif (\is_array($blockRendererOrCacheBlocks)) { + $this->cacheBlocks = $blockRendererOrCacheBlocks; + @trigger_error( + sprintf( + 'Passing an array as argument 2 for method "%s" is deprecated since sonata-project/block-bundle 4.x. The argument will change to "%s" in 5.0.', + __METHOD__, + BlockRendererInterface::class + ), + \E_USER_DEPRECATED + ); + } else { + throw new \TypeError( + sprintf( + 'Argument 2 of method "%s" must be an array or an instance of "%s"', + __METHOD__, + BlockRendererInterface::class + ) + ); + } + + if ($blockContextManagerOrBlockRenderer instanceof BlockContextManagerInterface) { + $this->blockContextManager = $blockContextManagerOrBlockRenderer; + } elseif ($blockContextManagerOrBlockRenderer instanceof BlockRendererInterface) { + $this->blockRenderer = $blockContextManagerOrBlockRenderer; + @trigger_error( + sprintf( + 'Passing an instance of "%s" as argument 3 for method "%s" is deprecated since sonata-project/block-bundle 4.x. The argument will change to "%s" in 5.0.', + BlockRendererInterface::class, + __METHOD__, + BlockContextManagerInterface::class + ), + \E_USER_DEPRECATED + ); + } else { + throw new \TypeError( + sprintf( + 'Argument 3 of method "%s" must be an instance of "%s" or "%s"', + __METHOD__, + BlockContextManagerInterface::class, + BlockRendererInterface::class + ) + ); + } + + if ($eventDispatcherOrBlockContextManager instanceof EventDispatcherInterface) { + $this->eventDispatcher = $eventDispatcherOrBlockContextManager; + } elseif ($eventDispatcherOrBlockContextManager instanceof BlockContextManagerInterface) { + $this->blockContextManager = $eventDispatcherOrBlockContextManager; + @trigger_error( + sprintf( + 'Passing an instance of "%s" as argument 4 for method "%s" is deprecated since sonata-project/block-bundle 4.x. The argument will change to "%s" in 5.0.', + BlockContextManagerInterface::class, + __METHOD__, + EventDispatcherInterface::class + ), + \E_USER_DEPRECATED + ); + } else { + throw new \TypeError( + sprintf( + 'Argument 4 of method "%s" must be an instance of "%s" or "%s"', + __METHOD__, + EventDispatcherInterface::class, + BlockContextManagerInterface::class + ) + ); + } + + if ($stopwatchOrEventDispatcher instanceof Stopwatch) { + $this->stopwatch = $stopwatchOrEventDispatcher; + } elseif ($stopwatchOrEventDispatcher instanceof EventDispatcherInterface) { + $this->eventDispatcher = $stopwatchOrEventDispatcher; + $this->stopwatch = $stopwatch; + @trigger_error( + sprintf( + 'Passing an instance of "%s" as argument 5 for method "%s" is deprecated since sonata-project/block-bundle 4.x. The argument will change to "%s" in 5.0.', + EventDispatcherInterface::class, + __METHOD__, + Stopwatch::class + ), + \E_USER_DEPRECATED + ); + } elseif (null !== $stopwatchOrEventDispatcher) { + throw new \TypeError( + sprintf( + 'Argument 5 of method "%s" must be "null" or an instance of "%s" or "%s"', + __METHOD__, + Stopwatch::class, + EventDispatcherInterface::class + ) + ); + } + + if (null !== $cacheManager) { + $this->cacheManager = $cacheManager; + @trigger_error( + sprintf( + 'Passing an instance of "%s" as argument 6 for method "%s" is deprecated since sonata-project/block-bundle 4.x. The argument will be removed in 5.0.', + CacheAdapterInterface::class, + __METHOD__ + ), + \E_USER_DEPRECATED + ); + } + + if (null !== $cacheHandler) { + $this->cacheHandler = $cacheHandler; + @trigger_error( + sprintf( + 'Passing an instance of "%s" as argument 7 for method "%s" is deprecated since sonata-project/block-bundle 4.x. The argument will be removed in 5.0.', + HttpCacheHandlerInterface::class, + __METHOD__ + ), + \E_USER_DEPRECATED + ); + } } /** @@ -228,6 +345,7 @@ public function render($block, array $options = []): string $service = $this->blockServiceManager->get($blockContext->getBlock()); + // NEXT_MAJOR: simplify code and remove all cache-related usages $useCache = true === $blockContext->getSetting('use_cache'); $cacheService = $useCache ? $this->getCacheService($blockContext->getBlock(), $stats) : null; diff --git a/src/Test/BlockServiceTestCase.php b/src/Test/BlockServiceTestCase.php index ac5e4ab5..5268aa63 100644 --- a/src/Test/BlockServiceTestCase.php +++ b/src/Test/BlockServiceTestCase.php @@ -79,10 +79,14 @@ protected function getBlockContext(BlockServiceInterface $blockService): BlockCo protected function assertSettings(array $expected, BlockContextInterface $blockContext): void { $completeExpectedOptions = $expected + [ + // NEXT_MAJOR: remove 'use_cache' => true, + // NEXT_MAJOR: remove 'extra_cache_keys' => [], 'attr' => [], + // NEXT_MAJOR: remove 'template' => null, + // NEXT_MAJOR: remove 'ttl' => 0, ]; diff --git a/tests/App/config.yml b/tests/App/config.yml index 634f3737..e7992a27 100644 --- a/tests/App/config.yml +++ b/tests/App/config.yml @@ -18,3 +18,7 @@ services: Sonata\BlockBundle\Tests\App\Controller\DemoController: tags: - controller.service_arguments + +# NEXT_MAJOR: remove this block +sonata_block: + http_cache: false diff --git a/tests/Block/BlockContextManagerTest.php b/tests/Block/BlockContextManagerTest.php index 936534a9..4ed86d7b 100644 --- a/tests/Block/BlockContextManagerTest.php +++ b/tests/Block/BlockContextManagerTest.php @@ -56,6 +56,11 @@ public function testGetWithValidData(): void ], $blockContext->getSettings()); } + /** + * NEXT_MAJOR: remove legacy group. + * + * @group legacy + */ public function testGetWithSettings(): void { $service = $this->createMock(AbstractBlockService::class); @@ -76,6 +81,7 @@ public function testGetWithSettings(): void $manager = new BlockContextManager($blockLoader, $serviceManager, $blocksCache); + // NEXT_MAJOR: remove ttl $settings = ['ttl' => 1, 'template' => 'custom.html.twig']; $blockContext = $manager->get($block, $settings); @@ -91,6 +97,7 @@ public function testGetWithSettings(): void ], 'attr' => [], 'template' => 'custom.html.twig', + // NEXT_MAJOR: remove ttl 'ttl' => 1, ], $blockContext->getSettings()); } diff --git a/tests/DependencyInjection/ConfigurationTest.php b/tests/DependencyInjection/ConfigurationTest.php index a29a8e01..edff4f86 100644 --- a/tests/DependencyInjection/ConfigurationTest.php +++ b/tests/DependencyInjection/ConfigurationTest.php @@ -22,6 +22,9 @@ final class ConfigurationTest extends TestCase /** * @param string[] $contexts * + * @group legacy + * NEXT_MAJOR: remove group legacy annotation + * * @dataProvider providerContexts */ public function testOptions(array $contexts): void @@ -62,6 +65,7 @@ public function testOptions(array $contexts): void 'template' => '@SonataBlock/Profiler/block.html.twig', ], 'context_manager' => 'sonata.block.context_manager.default', + // NEXT_MAJOR: change default options for http_cache 'http_cache' => [ 'handler' => 'sonata.block.cache.handler.default', 'listener' => true, diff --git a/tests/DependencyInjection/SonataBlockExtensionTest.php b/tests/DependencyInjection/SonataBlockExtensionTest.php index a4d7450a..a5d4146b 100644 --- a/tests/DependencyInjection/SonataBlockExtensionTest.php +++ b/tests/DependencyInjection/SonataBlockExtensionTest.php @@ -21,7 +21,10 @@ final class SonataBlockExtensionTest extends AbstractExtensionTestCase public function testLoadDefault(): void { $this->setParameter('kernel.bundles', []); - $this->load(); + $this->load([ + // NEXT_MAJOR: remove argument for load method + 'http_cache' => false, + ]); $this->assertContainerBuilderHasService('sonata.block.service.container'); $this->assertContainerBuilderHasService('sonata.block.service.empty'); @@ -35,7 +38,10 @@ public function testLoadDefault(): void public function testLoadWithKnpMenuBundle(): void { $this->setParameter('kernel.bundles', ['KnpMenuBundle' => true]); - $this->load(); + $this->load([ + // NEXT_MAJOR: remove argument for load method + 'http_cache' => false, + ]); $this->assertContainerBuilderHasService('sonata.block.service.menu'); } diff --git a/tests/Templating/Helper/BlockHelperTest.php b/tests/Templating/Helper/BlockHelperTest.php index 8d90ef38..1735381a 100644 --- a/tests/Templating/Helper/BlockHelperTest.php +++ b/tests/Templating/Helper/BlockHelperTest.php @@ -19,15 +19,38 @@ use Sonata\BlockBundle\Block\BlockRendererInterface; use Sonata\BlockBundle\Block\BlockServiceManagerInterface; use Sonata\BlockBundle\Block\Service\BlockServiceInterface; +use Sonata\BlockBundle\Cache\HttpCacheHandlerInterface; use Sonata\BlockBundle\Event\BlockEvent; use Sonata\BlockBundle\Model\Block; use Sonata\BlockBundle\Model\BlockInterface; use Sonata\BlockBundle\Templating\Helper\BlockHelper; +use Sonata\Cache\CacheManagerInterface; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Stopwatch\Stopwatch; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; final class BlockHelperTest extends TestCase { + /** + * @group legacy + * @doesNotPerformAssertions + * + * NEXT_MAJOR: remove this test. + */ + public function testDeprecatedConstructorSignature(): void + { + new BlockHelper( + $this->createMock(BlockServiceManagerInterface::class), + ['by_class' => [], 'by_type' => []], + $this->createMock(BlockRendererInterface::class), + $this->createMock(BlockContextManagerInterface::class), + $this->createMock(EventDispatcherInterface::class), + $this->createMock(CacheManagerInterface::class), + $this->createMock(HttpCacheHandlerInterface::class), + new Stopwatch() + ); + } + public function testRenderEventWithNoListener(): void { $blockServiceManager = $this->createMock(BlockServiceManagerInterface::class); @@ -38,8 +61,7 @@ public function testRenderEventWithNoListener(): void return $event; }); - $cacheBlocks = ['by_class' => [], 'by_type' => []]; - $helper = new BlockHelper($blockServiceManager, $cacheBlocks, $blockRenderer, $blockContextManager, $eventDispatcher); + $helper = new BlockHelper($blockServiceManager, $blockRenderer, $blockContextManager, $eventDispatcher); static::assertSame('', $helper->renderEvent('my.event')); }