Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[TwigBundle] Fix deprecation warnings about base_template_class #54810

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 1 commit into from
May 2, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
'bad' => ['key' => 'foo'],
],
'auto_reload' => true,
'base_template_class' => 'stdClass',
'cache' => '/tmp',
'charset' => 'ISO-8859-1',
'debug' => true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

$container->loadFromExtension('twig', [
'base_template_class' => 'stdClass',
]);
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig https://symfony.com/schema/dic/twig/twig-1.0.xsd">

<twig:config auto-reload="true" base-template-class="stdClass" cache="/tmp" charset="ISO-8859-1" debug="true" strict-variables="true">
<twig:config auto-reload="true" cache="/tmp" charset="ISO-8859-1" debug="true" strict-variables="true">
<twig:path namespace="namespace3">namespaced_path3</twig:path>
</twig:config>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/twig https://symfony.com/schema/dic/twig/twig-1.0.xsd">

<twig:config auto-reload="true" base-template-class="stdClass" cache="/tmp" charset="ISO-8859-1" debug="true" strict-variables="true" default-path="%kernel.project_dir%/Fixtures/templates">
<twig:config auto-reload="true" cache="/tmp" charset="ISO-8859-1" debug="true" strict-variables="true" default-path="%kernel.project_dir%/Fixtures/templates">
<twig:form-theme>MyBundle::form.html.twig</twig:form-theme>
<twig:global key="foo" id="bar" type="service" />
<twig:global key="baz">@@qux</twig:global>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" ?>

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

<twig:config base-template-class="stdClass" />
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ twig:
pi: 3.14
bad: {key: foo}
auto_reload: true
base_template_class: stdClass
cache: /tmp
charset: ISO-8859-1
debug: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
twig:
base_template_class: stdClass
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection;

use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\RuntimeLoaderPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\TwigExtension;
use Symfony\Bundle\TwigBundle\Tests\DependencyInjection\AcmeBundle\AcmeBundle;
Expand All @@ -31,6 +32,8 @@

class TwigExtensionTest extends TestCase
{
use ExpectDeprecationTrait;

public function testLoadEmptyConfiguration()
{
$container = $this->createContainer();
Expand All @@ -56,7 +59,7 @@ public function testLoadEmptyConfiguration()
/**
* @dataProvider getFormats
*/
public function testLoadFullConfiguration($format)
public function testLoadFullConfiguration(string $format)
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
Expand Down Expand Up @@ -91,17 +94,36 @@ public function testLoadFullConfiguration($format)
$options = $container->getDefinition('twig')->getArgument(1);
$this->assertTrue($options['auto_reload'], '->load() sets the auto_reload option');
$this->assertSame('name', $options['autoescape'], '->load() sets the autoescape option');
$this->assertEquals('stdClass', $options['base_template_class'], '->load() sets the base_template_class option');
$this->assertArrayNotHasKey('base_template_class', $options, '->load() does not set the base_template_class if none is provided');
$this->assertEquals('/tmp', $options['cache'], '->load() sets the cache option');
$this->assertEquals('ISO-8859-1', $options['charset'], '->load() sets the charset option');
$this->assertTrue($options['debug'], '->load() sets the debug option');
$this->assertTrue($options['strict_variables'], '->load() sets the strict_variables option');
}

/**
* @group legacy
*
* @dataProvider getFormats
*/
public function testLoadCustomBaseTemplateClassConfiguration(string $format)
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());

$this->expectDeprecation('Since symfony/twig-bundle 7.1: The child node "base_template_class" at path "twig" is deprecated.');

$this->loadFromFile($container, 'templateClass', $format);
$this->compileContainer($container);

$options = $container->getDefinition('twig')->getArgument(1);
$this->assertEquals('stdClass', $options['base_template_class'], '->load() sets the base_template_class option');
}

/**
* @dataProvider getFormats
*/
public function testLoadCustomTemplateEscapingGuesserConfiguration($format)
public function testLoadCustomTemplateEscapingGuesserConfiguration(string $format)
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
Expand All @@ -115,7 +137,7 @@ public function testLoadCustomTemplateEscapingGuesserConfiguration($format)
/**
* @dataProvider getFormats
*/
public function testLoadDefaultTemplateEscapingGuesserConfiguration($format)
public function testLoadDefaultTemplateEscapingGuesserConfiguration(string $format)
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
Expand All @@ -129,7 +151,7 @@ public function testLoadDefaultTemplateEscapingGuesserConfiguration($format)
/**
* @dataProvider getFormats
*/
public function testLoadCustomDateFormats($fileFormat)
public function testLoadCustomDateFormats(string $fileFormat)
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
Expand Down Expand Up @@ -178,7 +200,7 @@ public function testGlobalsWithDifferentTypesAndValues()
/**
* @dataProvider getFormats
*/
public function testTwigLoaderPaths($format)
public function testTwigLoaderPaths(string $format)
{
$container = $this->createContainer();
$container->registerExtension(new TwigExtension());
Expand Down Expand Up @@ -207,7 +229,7 @@ public function testTwigLoaderPaths($format)
], $paths);
}

public static function getFormats()
public static function getFormats(): array
{
return [
['php'],
Expand All @@ -219,7 +241,7 @@ public static function getFormats()
/**
* @dataProvider stopwatchExtensionAvailabilityProvider
*/
public function testStopwatchExtensionAvailability($debug, $stopwatchEnabled, $expected)
public function testStopwatchExtensionAvailability(bool $debug, bool $stopwatchEnabled, bool $expected)
{
$container = $this->createContainer();
$container->setParameter('kernel.debug', $debug);
Expand Down Expand Up @@ -290,7 +312,7 @@ public function testCustomHtmlToTextConverterService(string $format)
$this->assertEquals(new Reference('my_converter'), $bodyRenderer->getArgument('$converter'));
}

private function createContainer()
private function createContainer(): ContainerBuilder
{
$container = new ContainerBuilder(new ParameterBag([
'kernel.cache_dir' => __DIR__,
Expand All @@ -311,15 +333,15 @@ private function createContainer()
return $container;
}

private function compileContainer(ContainerBuilder $container)
private function compileContainer(ContainerBuilder $container): void
{
$container->getCompilerPassConfig()->setOptimizationPasses([]);
$container->getCompilerPassConfig()->setRemovingPasses([]);
$container->getCompilerPassConfig()->setAfterRemovingPasses([]);
$container->compile();
}

private function loadFromFile(ContainerBuilder $container, $file, $format)
private function loadFromFile(ContainerBuilder $container, string $file, string $format): void
{
$locator = new FileLocator(__DIR__.'/Fixtures/'.$format);

Expand Down